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.

If Else & Nested If Else

IF ELSE STATEMENT

If statement execute single statement and else statement executes set of statements. When the test expressions or condition is true is does nothing otherwise it executes else statement.

Syntax

if (condition)
{
True-block statement(s)
}
else
{
False-block statement(s)
}

Example Program Code:

 main ()

{

Int charactercount=0;

Int wordcount=1; ch;

Clrscr()

Printf(“\n\n This program will count number of characters and words you entered”);

Printf(“Type whatever you like .and press enter to see result\n\n”);

While((ch=getche())=!’\r’) {

If(ch==” ’’)

Wordcout++;

Else

Charactercout++;

}

Printf(“entered characters are :”,charactercount);

Printf(“entered words are :”,wordcount);

Getch();

}

WHERE

-“int” it declares integer

-“charactercount” you can give any name you like and it should must explicable. =0 means its value         is zero. Here it will add all that characters you enter.

-“printf” what ever you write in it that appears on screen

-“clrscr()” use to clear all code above it and show result only code below it.

-“\r” use for enter

-“ch=getche()” ch is declared as character storage and =getche()means user will give input to program while it will run.

-“=!” not equal

-“ch==” <space>”” the space between inverted commas  it will count words compiler will take it as word for example “Science and technology ” we can differentiate these three words due to space between them that’s why  ch==”<space>”.

-“getch()” at the end it is important if use want prompt to stay till you close it.

 

OutPut

It depends what you entered.

Example Program Code:

if-else-if statement

void main(void)
{
int numb;
printf(“Type any Number : “);
scanf(“%d”, &numb);
if(numb > 0) {

    printf(“%d is the positive number”, numb);
}
else if(numb < 0)
printf(“%d is the Negative number”, numb);
else printf(“%d is zero”,numb);
}

Out Put

It depends what you entered.

                                                         

NESTED IF-ELSE STATEMENT

Use of If else inside if else statement

Syntax

{

int x,y;

For (y=1; y<12; y++)

             {

               For (x=1;x<10;x++)

                 If(y==x)

              Printf(“\xDB”);

              Else

             If(x==12-y)

            Printf(“\xB0”);

            Else

       }

}

OutPUT
Printf(“\n”);