CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

[C++] Practical C++ Part 4 - Conditional Statements


[Reply] [View by Thread] [Help]
[Back To Article Discussion Forum]

Posted by Author Rae On 2007-04-29 11:06:01




View and vote on the article here: Practical C++ Part 4 - Conditional Statements


Practical C++ Part 4 - Conditional Statements

Category
C++
Summary
Body
Practical C++ Part 4 - Conditional Statements


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 imported from the CyberArmy University site. (original author: Rae)


There are no replies to this post yet.



Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.02000 seconds


About Us | Privacy Policy | Mission Statement | Help