If...else can also be nested inside other if...else constructs.
like we can put an if...else conditional construct inside another if...else also for further checking a different condition.
here's an example to check largest of 3 numbers.
/* Header Files included*/
//Also called preprocessor directives.
//iostrea.h to include basic input output functions i.e. cout() and cin().
/*conio.h header file to include getch() i.e. getcharacter function and clrscr() i.e. clrscr function.*/
//main starts here...
//to clear the screen.
// Three variables declared.
//Prompting the user to do some input.
cout<<"Enter 1st no"<
cout<<"Enter 3rd no"<
//Check if 1st no is greater than b.
//checking if 1st no is also greater than c.
//Coz 1st no is greater than 2nd and also than 3rd.
cout<<"1st no is largest"<
//Coz 3rd no is greater than 2nd and also than 1st.
cout<<"3rd no is largest"<
//checking if 2nd no is also greater than 3rd.
cout<<"2nd no is largest";
//Because 3rd no is largest then print it.
cout<<"3rd no is largest";
Note: Lines with leading with double slashes (//) and single slash with a star symbol(/*..........*/) are comments. These lines will not be compiled by the compiler and are ignored.
In if...else construct we can check for many conditions with
else if(condition) after an if(statement) expression. Like,
We want to check the value for a variable we can check like that...
cout<<"X exceeds Zero"<
cout<<"X equals Zero"<
cout<<"X equals ten"<
cout<<"X exceeds ten"<
As you can see from the above code, We can check the value for a variable using if... if else... else... construct.
But the statements became quite complex as with using many if and else if conditions.
The above statements can also be simplified by using Switch...Case construct.
This is used when there are multiple values for a variable. When it gets executed the condition variable is evaluated and compared with each case constant and the execution is transferred to the respective case block. and if no matching case block is found the execution is transferred to the default block.
The keyword switch is followed by the variable in parentheses, as show below:
Each case keyword is followed by a case constant, in simple terms, we can say the condition.
The datatype of the case constant should match with the datatype of the variable.
int this time.
The break keyword is used to skip the execution of the remaining statements after a condition is met.
The default keyword is used to execute the statements if non of the case constant matches the condition.
As i told we use switch...case construct when we've multiple values for a variable.
So we can check the values for a variable using switch case. For eg.
cout<<"X is Zero"<
cout<<"X is Ten"<
cout<<"X is Twenty"<
cout<<"X is not Zero or Ten nor Twenty"<
By executing the above statements, The condition is evaluated with each case expression. if any matching case found, the statements executes of that case otherwise the sequence of execution is transferred to the next case block. If non of the matching case block found, the default block executes.
There are many kinds of loops in C++.
So let's start with the While loop first.
The while loop is used to execute a statement or block of statements as long as a specified condition is true. The general form of the while loop is:
Here loop_statement is executed repeatedly as long as the condition expression has the value true. After the condition becomes false, the program continues with the statement following the loop. As always, a block of statements between braces could replace the single loop_statement.
You could write some statements to print numbers from 1 to 10. Like
The above loop statements will print numbers starting from 1 to 10.
Here's the explaination, how the above statements will be executed:
Initially X is having a value "1". The while loop will check the value of X if it is less than 11. If so the execution flow will enter the body of the loop and print the value of X i.e. 1 this time. Then X will be incremented by 1. Now X is having a value 2 which is again less than 11 so it'll again print the value of X i.e. 2 this time. So it'll print the value of X till it's value is 10. After this X will increment by 1 which will make X equals to 11. Now, when the condition is checked the value of X is not less than 11. So the loop will be discarded and the execution will continue with the statements following the loop.
Another kind of loop in C++ is do while loop.
Do... While loop:
The do-while loop is similar to the while loop in that the loop continues as long as the specified loop condition remains true. The main difference is that the condition is checked at the end of the loop which contrasts with the while loop and the for loop where the condition is checked at the beginning of the loop. Consequently, the do-while loop statement is always executed at least once. The general form of the do-while loop is:
You could replace the while loop in the last version of the program to print numbers from 0 to 10 with a do-while loop:
Like it on Facebook, Tweet it or share this article on other bookmarking websites.