UNIT 3 : Flow Control Structures

UNIT 3 : Flow Control Structures

 


Flow control structures in C programming empower you to manage the sequence of actions in your program based on conditions or repetition. The three primary control structures in C are:


1. Conditional (if-else) Statements:

   These are employed to execute a block of code only when a specific condition is satisfied.


   Example:


   ```c

   int x = 10;

   if (x > 5) {

       printf("x is greater than 5");

   }

   ```


2. Loops:

   Used to iteratively execute a block of code until a particular condition is met.


   Example:


   ```c

   for (int i = 0; i < 10; i++) {

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

   }

   ```


3. Switch Statements:

   These allow you to choose one block of code from multiple options based on the value of an expression.


   Example:


   ```c

   char grade = 'B';

   switch (grade) {

       case 'A':

           printf("Excellent!");

           break;

       case 'B':

           printf("Good!");

           break;

       case 'C':

           printf("Average");

           break;

       default:

           printf("Invalid grade");

   }

   ```


Decision Making Structures:


Decision-making structures in C are employed to make choices in a program based on certain conditions. There are two main decision-making structures:


1. if Statement:

   Used to execute a block of code only if a specified condition is true.


   Example:


   ```c

   int x = 10;

   if (x > 5) {

       printf("x is greater than 5");

   }

   ```


2. if-else Statement:

   Allows execution of one block of code if a condition is met and another if the condition is not met.


   Example:


   ```c

   int x = 10;

   if (x > 5) {

       printf("x is greater than 5");

   } else {

       printf("x is not greater than 5");

   }

   ```


Loop Control Structures:


Loop control structures in C are utilized to repetitively execute code until a certain condition is satisfied. The three primary loop control structures are:


1. for Loop:

   Used to execute a block of code a specific number of times.


   Example:


   ```c

   for (int i = 0; i < 10; i++) {

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

   }

   ```


2. while Loop:

   Executes a block of code as long as a specified condition is true.


   Example:


   ```c

   int i = 0;

   while (i < 10) {

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

       i++;

   }

   ```


3. do-while Loop:

   Similar to the while loop but ensures that the code inside the loop is executed at least once.


   Example:


   ```c

   int i = 0;

   do {

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

       i++;

   } while (i < 10);

   ```


Nested Control Structures:


Nested if-else and nested for loop structures enable testing multiple conditions and running inner loops for each iteration of an outer loop.


Example:


```c

int x = 10, y = 20;

if (x > 5) {

    if (y > 15) {

        printf("x is greater than 5 and y is greater than 15");

    } else {

        printf("x is greater than 5 and y is not greater than 15");

    }

} else {

    printf("x is not greater than 5");

}

```


Break, Continue, Goto, Exit:


These statements provide control over the flow of a program.


- break: Exits a loop prematurely.


  Example:


  ```c

  for (int i = 0; i < 10; i++) {

      if (i == 5) {

          break;

      }

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

  }

  ```


- continue:Skips the current iteration of a loop and proceeds to the next.


  Example:


  ```c

  for (int i = 0; i < 10; i++) {

      if (i % 2 == 0) {

          continue;

      }

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

  }

  ```


- goto: Jumps to a labeled statement in the program.


  Example:


  ```c

  for (int i = 0; i < 10; i++) {

      if (i == 5) {

          goto end;

      }

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

  }

  

  end:

      printf("Reached end of program.\n");

  ```


- exit: Immediately exits a program with a status value.


  Example:


  ```c

  for (int i = 0; i < 10; i++) {

      if (i == 5) {

          exit(0);

      }

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

  }

  ```