C Statements

In C programming, statements are the instructions that tell the compiler what to do. They form the building blocks of any C program and are executed sequentially unless control flow statements alter the execution order.

Key Topics

1. Expression Statements

Expression statements perform computations and assignments. They end with a semicolon.

Example: Assignment and Function Call Statements

#include <stdio.h>

int main() {
    int a;
    a = 5;
    printf("Value of a: %d\n", a);
    return 0;
}

Output:

Value of a: 5
                    

Code Explanation: The variable a is declared, assigned the value 5, and then printed using printf(). Each line ending with a semicolon represents an expression statement.

2. Compound Statements

Compound statements, also known as blocks, group multiple statements together using braces { }. They define a new scope for variables.

Example: Using Compound Statements

#include <stdio.h>

int main() {
    {
        int x = 10;
        int y = 20;
        int sum = x + y;
        printf("Sum: %d\n", sum);
    }
    // x, y, sum are not accessible here
    return 0;
}

Output:

Sum: 30
                    

Code Explanation: Variables x, y, and sum are declared inside a compound statement and are local to that block. They cannot be accessed outside of it.

3. Control Flow Statements

Control flow statements alter the execution flow of a program. They include conditional statements and loops.

Example: If-Else Statement

#include <stdio.h>

int main() {
    int num = -5;

    if (num > 0) {
        printf("Positive number\n");
    } else {
        printf("Non-positive number\n");
    }
    return 0;
}

Output:

Non-positive number
                    

Code Explanation: The if statement checks whether num is greater than zero. Since num is -5, the condition is false, and the else block executes.

Example: For Loop Statement

#include <stdio.h>

int main() {
    for (int i = 1; i <= 5; i++) {
        printf("Iteration %d\n", i);
    }
    return 0;
}

Output:

Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
                    

Code Explanation: The for loop runs five times, incrementing i each time, and prints the current iteration number.

Best Practices

  • Always end statements with a semicolon.
  • Use proper indentation and spacing for readability.
  • Group related statements using braces, even if they are single-line, to prevent errors during future modifications.
  • Comment your code to explain the logic where necessary.

Don'ts

  • Don't omit the semicolon at the end of statements; it will cause a compilation error.
  • Don't write code without proper indentation; it makes the code hard to read and maintain.
  • Don't neglect to use braces in control structures; it can lead to logical errors when adding more statements later.

Key Takeaways

  • Statements are the fundamental units of execution in a C program.
  • Expression statements perform operations and end with a semicolon.
  • Compound statements group multiple statements into a block and define a new scope.
  • Control flow statements like loops and conditionals alter the normal sequential execution of statements.