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.*/

#include
#include

//main starts here...
void main()
{

//to clear the screen.
clrscr();

// Three variables declared.
int a,b,c;

//Prompting the user to do some input.
cout<<"Enter 1st no"<cin>>a;
cout<<"Enter2nd no"<cin>>b;
cout<<"Enter 3rd no"<cin>>c;

//Check if 1st no is greater than b.
if(a>b)
{

//checking if 1st no is also greater than c.
if(a>c)
{
//Coz 1st no is greater than 2nd and also than 3rd.
cout<<"1st no is largest"<}

else
{
//Coz 3rd no is greater than 2nd and also than 1st.
cout<<"3rd no is largest"<}
}

else
{
//checking if 2nd no is also greater than 3rd.
if(b>c)
{
cout<<"2nd no is largest";
}
//Because 3rd no is largest then print it.
else
{
cout<<"3rd no is largest";
}
}
getch();
}


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...

int X=90;

if(X>0)
{
cout<<"X exceeds Zero"<}
else if(X==0)
{
cout<<"X equals Zero"<}
else if(X==10)
{
cout<<"X equals ten"<}
else
{
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.

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.

SYNTAX:

switch(variable_name)
{
case constant_expression1:
statements;
break;

case constant_expression2:
statements;
break;

case constant_expression3:
statements;
break;

case constant_expression4:
statements;
break;

default:
statements;
}

The keyword switch is followed by the variable in parentheses, as show below:
switch(X),

Each case keyword is followed by a case constant, in simple terms, we can say the condition.
case 1:

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.

Switch...Case continued.

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.

int X=20;

switch(X)
{
case 0:
cout<<"X is Zero"<break;

case 10:
cout<<"X is Ten"<break;

case 20:
cout<<"X is Twenty"<break;

default:
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.

While Loop:
---------------

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:

while(condition)
{
loop_statement;
}


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

int X=1;
while(X<11)
{
cout<X++;
}


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:


do
{
loop_statements;
}
while(condition);


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:

int X=1;
do
{
cout<}
while(X<11);


Like it on Facebook, Tweet it or share this article on other bookmarking websites.

No comments