PPL UNIT 3 2024-25

Control Structures and Loops in C

Overview of Control Structures

  • Control structures are essential in programming as they guide the flow of execution based on conditions.

  • The primary control structures in C include:

    • Selection Statements: Determine the flow based on conditions (e.g., if, switch).

    • Iterative Statements: Execute blocks of code multiple times (e.g., for, while, do while).

    • Unconditional Branching: Jumps to specific parts of code (e.g., break, continue, goto).

    • Guarded Commands: Conditional statements that guard the execution of their attached commands.

Selection Statements

  • Definition: A method for executing different parts of code based on the evaluation of conditions.

  • Types of Selection Statements:

    • If Statement: Executes a block if a condition is true.

      • Syntax: if (condition) { // statements }

    • If-Else Statement: Executes one block if the condition is true, or another if false.

    • If-Else-If Ladder: Allows checking multiple conditions in a sequential manner.

    • Switch Statement: Selects a block to execute based on the value of an expression.

  • Example of If-Else Statement:

    • "If x > y, execute block A; else, execute block B."

Nested Conditional Statements

  • Nested if: An if statement within another if statement, useful for checking multiple conditions.

    • Example: Checking if a number is valid while also checking if it meets certain criteria.

Iterative Statements (Loops)

  • Purpose: To repeat a statement or block of statements until a specified condition is met.

  • Types of Loops:

    • While Loop: Checks the condition before executing the block; entry-controlled.

      • Syntax:

        while (condition) {
            // statements
        }
    • Do-While Loop: Executes the block first and then checks the condition; exit-controlled.

      • Syntax:

        do {
            // statements
        } while (condition);
    • For Loop: Designed for a known number of iterations, initializes a counter, checks a condition, and updates the counter in a single line.

      • Syntax:

        for (initialization; condition; increment) {
            // statements
        }

Control Flow Modifications

  • Break: Exits from loops or switch cases prematurely.

  • Continue: Skips the current iteration of a loop and jumps to the next iteration.

  • Goto: Provides a way to jump to different parts in the code using labels.

Examples of Control Structures in C

  • If Statement Example:

    if (i > 15) {
        printf("%d is greater than 15", i);
    }
  • If-Else Example:

    if (i < 15) {
        printf("%d is smaller than 15", i);
    } else {
        printf("%d is greater than 15", i);
    }
  • Switch Statement Example:

    switch (var) {
        case 1:
            printf("Case 1 is executed");
            break;
        case 2:
            printf("Case 2 is executed");
            break;
        default:
            printf("Default Case is executed");
            break;
    }
  • While Loop Example:

    int i = 1;
    while (i <= 10) {
        printf("%d", i);
        i++;
    }
  • For Loop Example:

    for (int j = 0; j < 10; j++) {
        printf("%d", j);
    }

Conclusion

  • Control structures are foundational in C programming, allowing for decision making and repetition. Understanding how to effectively use if, switch, and looping constructs such as for, while, and do-while is crucial for creating efficient programs. By mastering these concepts, programmers can control the flow of execution in a logical and structured manner.