chapter3

Decision Making and Looping Statements in C

Overview

  • In C, programs can dynamically choose which part of the code executes based on specific conditions.

  • This capability is known as decision making, where the statements employed are referred to as conditional statements.

  • Conditional statements evaluate one or more conditions and determine whether to execute a specific block of code based on the outcome (true or false).

Conditional Statements in C

  1. If Statement

    • Simplest form of decision-making in C.

    • Definition: The if statement evaluates a condition and executes the corresponding block of code if the condition is true.

    • Syntax:

     if (condition) {
         // code to execute if condition is true
     }
    
    • Example:

     #include <stdio.h>
     int main() {
         int i = 10;
         if (i < 18) {
             printf("Eligible for vote");
         }
     }
     // Output: Eligible for vote
    
    • The expression within parentheses () is considered the condition and must evaluate to true or false.

    • The block of code within {} is the body of the if statement, which executes only if the condition evaluates to true.

    • If the body contains a single statement, the braces {} may be omitted.

  2. If-else Statement

    • Extends the if statement to include a block of code for when the condition is false.

    • Definition: An if-else statement consists of two blocks—one executed if the condition is true and the other if false.

    • Syntax:

     if (condition) {
         // code if condition is true
     } else {
         // code if condition is false
     }
    
    • Example:

     #include <stdio.h>
     void main() {
         int i = 10;
         if (i > 18) {
             printf("Eligible for vote");
         } else {
             printf("Not Eligible for vote");
         }
     }
     // Output: Not Eligible for vote
    
  3. Nested If Statement

    • If a condition within an if statement is itself an if statement, this is known as a nested if.

    • Definition: A nested if statement allows placing one if statement inside another.

    • Example:

     #include <stdio.h>
     int main() {
         int i = 10;
         if (i == 10) {
             if (i < 18)
                 printf("Still not eligible for vote");
             else
                 printf("Eligible for vote\n");
         } else {
             if (i == 20) {
                 if (i < 22)
                     printf("i is smaller than 22 too\n");
                 else
                     printf("i is greater than 25");
             }
         }
     }
     // Output: Still not eligible for vote
    
  4. If-else-if Ladder

    • This structure is used for multiple conditions.

    • Definition: The if-else-if statement executes the block of code for the first true condition, bypassing the rest; if none are true, the final else block executes.

    • Syntax:

     if (condition1) {
         // code for condition1
     } else if (condition2) {
         // code for condition2
     } else {
         // default code
     }
    
    • Example:

     #include <stdio.h>
     int main() {
         int i = 20;
         if (i == 10)
             printf("Not Eligible");
         else if (i == 15)
             printf("wait for three years");
         else if (i == 20)
             printf("You can vote");
         else
             printf("Not a valid age");
         return 0;
     }
     // Output: You can vote
    
  5. Switch Statement

    • An alternative to if-else-if for executing code based on a variable's value.

    • Definition: The switch statement executes the corresponding code block based on the value of the variable in the switch expression.

    • Syntax:

     switch (variable) {
         case value1:
             // execute if variable == value1
         case value2:
             // execute if variable == value2
         default:
             // executed if no case matches
     }
    
    • Example:

     #include <stdio.h>
     void main() {
         char var;
         int a, b, c;
         printf("Enter the character");
         scanf("%c", &var);
         printf("Enter the first number");
         scanf("%d", &a);
         printf("Enter the second number");
         scanf("%d", &b);
         switch (var) {
             case '+':
                 c = a + b;
                 printf("%d", c);
             default:
                 printf("Default Case is executed");
                 break;
         }
     }
    
    • Note: The switch expression should only evaluate to integers or characters, not other data types.

  6. Conditional Operator (Ternary Operator)

    • A shorthand for if-else that operates on three operands.

    • Definition: The conditional operator ? : evaluates a condition and returns one of two values based on the truth of that condition.

    • Syntax:
      c condition ? expression1 : expression2

    • Example:
      c #include <stdio.h> int main() { int var; int flag = 0; var = flag == 0 ? 25 : -25; printf("Value of var when flag is 0: %d\n", var); return 0; } // Output: Value of var when flag is 0: 25

Jump Statements in C

  • Jump statements control the flow of execution within loops and functions.

    • A) Break Statement

    • Terminates the loop or switch statement.

    • Control returns to the statement following the loop.

    • Example:
      c #include <stdio.h> void main() { int arr[] = {1, 2, 3, 4, 5, 6}; int key = 3; int size = 6; for (int i = 0; i < size; i++) { if (arr[i] == key) { printf("Element found at position: %d", (i + 1)); break; } } } // Output: Element found at position: 3

    • B) Continue Statement

    • Opposite of break, it forces the loop to skip the current iteration and continue to the next.

    • Example:
      c #include <stdio.h> int main() { for (int i = 1; i <= 10; i++) { if (i == 6) continue; else printf("%d ", i); } return 0; } // Output: 1 2 3 4 5 7 8 9 10

    • C) Goto Statement

    • Allows a jump to another point in the program marked by a label.

    • Example:
      c #include <stdio.h> void main() { int n = 1; label: printf("%d ", n); n++; if (n <= 10) goto label; } // Output: 1 2 3 4 5 6 7 8 9 10

Loops in C

  • Allow repeating a block of code until a specific condition is met.

  • Types of loops in C:

    1. For Loop

    • Definition: An entry-controlled loop where the condition is checked before the loop executes.

    • Syntax:
      c for (initialization; condition; update) { // body of the loop }

    • Example:
      c #include <stdio.h> int main() { // Loop to print numbers from 1 to 5 for (int i = 0; i < 5; i++) { printf("%d ", i + 1); } return 0; } // Output: 1 2 3 4 5

    1. While Loop

    • Definition: Also an entry-controlled loop, checking the condition before the loop body runs.

    • Syntax:
      c while (condition) { // Body of the loop }

    • Example:
      c #include <stdio.h> int main() { int i = 0; // Initialization expression while (i <= 5) { // Test expression printf("%d ", i + 1); i++; // update expression } return 0; } // Output: 1 2 3 4 5 6

    1. Do-While Loop

    • Definition: An exit-controlled loop; it checks the condition after executing the loop body at least once.

    • Syntax:
      c do { // Body of the loop } while (condition);

    • Example:
      c #include <stdio.h> void main() { int i = 0; // Initialization expression do { printf("%d ", i); // loop body i++; // Update expression } while (i <= 10); // Condition to check } // Output: 0 1 2 3 4 5 6 7 8 9 10

Nested Loops

  • Nested loops involve placing one loop inside another.

  • Definition: The inner loop runs completely for each iteration of the outer loop.

  • Useful for tasks that require multiple iterations, such as operating on two-dimensional arrays.

  • Example:

  #include <stdio.h>
  void main() {
      for (int i = 0; i < 3; i++) { // Outer loop runs 3 times
          for (int j = 0; j < 2; j++) { // Inner loop runs 2 times for each outer iteration
              printf("i = %d, j = %d\n", i, j);
          }
      }
  }
  // Output: 
  // i = 0, j = 0
  // i = 0, j = 1
  // i = 1, j = 0
  // i = 1, j = 1
  // i = 2, j = 0
  // i = 2, j = 1