Social Icons

10 July 2012

0 If statement in C

Like most languages C uses the keyword ‘if’ to implement the decision control instruction. The general form of the ‘if’ statement is given below-

-------------
-------------
if (condition)
  {
    statement1;
    statement2;
    -----------
    -----------
    -----------
  }
 statement9;
-------------
-------------

When control reaches to the if statement , condition within if is evaluated.If condition evaluates true control goes inside the body of if and statements within the body of if are executed.If condition evaluates false, compiler executes the statement following the body of if statement, statement9 in this case.
If body of if has single statement instead of multiple statements ,then braces are not compulsory.But there is no restriction even though if you have single statement within the body of if ,you can still use braces.

the keyword if tells the compiler to follow the decision control instruction. Any condition which follow the if keyword, always enclosed within pair of parentheses. We express a condition using C’s relational operators which allow us to compare two values to see whether they are equal, unequal or one is greater than other.

Relational Operators and their meaning-

x == y  read as x is equal to y
x != y read as x is not equal to y
x < y read as x is less than y
x > y read as x is greater than y
x <= y read as x is less than or equal to y
x >= y read as x is greater than or equal to y

Example- 
/* Demonstration of if statement */
#include<stdio.h>
int main( )
{
int num ;
printf ( "Enter a number less than 10 " ) ;
scanf ( "%d", &num ) ;
if ( num > 10 )
 {
   printf ( "What an obedient servant you are !" ) ;
 }
return 0;
}

Explanation- when we execute the above program then if you type the number greater than 10 then you will get a message on screen through printf(). If you type the number which is greater than 10 then program will do nothing.

0 comments:

Post a Comment

Hey this is a test comment

Related Posts Plugin for WordPress, Blogger...