Control Structures


  • Flow of program is a sequence.
  • Why control structures comes in action, because at certain situation you have to take decisions and work according to condition and Flow of program changes.
  • Decision making is process where we see that certain condition is occurred  or not ,and to do work according to the condition.
    • When a program breaks the sequential flow and jumps to an another piece of the program is called Branching
    • Branching is subdivided in Two part, Conditional and Unconditional.
      • Conditional Branching
        • In conditional Branching the condition is checked first then control is pass to another piece of program.
        • example: If, Ternary operator, switch statements.
      • Unconditional Branching
        • In Unconditional Branching condition is not checked and control  is pass to another piece of program.
        • Mainly, this statements are Used to break the flow of program or to pass the control to another part of program.
        • example: break, continue, goto  statements.
    • Looping
      • Looping is the process where some task is performed repeatedly till some condition is met.
      • Example: While ,do-while, and for loops.

If-Else
Statement

if Statement:

  • It is a Two way decision Statement, which depends upon some condition.
  • Condition Evaluates in the form of true or false.
  • If condition Evaluates True than, it  will
    do the work that is written in the block of If statement.
  • If condition evaluates False than it will
    Ignore the block of if statement, and  goto  to the next step.

Syntax:

if(condition)
{
  
//block of  "if statement"
//it will come in action ,when  'condition'  Evaluates to true. 
}

Lets understand it by an Simple program.

#include<stdio.h>
 
void main()
{
int  var1 = 5;
if(var1 > 4)                       // '>' greater than is a relational operator
{ 
//  'condition'  is Evaluated to true. 
// Now it  will execute whatever is written in this block.
 
 printf("%d is greater than 4",var1);

//block of  "if statement"
}


}

Output:

5 is greater than 4

Example:

#include<stdio.h>
 
void main()
{
int  var1 = 5;
int var2 = 3;
if(var1 > 4)                           // '>' greater than is a relational operator
{ 
//  'condition'  is Evaluated to true. 
// Now it  will execute whatever is written in this block.
 
 printf("%d is greater than 4",var1);

//block of  "if statement"
}
// another  'if '  block.
if(var1 == 5)                //'==' is equal to operator.
{                        //which evaluates true if both the operants are same.
printf("var1 is equal to 5");
}
// another 'if' block.
if(var2  <  5)
{
printf("var2 is less than 5");
}
// another 'if' block.
if((var1 > 5)  &&( var2 < 5))        //'&&' is a logical 'AND' operator.
{                                                     //which evaluates true only if both the condition are true.
//var1 > 5 : first condition.
//var2 < 5 :second condition.
//for now both the conditions are true.
printf("var1 is greater than 5 and var2 is less than 5");


}

}

Output:

5 is greater than 4
var1 is equal to 5
var2 is less than 5
var1 is greater than 5 and var2 is less than 5

else
Statement

  • else statement is used with if statement.
  • If condition in if statement evaluates false, than else block comes in action.

Syntax:

if(condition)
{
// this block will execute  if condition evaluates to true.

}

else
{
// this block will execute if above 'if' block condition evaluates to false.

//block of 'else'  statement
}

You will understand this by an example,so lets see an example.

#include<stdio.h>
void main()
{
int var1 = 3;

if(var1 < 4)
{       //this condition will evaluate to false,because 3 is less than 4.
        printf("var1 is greater than 4"); 
         //so,this piece of code will not execute.
        // and control will go to the else block.
}
else
{
// As above condition evaluated to false.
//control has come to this block, and it will execute this piece of code.
printf("var1 is less than 4");
}
}

Output:

var1 is less than 4

Ternary Operator

  • This operator works like If-else statements.
  • If your blocks of if-else statement has only one line of code than
    for that situation this operator is best.
  • We can write the above same code in single line.

Syntax:

#include<stdio.h>var
 void main()
{
int var1 = 5;
var1 > 4 ? printf("var1 is greater than  4") : printf("var1 is less than 5");

// ':' is barrier between the two possibilities of output
// if 'var>4' evaluates to true ,it will print 'var1 is greater than 4'
// if 'var>4' evaluates to false ,it will print 'var1 is less  than 4'

}

Output:
var1 is greater than 4

#include<stdio.h>var
 void main()
{
int var1 = 5;
var1 < 4 ? printf("var1 is greater than  4") : printf("var1 is less than 5");

// ':' is barrier between the two possibilities of output
// if 'var>4' evaluates to true ,it will print 'var1 is greater than 4'
// if 'var>4' evaluates to false ,it will print 'var1 is less  than 4'

}

Output:
var1 is less than 5








Further Concepts







  Practice Programs




  • Programs Regarding If-else Statements
  • Program Regarding nested If - statements
  • Programs Regarding loops




Further Topics



People Also Searched: