C Control Statements


Every C program is executed sequentially from top to bottom. But sometimes, We may want to use a condition for executing only one part of program and if condition fails the other part of the program will be executed or sometime we may need to jump from one section to another section of the program. All this is done in C language with the help of Control Statements.

Control Statements

Control Statements enable us to specify or change the order in which various instructions in the program are to be executed. We can also say that control statements can change the flow of the program. Control Statements define how the control is transferred to other parts of the program.
C language supports three types of control statements :-
  1. Decision Making Structures
  2. Loop Control Structures
  3. Case Control Structures
Let's understand each of them in detail :

1. Decision Making Structures

Decision making structures are used to test a condition. If the condition is true then a certain action is defined and it is followed; and if the condition is false then another action is defined and it is then followed. We can also say that decision making structures are used to make a decision on a given condition.
Decision Making Structures basically contains 3 Structures :

(i) if Statement
(ii) if - else Statement
(iii) switch Statement

(i) if Statement

If statement is used to test one condition. If the condition is true then the if-block statement will be executed. If the condition is not true or false then the if-block statement will not be executed and control will be transferred to to the next statement.

Syntax of if() statement :
if ( condition )
      if-block statement;
For Example -
int age = 20;

if ( age>18 )
       printf("You are an adult\n");
       /* The above line is "if-block statement" will be printed as condition is true */

printf("This is the end of the program");
In the above if statement, the condition is true, therefore "if-block statement" will be printed on the output.

Let's take another example -
int age = 12;

if ( age>18 )
       printf("You are an adult\n");
       /* The above line is "if-block statement" will not be printed as condition is false */

printf("This is the end of the program");
/* Control is directly transferred to this line */
In the above if statement, the condition is false, therefore "if-block statement" will be not be printed on the output.

Program Example -
int age = 20;
    
    if ( age>18 )
       printf("You are an adult\n");
       /* The above line is "if-block statement" will be printed as condition is true */

    printf("This is the end of the program");

    age = 12;

    if (age>18)
       printf("You are an adult\n");
       /* The above line is "if-block statement" will not be printed as condition is false */

    printf("\nThis is the end of the program");
    /* Control is directly transferred to next line */

if condition flowchart
Flowchart of if statement

Note : Only one statement is included in if-block code. If you want to include more than one statement, then use "curly braces" around these statements.
For Example -
if ( condition==true ) // if condition is true
{
/* All the statements inside curly braces are followed. */
}
Note : It is always a good practice to include curly braces around the if-block statements.

(ii) if-else statement

if-else statement is used to test 2 statements. if-else block specifies two conditions : one to be followed when condition is true and other to be followed when condition is false.
It has the following syntax :
if (condition) {
     /* If condition is true, this statement is followed */
        }

else {
     /* If condition is false, this statement is followed */
        }
It is to be remembered, No condition is to be specified with else statement. else statement is followed, when if condition is false.

Program example
int main(){
    int age;
    printf("Enter Age : ");
    scanf("%d",&age);
    
    if (age>18)
    printf("\nYou are an adult.");
    else
    printf("\nYou are not an adult. ");
}
Click on Run and Enter the value in stdint section



if-else flowchart
Flowchart of if-else statement

There is also a modification to if- else statement. With this modification, you can also test more than 2 conditions. elseif statement is used for this purpose.

The syntax of if-else if-else is given below :
if (condition) {
        //first statement
        }
else if (condition) {
        //second statement
        }
else if (condition) {
        //third statement
        }
else if (condition) {
        //fourth statement
        }
else {
        //last statement
        }
Let's understand how the above syntax will work

If the first if (condition) is true then the first statement will be executed. And the control will be transferred to the outside of if block.

If the first if (condition) is false then the next else if (condition) will be checked and if it is true then second statement will be executed.

Similarly if the first else if (condition) is not true then the next else if (condition) will be checked and if it is true then third statement will be executed and so on till the last else statement.

Program Example -
int main(){
    int percentage;
    
    printf("Enter the percentage of the student : ");
    scanf("%d",&percentage);
    
    if (percentage<33)
    printf("\nYou have Failed");
    else if(percentage>=33&&percentage<=50)
    printf("\nYour Grade is D");
    else if(percentage>=51&&percentage<=65)
    printf("\nYour Grade is C");
    else if(percentage>=66&&percentage<=85)
    printf("\nYour Grade is B");
    else 
    printf("\nYour Grade is A");
    
}
Click on Run and Enter the value in input section


Nested if Statements


An if statement can include another if statement to form a nested statement. Nesting an if allows a decision to be based on two or more conditions.

Consider the following example:
int age=25;
char gender='F';

if (age > 18) {
/* This condition is true */

     if (gender=='M'||gender=='m') {
     /* This condition is false */
             printf("\nYou are an adult male");
             /* The above line will not be printed */

     else {
     /* This condition is true */
             printf("\nYou are an adult female");
}

else {
   printf("You are not an adult");
}

/*
         OUTPUT:
            You are an adult female
 */


(iii) switch Statement

Switch statement is a multi-decision statement. It is used for testing many conditions and follow any one condition. In Switch Statements, conditions are called 'cases'. 

The syntax of switch statement is given below :
switch(variable-expression) {
        case constant-value1 :
                 statement of first case
                 break;
        case constant-value2 :
                 statement of second case
                 break;
        case constant-value3 :
                 statement of third case
                 break;
        case constant-value-n :
                 statement of nth case
                 break;
        default :
                 statement of nth case
switchcasebreak, are the keywords. case means condition and break keyword is used to transfer the control of the compiler out of the switch statement. variable-expression is the variable on which condition is being tested.
When a case is matched, then it's statement and break statement will be followed.
Note : It is must to include break statement in every case statement. If break is not included, then all the statements after the first matched statements will be automatically executed.

Let's understand switch statement with an example :
int num;

printf("Enter the Number(in figures) : ");
scanf("%d",&num);

switch(num) {
/* Switch statement starts, condition is being tested on 'num' variable */
case 1 :
/* When "num" is equals to 1, then following statement will be executed */
printf("\nFirst");
break;

case 2 :
/* When "num" is equals to 2, then following statement will be executed */
printf("\nSecond");
break;

case 3 :
/* When "num" is equals to 3, then following statement will be executed */
printf("\nThird");
break;

default :
/* When no case is matched, then following statement will be executed */
printf("\nEnter the correct Number");

}
Program Example -

Click on Run and Enter the value in input section



<<Previous

Post a Comment