Jul 13

We can execute one group of statement if expression is true and another group of statements if expression evaluates to false. This is done by if else structure.

Syntax:

if (condition)

{

statements_1;

}

else

{

statements_2;

}

Example

main()

{

int num;

printf(”enter a number\n”);

scanf(”%d”, &num);

if(num<10)

printf(”\nnumber entered is less than 10″);

else

printf(”\nnumber entered is greaterĀ  than 10″);

}

Output:

enter a number

12 (say)

number enteredĀ  is greater than 10

  • Nested if else

if(condition)

{

if(condition)

{

block of statements;

}

else

{

block of statements;

}

else

{

block of statements;

}

Example

main()

{

int a=1,b=1;

if(a==0)

if(b==0)

printf(”Hi”);

else

printf(”Bye”);

}

Output:

No output..

For explanation or any ques. write in the comment section.




Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • LinkedIn
  • MySpace
  • Reddit
  • RSS
  • Twitter

written by Shweta