C Programming Language


Control Statements


Following Decisional Making statement or Control Statement used in C Language:

if condition :

An if statement consists of a Boolean expression followed by one or more statements.

Syntax :

if(true)
{
/* statement(s) will execute if the expression is true */
}
Example :

Output :

value of x is : 20

 

if .. else condition :

An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

Syntax :

if(true)
{
/* statement will execute if the condition is true */
}
else
{
/* statement will execute if the condition is false */
}
Example :

Output :

x is not less than 30
value of x is : 200

 

if .. else if condition :

An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.

Syntax :

if(condition)
{
  /* statement will execute if condition is true */
}
else if(condition)
{
 /* Executes when condition 2 is true */
}
else if(condition)
{
 /* Executes when Condition 3 is true */
}
else
{
 /* executes when the not true above all condition */
}
Example :

Output :

None of the above true
Exact value of x is: 200

nested if condition :

Nested if condition helps to put condition inside condition into the block. Through the nested if we filter our output as per our desired conditions

Syntax :

if(condition)
{
     if(condition)
    {
      /* Executes when the condition 2 is true */
     }
}
Example :

Output :

Value of ax is 200 and y : 300
Exact value of x: 200
Exact value of y: 300

switch case condition :

A switch statement helps to choose our desired portion of the condition ,only choosen part will be run at runtime with help of the case.

Syntax :

if(condition)
{
switch(choice_variable)
{
  case value :
         //Statements
        break;//optional
  case value :
        //Statements
        break;//optional
  default://Optional
        //Statements
}
Example :

Output :

Good!