Ticker

6/recent/ticker-posts

Control Statements in C Language

Control Statement

Generally C program statement is executed in a order in which they appear in the program. But sometimes we use decision making condition for execution only a part of program, that is called control statement. Control statement defined how the control is transferred from one part to the other part of the program. There are several control statement like if...else, switch, while, do....while, for loop, break, continue, goto etc.

Conditional Statement are 4 types:

  1. if statement
  2. if…..else ... Statement
  3. Nesting of if …else
  4. If….else LADDER

If Statement 

Statement execute set of command like when condition is true and its syntax is 

    If (condition) 

    Statement; 

The statement is executed only when condition is true. If the if statement body is consists of several statement then better to use pair of curly braces. Here in case condition is false then compiler skip the line within the if block. 

void main() { 

     int n; 

     printf (“ enter a number:”); 

     scanf(“%d”,&n); 

     If (n>10) 

     Printf(“ number is grater”); 

 } 

Output

Enter a number:12 

Number is greater

if…..else ... Statement 

it is bidirectional conditional control statement that contains one condition & two possible action. Condition may be true or false, where non-zero value regarded as true & zero value regarded as false. If condition are satisfy true, then a single or block of statement executed otherwise another single or block of statement is executed. 

Its syntax is:- 

     if (condition) { 
        Statement1; 
        Statement2; 
    } else 
     { 
         Statement1; 
         Statement2; 
     } 

Else statement cannot be used without if or no multiple else statement are allowed within one if statement. It means there must be a if statement with in an else statement. 

Example:- 

 /* To check a number is eve or odd */

void main() 

     int n; 
     printf (“enter a number:”); 
     sacnf (“%d”, &n); 
     If (n%2==0) 
         printf (“even number”); 
    else 
         printf(“odd number”); 

Output: 

    enter a number:121 

    odd number

Nesting of if …else 

When there are another if else statement in if-block or else-block, then it is called nesting of if-else statement. 

Syntax is :- 

 if (condition) 
     { 
        If (condition) 
             Statement1; 
        else 
             Statement2; 
     
 Statement3;

If….else LADDER 

In this type of nesting there is an if else statement in every else part except the last part. If condition is false control pass to block where condition is again checked with its if statement. 

Syntax is :- 

if (condition) 
     Statement1; 
 else if (condition) 
     statement2; 
 else if (condition) 
     statement3; 
 else 
     statement4; 

This process continue until there is no if statement in the last block. if one of the condition is satisfy the condition other nested “else if” would not executed.

But it has disadvantage over if else statement that, in if else statement whenever the condition is true, other condition are not checked. While in this case, all condition are checked. 

Loops in C

Loop:-it is a block of statement that performs set of instructions. In loops Repeating particular portion of the program either a specified number of time or until a particular no of condition is being satisfied. 

There are three types of loops in C 

  1. While loop 
  2. do while loop 
  3. for loop 

 While loop 

Syntax:- 

while(condition) 

    Statement 1; 

    Statement 2; 

Or 

while(test condition) 

    Statement;

The test condition may be any expression .when we want to do something a fixed no of times but not known about the number of iteration, in a program then while loop is used. 

Here first condition is checked if, it is true body of the loop is executed else, If condition is false control will be come out of loop.

Example:- 

/* wap to print 5 times welcome to C” */ 

#include 

void main() { 

    int p=1; 

    While(p<=5) 

    

        printf(“Welcome to C\n”); 

        P=p+1; 

    

}

Output: Welcome to C 

             Welcome to C 

             Welcome to C 

             Welcome to C 

             Welcome to C

So as long as condition remains true statements within the body of while loop will get executed repeatedly.

do while loop 

This (do while loop) statement is also used for looping. The body of this loop may contain single statement or block of statement. The syntax for writing this statement is:

Syntax:- 

Do 

    Statement; 

} while(condition); 

Example:- 

#include <stdio.h>

void main() { 

    int X=4; 

    do { 

         Printf(“%d”,X); 

        X=X+1;

     }while(X<=10); 

     Printf(“ ”); 

Output: 4 5 6 7 8 9 10

Here firstly statement inside body is executed then condition is checked. If the condition is true again body of loop is executed and this process continue until the condition becomes false. Unlike while loop semicolon is placed at the end of while. 

There is minor difference between while and do while loop, while loop test the condition before executing any of the statement of loop. Whereas do while loop test condition after having executed the statement at least one within the loop. 

If initial condition is false while loop would not executed it’s statement on other hand do while loop executed it’s statement at least once even If condition fails for first time. It means do while loop always executes at least once. Notes: 

Do while loop used rarely when we want to execute a loop at least once. 

for loop 

In a program, for loop is generally used when number of iteration are known in advance. The body of the loop can be single statement or multiple statements. Its syntax for writing is: 

Syntax:-

for(exp1;exp2;exp3) 

    Statement; 

}

Or 

for(initialized counter; test counter; update counter) 

    Statement; 

}

Here exp1 is an initialization expression, exp2 is test expression or condition and exp3 is an update expression. Expression 1 is executed only once when loop started and used to initialize the loop variables. Condition expression generally uses relational and logical operators. And updation part executed only when after body of the loop is executed. 

Example:- 

void main() { 

    int i; 

    for(i=1;i<10;i++) 

    

        Printf(“ %d ”, i); 

     } 

Output:-1 2 3 4 5 6 7 8 9 

Nesting of loop 

When a loop written inside the body of another loop then, it is known as nesting of loop. Any type of loop can be nested in any type such as while, do while, for. For example nesting of for loop can be represented as : 

void main() 

    int i,j; 

    for(i=0;i<2;i++) 

         for(j=0;j<5; j++) 

            printf(“%d %d”, i, j); 

 } 

Output: i=0 

               j=0 1 2 3 4 

              i=1 

              j=0 1 2 3 4

Break statement(break) 

Sometimes it becomes necessary to come out of the loop even before loop condition becomes false then break statement is used. Break statement is used inside loop and switch statements. It cause immediate exit from that loop in which it appears and it is generally written with condition. It is written with the keyword as break. When break statement is encountered loop is terminated and control is transferred to the statement, immediately after loop or situation where we want to jump out of the loop instantly without waiting to get back to conditional state. 

When break is encountered inside any loop, control automatically passes to the first statement after the loop. This break statement is usually associated with if statement

Example : 

void main() { 

    int j=0; 

    for(;j<6;j++) 

        if(j==4) 

        break; 

Output: 0 1 2 3 

Continue statement (key word continue)

Continue statement is used for continuing next iteration of loop after skipping some statement of loop. When it encountered control automatically passes through the beginning of the loop. It is usually associated with the if statement. It is useful when we want to continue the program without executing any part of the program. 

The difference between break and continue is, when the break encountered loop is terminated and it transfer to the next statement and when continue is encounter control come back to the beginning position. 

In while and do while loop after continue statement control transfer to the test condition and then loop continue where as in, for loop after continue control transferred to the updating expression and condition is tested. 

Example:- 

void main() { 

        int n; 

        for(n=2; n<=9; n++) 

        

            if(n==4) 

                continue; 

           printf(“%d”, n); 

        

    Printf(“out of loop”); 

Output: 2 3 5 6 7 8 9 out of loop 

ALSO SEARCH:

"explain various control statements in c programming language"

"flow control statements in c programming language"

"what are the control statement in c"

"control statements in programming language"

"what is control statement in c programming"

"types of control statements in c language"