Friday, January 13, 2012

C++ EXPRESSIONS AND OPERATORS



I originally published this article on Yahoo Voices under the pen name John Mario.

THE IF STATEMENT

The simplest of the C++ control statements is the IF statement. This article will use the IF statement to explain the use of expressions and operators.

The best example of the IF statement are the ones written in plain English. Note that the examples are enclosed in double astericks.

The basic form of the IF statement is

'if condition then action

else some other action'

The condition is an expression containing variables and operators.

Here is an example written in the Englsigh language.

**

If it is raining then bring the umbrella

Else leave the umbrella home.

**

The condition is 'is it raining.'

The possible reactions are bring the umbrella or don't bring the umbrella.

In the C++ programming language, the condition is always placed in parenthesis.

THE == OPERATOR

The first operator we will consider is the operator ==.

This operator is used to determine if two values are equal. It is used in the IF statement within the following C++ program.

**

char answer; // The variable answer will contain a character

cout << "Will it rain today? " << endl; //Ask the user to a question

cout << "Type y for yes or n for no" << endl; // Give user choice of answers

cin >> answer; //Accept answer from user

If (answer == 'y') //test answer of 'y'

cout << "Bring Umbrella" << endl; // It will rain. Tell user to bring umbrella

Else

cout << "Leave umbrella home" << endl; //It won't rain. Leave umbrella home

**

The double equal sign indicates a comparison.

If the user makes the mistake of using a single equal sign in the expression , the ELSE statement will never be executed.

**

Example:

If (answer = 'y') // Error. Expression will assign 'y' to answer

cout << "Bring Umbrella" << endl; // Will always execute.

Else

cout << "Leave umbrella home" << endl; //Will never execute.

**

The reason is the single equal sign means assign the variable to the left of the equal statement the value on the right of the equal statement. This IF statement would unconditionally assign the character 'y' to the variable 'answer.'

THE != OPERARATOR

The operator != means 'not equal to.'

answer != 'y'

means answer is not equal to 'y'

The IF statement

if (answer != 'y')

cout << answer;

displays answer if the variable answer does not contain the character 'y'

THE > OPERATOR

The operator > means 'is greater than.'

The expression

answer > 4

in the IF statement

if (answer > 4)

cout << answer;

tests the value in the variable answer for a value greater than 4.

If the variable answer has a value that is greater than four than the value is displayed.

THE >= OPERATOR

The operator >= means 'greater than or equal to.'

The instruction

if (answer >= 4)

cout << answer;

means "if answer is greater than or equal to four, display answer."

THE <= OPERATOR

The operator <= means 'less than or equal to.'

The instruction

if (answer <= 4)

cout << answer;

means "if answer is less than or equal to four, display answer."

THE && OPERATOR

The && operator means AND.

If condition one exists and condition two exists do this.

Both condition one and condition two must be true.

The statement

if ((expression one) && (expression two))

means "if expression one is true AND expression two is true.

The && operator allows us to test for a range of values. Consider the following C++ program:

**

int answer; //answer will hold an integer

cout << "Type in one of the integers 4, 5, 6, 7, or 8 "; //ask user for integer value

cin >> answer; // Accept answer from user.

if ((answer >= 4) && (answer <= 8))

cout << "answer = " << answer << endl; //Answer within rage. Display answer.

else

cout << "answer out of range." << endl; //Tell user the answer is out of range.

**

THE || OPERATOR.

The || operator means 'or.'

The expression 'condition one OR condition two' means 'if either condition is true.'

The statement

if ((condition one) || (condition two))

cout << answer;

means " if condition one is true or condition two is true, display answer.

The || operator allows us to testing for a one of two values. Consider the following program.

**

char answer; //answer will contain a character

cout << are you at least 18 years old? (y/n)"; //ask user a question

cin >> answer //Accept answer

if ((answer == 'y') || (answer == 'n'))

cout << answer; //Answer is 'y' or 'n' Display answer.

Else

cout << "Error! Enter y for yes or n for no" << endl; // answer is not acceptable.

**
References: My own experiences during my career as a embedded systems software engineer.

The book 'C++ THE COMPLETE REFERENCE' by Herbert Schildt.

ISBN 0-07-222680-3


No comments: