In this section of the series, we are going to look at conditional statements, that is, "if-else" and "switch" statements.
The "if" statement is used to execute an instruction or block of instructions only if a condition is satisfied. The general form of an 'if' statement is,
if (condition) statement
Here <condition> implies the expression which has to be evaluated and then checked. If the result is TRUE, the <statement> is executed or processed, else the compiler just ignores the statement, and continues with the next instruction. If there is a block of statements to be executed, they are to be enclosed within curly braces { and }. Let us take an example to make the point more clear :-
if(a==5)
cout<<"Variable A has a value of 5"<<endl;
This code will check whether variable 'a' has a value equal to 5, if yes, it prints the appropriate message with the help of the corresponding 'cout' statement. If no, then the compiler ignores the 'cout' statement and continues normal execution. A point to notice here is the position of the semicolon ';'. It is placed not after the 'if' statement, but after the statement which is to be executed. Also important to notice is the '==' used for checking. In C++, '=' is used as assignment operator, and '==' is used for checking condtions.
We now look at the 'if-else' statement. Take a look at the code below :-
>if(a==5)
cout<<"Variable A has a value of 5"<<endl;
else
cout<<"A is not equal to 5"<<endl;
In this code, if the value of 'a' is not 5, then the compiler executes the 'else' clause, and thus prints the corresponding statement, which states that 'A is not equal to 5'. Notice the positions of the semi-colons here also.
Another interesting mod of such conditional statements is the 'if-else-if' statement. This will be more clear if we look at the code given below :-
if(a==5)
cout<<"Variable A has a value of 5"<<endl;
else if(a==10)
cout<<"A is equal to 10"<<endl;
else
cout<<"A is not equal to 5 or 10"<<endl;
The example given is pretty much self-explanatory. Just remember, you can add as many as else-if statements as required.
We now take a look at another important statement called the 'switch'. This is very often used for making menus in applications. Take a look at the code below :-
cout<<"1. Add numbers"<<endl;
cout<<"2. Substract numbers"<<endl;
cout<<"Enter your choice :";
cin>>choice;
switch(choice)
{ case 1 : result = a+b;
break;
case 2 : result = a-b;
}
When the user enters his choice (1 or 2), the switch statement checks the value. If value is 1, it adds the numbers, if the value is 2, it substracts the numbers. A statement to notice here is the 'break' statement. It is used to break out of a loop, such as the 'switch'. If the 'break' is not used here, both a+b and a-b will be carried out. Hence it is imperative to use 'break' in such cases.
References :-
Written by Rae (13 January 2005)
Rae is a member of CAU Knowledge-Bank Tutorial Writers
This article was originally published by CyberArmy.net in the CyberArmy Library.
|
|