Jul 12

Syntax:

if (condition)

execute the instructions;

  • If the condition is true (non-zero),then instructions will be  executed otherwise those instructions will be skipped and program continues on the next instruction.

Example

main()
{

int num;

printf(”Enter a number”);

scanf(”%d”,num);

if(num<10)

printf(”number entered is less than 10);

}

on execution of this program,if you type a number less than 10,you get the message on the screen through printf().

if you type some other number ,the program doesn’t do anything.

  • If we want more than one statement to be executed,then we can specify a block of statements within curly braces { }.

Syntax:

if (condition)

{

block of statements;

}

  • We can even use arithmetic expressions in if statement.

Example

if (3+2 % 5)

printf(” Hello “);

Output:     Hello

Since expression( 3 +2 % 5)  evaluates to 5,which is non-zero or true,hence the output.

  • In a C ,a non zero value is considered to be true,whereas zero is consedered to be false.
  • If semicolon is used in if statement,then compiler treat if as blank statement and proceed through next statements.

Example

int x=10,y=100%90;

if (x!= y);

printf(”x =”%d” , y = “%d”,x,y);

Output:

x=10,y=10


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

written by Shweta