IF & Nested IF Statement

IF statement

The if statement is a powerful decision making statement and is used to control
the flow of execution of statements. If statement is similar to while loop in operation as well as in format. In both cases statements making up the body and will not be executed at all if condition is not true. And if condition is true it will execute until it become false.

Syntax
    if (conditional)
{
block of statements executed if conditional is true;
}

Let’s see an example program code

/*Program will tell you what character you typed.*/

/*By Using IF Statement */

Void main (void)

{

Char ch;

Ch=getche();

If(ch==’y’)

Printf(“\n you entered y”);

}

 

WHERE:

-“char” is user to declare character

-“ch” means storage name in memory for character

-“getche()” use to take character form user

“==” means equal

-“\n” use for next line

 

OUTPUT:

Y

YOU ENTERED Y

NESTED IF STATEMENT

If statement inside if statement is nested if statement

Syntax

  {

  if (conditional)

              if(conditional)
block of statements executed if conditional is true;
}

Example Program Code

Void main (void)

 {

If(getche()==’M’)

     If(getche()==’E’)

Printf(“\you entered  ME”);

}

WHERE

-“getche()” use to take character form user

“==” means equal

 

OUTPUT

You Entered ME.