Nano Technology

Control Statements in C


1. What are Control Statements?

Control statements in C are used to control the flow of execution of a program. By default, a C program executes statements sequentially, one after another. Control statements allow the program to make decisions, repeat operations, or jump to different parts of the code.

They play a crucial role in implementing logic and decision-making in C programs.

Control Statements in C

2. Decision Making Statements

Decision-making statements allow a program to execute different blocks of code based on whether a given condition is true or false.

a) if Statement

The if statement executes a block of code only when a specified condition is true.


if (condition)
{
    // statements
}

If the condition evaluates to false, the statements inside the if block are skipped.

b) if–else Statement

The if–else statement provides an alternative block of code that executes when the condition is false.


if (condition)
{
    // statements if true
}
else
{
    // statements if false
}

This structure ensures that exactly one block of code is executed.

c) Nested if Statement

A nested if statement is an if statement placed inside another if or else block. It is used when multiple conditions need to be checked.


if (condition1)
{
    if (condition2)
    {
        // statements
    }
}

Nested if statements should be used carefully to maintain program readability.

3. switch Statement

The switch statement is used to select one block of code from multiple alternatives based on the value of an expression.


switch (expression)
{
    case value1:
        // statements
        break;
    case value2:
        // statements
        break;
    default:
        // statements
}

The break statement prevents fall-through to the next case. The default case executes if no matching case is found.

4. goto Statement

The goto statement transfers control unconditionally to a labeled statement in the program.


goto label;
/* statements */
label:
    // statements

Although supported by C, the use of goto is discouraged because it makes programs difficult to understand and debug.

5. Examples


#include <stdio.h>

int main()
{
    int num = 5;

    if (num > 0)
        printf("Positive number\n");
    else
        printf("Negative number\n");

    switch (num)
    {
        case 1:
            printf("One\n");
            break;
        case 5:
            printf("Five\n");
            break;
        default:
            printf("Other number\n");
    }

    return 0;
}

This program demonstrates the use of if–else and switch statements in C.

6. Comparison Table

Statement Purpose Usage
if Tests a single condition Simple decision making
if–else Provides two alternatives Binary decisions
nested if Checks multiple conditions Complex decisions
switch Selects from multiple options Menu-driven programs
goto Unconditional jump Avoid when possible

7. Summary

  • Control statements change the normal flow of program execution.
  • Decision-making statements execute code based on conditions.
  • switch is useful for multiple fixed choices.
  • goto should be avoided in structured programming.
Note: Mastery of control statements is essential before learning loops, arrays, and advanced program logic.