Control Statement

Control statements in C programming are fundamental constructs that manage the flow of execution in a program. They allow the program to make decisions, execute certain code blocks conditionally, and repeat actions. Here’s an overview of the primary types of control statements in C:


    Conditional Statements:

    • if statement: Executes a block of code if a specified condition is true.
      if (condition) {
          // code to execute if condition is true
      }
    • if-else statement: Executes one block of code if the condition is true, and another block if it is false.
      if (condition) {
          // code to execute if condition is true
      } else {
          // code to execute if condition is false
      }
    • else-if ladder: Used to test multiple conditions in sequence.
      if (condition1) {
          // code to execute if condition1 is true
      } else if (condition2) {
          // code to execute if condition2 is true
      } else {
          // code to execute if all conditions are false
      }

    Switch Statement:
    A switch statement tests a variable against a list of values (cases) and executes the corresponding block of code.
    switch (expression) {
        case value1:
            // code to execute if expression equals value1
            break;
        case value2:
            // code to execute if expression equals value2
            break;
        // more cases...
        default:
            // code to execute if expression does not match any case
    }

        Looping Statements:

        for loop: Repeats a block of code a specified number of times. 

        for (initialization; condition; increment) {
            // code to execute for each iteration
        }

        while loop: Repeats a block of code as long as a specified condition is true.

        while (condition) {
            // code to execute as long as condition is true
        }

        do-while loop: Similar to the while loop, but the block of code is executed at least once before the condition is tested.

        do {
            // code to execute
        } while (condition);


        0 Comments had been done yet:

        Post a Comment