Showing posts with label else. Show all posts
Showing posts with label else. Show all posts

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


A Generic Introduction To Computer Programming

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

This article does not focus on any particular computer language. It introduces computer programming by explaining some of the programming instructions that exist in all computer languages. It is directed toward those who are curious about computer programming, but have not yet decided to devote time and money to it.
 
Computer Programming is the art of using a set of instructions to force the computer to perform a certain task. The programming instructions described in this article will be written in plain english.
The following computer language instructions will be explained in this article:

The assignment statement
The PRINT instruction
The IF instruction
The ELSE instruction
The INPUT instruction
The WHILE instruction
The FOR loop
Instructions that perform math operations.

The Assignment statement
Any computer program needs to store temporary data provided by the programmer in memory. The programmer simply provides a name for the data being stored and the value of that data. The programmer does not have to know the exact memory location. The program automatically takes care of that.

For example: If you want to store the price of an apple, you could use the word apple_price as the name of the memory the price of apples is stored in. Then you store a price in the memory named apple_price. The instuction would look like this:

apple_price = 0.25

The statement simply states to store the value 0.25 in the memory named apple_price. This statement is known as an assignment statement. It assigns a value to the memory named apple_price.

The PRINT Instruction

The PRINT instruction can be used to display a value on the monitor. You can use the print instruction to display words, phrases and values.

Here is an instruction that displays the price of an apple.
print apple_price

Different languages will have different rules for typing the PRINT instruction. Some languages demand a set of parenthesis around the value to be printed.

print (apple_price)

You could also use the print statement to print a phrase. The instruction

print "Hello world!"

will display the message Hello world! on the monitor.

The IF Instruction

The IF instuction tells the computer when a specific action should be performed. For example, we want the computer to print the price of an apple if the price was less than one dollar:

If apple_price is less then 1.00, print apple_price.

The way to type less than is different for different languages.

The ELSE statement

The ELSE statement is part of the IF statement.
For example:

if apple-price is less than a quarter, then buy apples, else don't buy apples

The INPUT Instruction

The INPUT instruction tells the computer to read a value from a particular input. For purposes of this article, that input will be the keyboard. Not all programming languages name this instruction INPUT. The input instruction defines where it wants the input to be stored. For example

apple_price = input

tells the computer to input a value from the keyboard and store it in the memory named apple_price.

The WHILE Instruction

The while instruction commands the computer to keep doing something as long as specified condition exists. Here is a real life example of the WHILE instruction:

while tornado is near, stay in cellar

The FOR Instruction

The FOR instruction is used to repeat a something a specific number of times. The FOR instruction uses a counter to count the number of times the action is performed. Suppose we wanted to print apple_price five times. We would create a new memory name index to use as a counter.
For example

For index = 0 to 4, print apple_price

This instruction will set the value of index to 0 and prints the price of an apple, then it sets the value of index to 1 and prints the price of an apple. It increments the value of index and prints the price of an apple again. When the value of index is greater than 4, it stops printing the price of an apple. Hence, the price of an apple is printed five times: once for each value of index between 0 and 4.

Math
Math statements are also allowed in all programming languages. For example;

number_of_apples = 5
number_of_oranges = 3
total_number_of_fruit = number_of_apples + number_of_oranges

The memory named total will contain the sum of 5 and 3. Note that in this example, we wrote a short program containing three instructions. The first instruction sets the number_of_apples to 5. The second instruction sets the number_of_oranges to 3. The third instruction adds the number_of apples and the number_of_oranges to obtain the total_number_of_fruit. If we added an instruction to print the total_number_of_fruit, our program would look like this:

number_of_apples = 5
number_of_oranges = 3
total_number_of_fruit = number_of_apples + number_of_oranges
print total_number_of_fruit

References:

I have a Bachelor of Science degree in Electrical Engineering and I worked as an Embedded Systems Software Engineer. I designed and modified programs in several assembly languages and two high level languages.