×
☰ Menu

The if Statement

C uses the keyword if to implement the decision control instruction. The general form of if statement looks like this:

 

Syntax :

if ( this condition is true )
{
               Statement No 1
               Statement No 1
               .
               .
                Statement No N
}
next_statement;

 

The if statement by itself will execute a single statement, or a group of statements when the expression following if evaluates to true. It does nothing when the expression evaluates to false.

Note :

  • More than one condition can be Written inside the If statement.
  • Opening and Closing Braces are required only when “Code” after if a statement occupies multiple lines.
  • Code will be executed if the condition statement is True.
  • Non-Zero Number Inside if  means “TRUE Condition”

 

Note: As a general rule, we express a condition using C’s ‘relational’ operators. The relational operators allow us to compare two values to see whether they are equal to each other, unequal, or one is greater than the other.

void main( )
{
int num ;
printf ( "Enter a number less than 5 " ) ;
scanf ( "%d", &num ) ;
if ( num <= 5 )
printf ( "Correct!" ) ;
getch();
}

 

On execution of this program, if you type a number less than or equal to 5, you get a message on the screen through printf( ). If you type some other number the program doesn’t do anything.