C programming tutorial

                 PREVIOUS                                                                                   NEXT CHAPTER

IF STATEMENT C

 

By default the instructions in a program are executed sequencially.But Many times in real world c programming It is often required that certain block of statements executes only if certain condition are met or evaluates to be true.There come the role of if statement.

 

If statement syntax in C

if(condition)

{
//statements;
}
if the condition evaluates to true,the if statement block is executed otherwise(if condition evaluates to false) statements inside if are ignored and the whole block gets skipped.

There is no need of putting braces if number of statements inside if is one.


example

int i=2;
if(i<3)
{
printf("c programming");
}

In above example.
condition is checked ,since i=2 hence less then 3,statements inside if are executed.
hence "c programming" will be printed on screen.

 

Note:In C a non zero value is considered to be true and a zero is considered as false.

 

On the basis of above note guess the output of following program

main()

{

if (6/5)

printf(" Where ");

if(a=10)

printf(" my ");

if(-2)

printf("wife");

if(a)

printf("is");

if(5/6)

printf("huh");

}

 


                 PREVIOUS                                                                                   NEXT CHAPTER