Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  Programming and Data Structures  >  C - Flow Control Statements - Programming and Data Structures - Computer Science

C - Flow Control Statements - Programming and Data Structures - Computer Science

Flow control in the C language refers to how a program makes decisions and repeats actions. It allows you to control the order in which instructions are executed, which is important for creating interactive, logical, and dynamic programs.

C - Flow Control Statements

C provides three styles of flow control

  1. Branching
  2. Looping
  3. Jumping

Branching

Branching is so called because the program chooses to follow one branch or another.Branching

if statement

This is the simplest form of the branching statements.

It takes an expression in parentheses and a statement or block of statements. If the expression is true, then the statement or block of statements gets executed; otherwise, these statements are skipped.

NOTE: An expression will be assumed to be true if its evaluated value is non-zero.

if statements take the following form:

if (expression)

  statement;

OR

if (expression)

  {

    Block of statements;

  }

OR

if (expression)

  {

    Block of statements;

  }

else

  {

    Block of statements;

  }

OR

if (expression)

  {

    Block of statements;

  }

else if (expression)

  {

    Block of statements;

  }

else

  {

    Block of statements;

  }

?: Operator

The ?: operator is just like an if ... else statement, except that because it is an operator, you can use it within expressions.

?: is a ternary operator in that it takes three values; this is the only ternary operator C has.

?: Operator

?: takes the following form:

if condition? X: Y

if condition is true ? then X return value : otherwise Y value;

Switch statement

The switch statement is much like a nested if...else statement. It's mostly a matter of preference which you use; a switch statement can be slightly more efficient and easier to read.

Switch statement

Example

switch( expression )

     {

        case constant-expression1:    statements1;

        [case constant-expression2:    statements2;]    

        [case constant-expression3:    statements3;]

        [default : statements4;]

     }

Using break keyword
If a condition is met in switch case then execution continues on into the next case clause also if it is not explicitly specified that the execution should exit the switch statement. This is achieved by using break keyword.
Example

#include <stdio.h>

main()

{

     int  Grade = 'B';

     switch( Grade )

     {

        case 'A' : printf( "Excellent\n" );

                   break;

        case 'B' : printf( "Good\n" );

                   break;

        case 'C' : printf( "OK\n" );

                   break;

        case 'D' : printf( "Mmmmm....\n" );

                   break;

        case 'F' : printf( "You must do better than this\n" );

                   break;

        default  : printf( "What is your grade anyway?\n" );

                   break;

     }

}

What is default condition?

If none of the listed conditions is met then default condition executed.
Example

#include <stdio.h>

main()

{

     int  Grade = 'L';

     switch( Grade )

     {

        case 'A' : printf( "Excellent\n" );

                   break;

        case 'B' : printf( "Good\n" );

                   break;

        case 'C' : printf( "OK\n" );

                   break;

        case 'D' : printf( "Mmmmm....\n" );

                   break;

        case 'F' : printf( "You must do better than this\n" );

                   break;

        default  : printf( "What is your grade anyway?\n" );

                   break;

     }

}

Looping

Loops provide a way to repeat commands and control how many times they are repeated. C provides a number of looping ways.

Looping

while loop

The most basic loop in C is the while loop. A while statement is like a repeating if statement. Like an if statement, if the test condition is true, the statements get executed. The difference is that after the statements have been executed, the test condition is checked again. If it is still true, the statements get executed again. This cycle repeats until the test condition evaluates to false. This loop is used when we know the terminating condition.

The basic syntax of a while loop is as follows:

while ( expression )

{

   Single statement 

   or

   Block of statements;

}

Here the expression is the condition of termination.

for loop

A for loop is similar to a while loop; it's just written differently. For statements are often used to process lists, such as a range of numbers. It is used when we know the number of iterations.

The basic syntax of a for loop is as follows:

for( expression1; expression2; expression3)

{

   Single statement

   or

   Block of statements;

}

In the above syntax:

  • expression1 – Initialises variables.
  • expression2 – Conditional expression: as long as this condition is true, the loop will keep executing.
  • expression3 – expression3 is the modifier which may be a simple increment of a variable.

do...while loop

do ... while is just like a while loop except that the test condition is checked at the end of the loop rather than the start. This has the effect that the content of the loop is always executed at least once.

The basic syntax of a do...while loop is as follows:

do

{

   Single statement

   or

   Block of statements;

}while(expression);

Jump Statements

Jump statements in C are used to alter the normal flow of a program by transferring control to another part of the code. They help in:

  • Exiting loops or switch statements early

  • Skipping part of a loop

  • Jumping to a labeled section (in special cases)

There are 3 types of Jump Statements

1. break
2. continue
3. goto

break and continue statements

C provides two commands to control how we loop:

  • break – exit from loop or switch.
  • continue – skip 1 iteration of loop.

You already have seen an example of using a break statement. Here is an example showing usage of the continue statement.

void main()

{

    int i;

    int j = 10;

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

    {

       if( i == 5 )

       {

          continue;

       }

       printf("Hello %d\n", i );

    }

}

This will produce the following output:

Hello 0

Hello 1

Hello 2

Hello 3

Hello 4

Hello 6

Hello 7

Hello 8

Hello 9

Hello 10

​goto statement

The goto statement in C is used to transfer control to a labelled part of the program. It is a jump statement that can move execution forward or backward in code.

Let us see an example of goto statement in C programming.

int main() {

    printf("Step 1\n");

    goto skip;  // jump to 'skip' label

    printf("Step 2\n");  // this line will be skipped

skip:

    printf("Step 3\n");

    return 0;

}

Output:

Step 1

Step 3

 Note: goto is rarely used in modern programming because it can make code difficult to understand and debug. It’s generally avoided unless absolutely necessary.

Conclusion

Control statements in C are essential for directing the flow of a program. Branching, looping, and jump statements help in making decisions, repeating tasks, and altering the execution path. Mastering these allows programmers to write logical and efficient code.


The document C - Flow Control Statements - Programming and Data Structures - Computer Science is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
161 docs|30 tests

FAQs on C - Flow Control Statements - Programming and Data Structures - Computer Science

1. What are flow control statements in C programming?
Ans. Flow control statements in C programming are constructs that manage the execution flow of the program. They enable the program to make decisions, repeat actions, or jump to different parts of the code based on certain conditions. The primary categories include conditional statements (like if, else, switch), looping statements (like for, while, do-while), and jump statements (like break, continue, return).
2. How do branching statements work in C?
Ans. Branching statements in C allow the program to take different paths based on conditions. The most common branching statements are 'if', 'else if', and 'else'. When a condition evaluates to true, the corresponding block of code executes, allowing for decision-making in the program. The 'switch' statement is another form of branching that handles multiple conditions based on the value of a variable.
3. What is the significance of jump statements in C?
Ans. Jump statements in C are used to transfer control to another part of the program. They include 'break', which exits loops or switch statements; 'continue', which skips the current iteration of a loop and proceeds to the next; and 'goto', which jumps to a labeled statement. These statements can improve code readability and control flow but should be used judiciously to avoid confusion.
4. Can you explain the difference between 'break' and 'continue' statements?
Ans. Yes! The 'break' statement is used to exit a loop or switch statement immediately, stopping further execution of that block of code. In contrast, the 'continue' statement is used within loops to skip the current iteration and proceed to the next one. Essentially, 'break' terminates the loop, while 'continue' just skips to the next cycle of the loop.
5. What role does the 'return' statement play in C functions?
Ans. The 'return' statement in C functions is used to exit a function and optionally return a value to the caller. This is important for functions that need to provide output back to the part of the program that invoked them. When a 'return' statement is executed, control is handed back to the calling function, and the value specified (if any) is passed along, allowing for effective data handling in programs.
Related Searches
practice quizzes, ppt, Semester Notes, video lectures, C - Flow Control Statements - Programming and Data Structures - Computer Science, Previous Year Questions with Solutions, C - Flow Control Statements - Programming and Data Structures - Computer Science, MCQs, shortcuts and tricks, Summary, mock tests for examination, Viva Questions, C - Flow Control Statements - Programming and Data Structures - Computer Science, study material, pdf , Free, Exam, Objective type Questions, Important questions, past year papers, Extra Questions, Sample Paper;