×
>
<

Aptitude

Control Structures

Introduction to control statements

 

In C language we have to perform a lot of operations and to do them we have a bunch of control statements that help us to perform specific tasks .

In C language you can decide which statements you want to execute in your program and which statements you want to skip. This is called decision making. Most decisions are done on a condition basis.

When a particular condition arrives, you can execute the statements. Because these statements work with the conditions, they are also called conditional statements. And because they control execution in the statement program, they are also called control statements.

Control statement in C

C program is the group of two or more statements, which is sequentially executed. But sometimes programmer may need to repeat any statements many times or change the sequence of program.

If statement
  • An if statement is used to make a comparison between two or more set of values, and if that condition or comparison is satisfied then the part of the code written in the if statement will get executed .
  • if statement block’s is defined by curly braces{}. When the condition is true then the statement given in this block is executed.
  • If the condition is false then skip the entire block.
Syntax of if statement
  
if(condition)
{
      //statements to be executed if condition is true.
}
  
  
#include
void main()
{
     int a=8;
     int b=5;        //checks if a is greater than b
     printf("a is greater than b");
}
  
output
  
a is greater than b
  
if else statement

In some cases when if condition is not satisfied an if else statement will be the next statement that the compiler will now go to and execute if conditions matched.

if else statement is considered to be part of an if statement. But else block is added in it. The statement given in Else block is executed when the condition of if is false.

Syntax of if else statement
  
// Check whether an integer is odd or even

#include "stdio.h"
int main() {
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);

    // True if the remainder is 0
    if  (number%2 == 0) {
        printf("%d is an even integer.",number);
    }
    else {
        printf("%d is an odd integer.",number);
    }

    return 0;
}
  
output
  
Enter an integer: 7
7 is an odd integer.
  
else if statement

if you want to put another condition in the middle of if and else, then you can do this by defining else if block.

else-if statement
  
if(condition)
{     
      //statement to be executed if condition is true
}
else if(condition)
{  
    //statement to be executed if else if condition is true
}
else(condition)
{
    // statement to be executed if, neither if nor else if is true.
}
  

Example :

  
#include 
void main()
{
   int a=5;
   int b=5;
   if(a>b)      //checks if a is greater than b
   {
       printf("a is greater than b");
   }
   else if(a < b)   //checks if a is less than b
   {
       printf("a is less than b");
   }
   else
   {
       printf("a is equal to b");
   }
}
  
output
  
a is equal to b
  

There are a lot of other comparisons we can do in an if statement like

  
if(a==b)   //if a is equal to b
if(a!=b)  //if a is not equal to b
if(a==b && a>5)   //if a is equal to b and a is greater than 5
if(a==b || a>5)  //if a is equal to b or a is greater than 5
  
Switch cases ?

In a switch case we provide (or take input from the user) a key and give it to the switch and the switch compares that value with its cases and if it is equal it will execute that case and if no cases match then it runs a default case.

Syntax of Switch case statement
  
switch(value)
{    
   case (condition):
       //statement to be executed if condition is true
       break;
   case (condition):
      //statement to be executed if condition is true
      break;
   default:
     //statement to be executed if none of the case is true
}
  

Example :

  
#include
int main()
{
   int a=3;
   switch(a) //value of 3 is given to switch
   {
       case 1 : //checks if a is equal to 1
         printf("One");
         break; //jumps out of all switch cases
       case 2 : //checks if a is equal to 2
          printf("Two");
          break; //jumps out of all switch cases
       default :
          printf("No values matched");
   }
}
  
output
  
No values matched