Control Flow: Conditional Statements (if, else, switch)

Using `if`, `else`, and `switch` statements to control the flow of execution based on conditions.


Control Flow: Conditional Statements (if) in C

Understanding Conditional Statements

In programming, control flow refers to the order in which statements in a program are executed. Conditional statements are essential tools for controlling this flow, allowing a program to make decisions and execute different blocks of code based on certain conditions.

The if Statement in C

The if statement is the most basic conditional statement in C. It allows you to execute a block of code only if a specified condition is true. The syntax of the if statement is as follows:

 if (condition) {
            // Code to be executed if the condition is true
        } 

Let's break down the components:

  • if: This keyword signifies the start of the if statement.
  • (condition): The condition is an expression that evaluates to either true (non-zero value) or false (zero value). It is enclosed in parentheses.
  • { ... }: The code enclosed within the curly braces { } is the block of code that will be executed if the condition is true. If the block contains only one statement, the curly braces are optional, but it is highly recommended to always use them for better readability and maintainability.

How the if Statement Works

The if statement works as follows:

  1. The program evaluates the condition inside the parentheses.
  2. If the condition is true (evaluates to a non-zero value), the code block enclosed within the curly braces is executed.
  3. If the condition is false (evaluates to zero), the code block is skipped, and the program continues with the next statement after the if statement.

Example of Using the if Statement

Here's a simple example that demonstrates the use of the if statement in C:

 #include <stdio.h>

        int main() {
            int age = 20;

            if (age >= 18) {
                printf("You are eligible to vote.\n");
            }

            printf("Program continues...\n");

            return 0;
        } 

In this example:

  • The variable age is initialized to 20.
  • The if statement checks if age is greater than or equal to 18. Since 20 >= 18 is true, the code block within the if statement is executed, and "You are eligible to vote." is printed to the console.
  • The program then continues to execute the next statement, which prints "Program continues...".

If we changed age to 15, the condition age >= 18 would be false, and only "Program continues..." would be printed.

Important Considerations

  • Truthiness and Falsiness in C: In C, any non-zero value is considered true, and zero is considered false. This is different from languages like Python or JavaScript, which have explicit boolean types.
  • Comparison Operators: Common comparison operators used in if statements include:
    • == (equal to)
    • != (not equal to)
    • > (greater than)
    • < (less than)
    • >= (greater than or equal to)
    • <= (less than or equal to)

Conclusion

The if statement is a fundamental building block in C programming, enabling you to create programs that can make decisions based on conditions. Mastering the if statement is essential for creating dynamic and responsive applications.