conditional control statements

  • Conditional Control Statements involves performing a Logic Test. 
  • This Test results either a TRUE or FALSE.
Depending upon the truthness or falsity of the condition, the statements to be executed is determined. After that, the control transfers to the statement in the program and start executing the statements. This is known as Conditional Execution.

In C four conditional control statements are widely used:
  1. If-statement
  2. If-else statement
  3. Nested-if-else statement.
Let us take all control statements one by one: 
If-Statement:
                    This is used to execute a statement or a collection of statements conditionally or you can say that it is used to execute only one action. It is called one way branching. Here the logical condition is tested which results either TRUE or FALSE. Syntax of if statement is
SYNTAX :
if(condition)
{
statement 1;
statement 2;
statement n;
}

Where
Condition => is a logical expression that results in TRUE or FALSE.
Statement => a simple statement(single statement) or compound statement(collection of two or more statement).
Explanation: If the logical condition is TRUE then statement 1 is executed and If the logical condition is FALSE then control transfers to the next executable statement.
EXAMPLE :
#include<stdio.h>
void main()
{
int num;
printf("Hello user, Enter a number");
scanf("%d",&num); // Collects the number from user
if(num==1)
{
printf("UNITED STATES");
}
if(num==2)
{
printf("SPAIN");
}
if(num==3)
{
printf("INDIA");
}
}

If-Else Statement:
This is used to execute two statements alternatively. It is called a two way branching.The syntax of if-else statement is
SYNTAX :
if(condition)
{
statement 1;
}
else
{
statement 2;
statement n;
}                     
Explanation : If the logical condition is TRUE then statement 1 is executed and If the logical condition is FALSE then control transfers to the next executable statement that is statement 2.
EXAMPLE :
#include<stdio.h>
void main()
{
int num;
printf("Hello user, Enter a number");
scanf("%d",&num); // Collects the number from user
if(num==1)
{
printf(
"UNITED STATES");
}
else if(num==2)
{
printf("
SPAIN");
}
else if(num==3)
{
printf("
INDIA");
}
else
{
printf("
WRONG ENTRY"); // See how else is used to output "WRONG ENTRY"
}
}

Nested If-Else Statement:
It is used if there are more than two alternatives to select.The syntax of nested-if statement is
SYNTAX :
 if(condition 1) 
{
if(condition 2)
{
statement 1;
}
else 
{
statement 2;
}
}
else
{
statement 3;
statement n;
}               
Explanation:
Statement 1 is executed if condition 1 and condition 2 are TRUE. if condition 1 is TRUE and condition 2 is FALSE then statement 2 is executed. if condition 1 is FALSE then control transfer to the else part and statement 3 is executed.
EXAMPLE :
#include <stdio.h> 
int main () 
{
    int a = 100;
   int b = 200;
    if( a == 100 )
     {   
          if( b == 200 )
          {
                 printf("Value of a is 100 and b is 200\n" );
           }
      }
   
   printf("Exact value of a is : %d\n", a );
   printf("Exact value of b is : %d\n", b );
}