How to Use Control Statements and Loops in C

Markdown

View as Markdown

Control statements and loops are fundamental programming constructs that determine how a program makes decisions and repeats tasks. They allow developers to execute specific blocks of code based on conditions and automate repetitive operations efficiently.

Learning Objectives

  • Use if, if-else, and nested if statements

  • Implement switch statements

  • Write for, while, and do-while loops

  • Use break and continue

  • Choose the appropriate control structure for different programming scenarios

Decision Making Using if Statement

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

Example

#include <stdio.h>

int main()

{

    int marks = 80;

    if (marks >= 50)

    {

        printf("Pass");

    }

    return 0;

}

Output

Pass

if...else Statement

The if...else statement executes one block when the condition is true and another when it is false.

#include <stdio.h>

int main()

{

    int age = 16;

    if (age >= 18)

    {

        printf("Eligible to vote");

    }

    else

    {

        printf("Not eligible");

    }

    return 0;

}

switch Statement

The switch statement selects one block of code from multiple options.

#include <stdio.h>

int main()

{

    int day = 3;

    switch(day)

    {

        case 1:

            printf("Monday");

            break;

        case 2:

            printf("Tuesday");

            break;

        case 3:

            printf("Wednesday");

            break;

        default:

            printf("Invalid Day");

    }

    return 0;

}

Output

Wednesday

Arithmetic Expressions

Expressions combine variables, constants, and operators to produce a value.

Example:

int total = (20 + 10) * 2;

Understanding operator precedence ensures expressions are evaluated correctly.

Looping in C

Loops allow a block of code to execute repeatedly until a specified condition becomes false.

C provides three primary looping constructs:

  • for

  • while

  • do-while

for Loop

The for loop is commonly used when the number of iterations is known.

#include <stdio.h>

int main()

{

    int i;

    for(i = 1; i <= 5; i++)

    {

        printf("%d\n", i);

    }

    return 0;

}

 

Output

1

2

3

4

5

while Loop

The while loop evaluates the condition before executing the loop body.

#include <stdio.h>

int main()

{

    int i = 1;

    while(i <= 5)

    {

        printf("%d\n", i);

        i++;

    }

    return 0;

}

do...while Loop

The do...while loop executes the loop body at least once before checking the condition.

#include <stdio.h>

int main()

{

    int i = 1;

    do

    {

        printf("%d\n", i);

        i++;

    } while(i <= 5);

    return 0;

}

 

break Statement

The break statement immediately exits the loop.

for(int i=1; i<=10; i++)

{

    if(i==6)

        break;

    printf("%d\n", i);

}

Output

1

2

3

4

5

continue Statement

The continue statement skips the current iteration and moves to the next one.

for(int i=1; i<=5; i++)

{

    if(i==3)

        continue;

    printf("%d\n", i);

}

Output

1

2

4

5

Best Practices

  • Use meaningful conditions.

  • Avoid deeply nested if statements.

  • Use switch when multiple fixed options exist.

  • Choose the appropriate loop based on the problem.

  • Avoid infinite loops.

  • Use break and continue only when they improve readability.

Common Mistakes

  • Missing break statements in switch.

  • Incorrect loop conditions causing infinite loops.

  • Forgetting to increment loop variables.

  • Using assignment (=) instead of comparison (==) in conditions.

 

 

 

 

 

 

 

 

 

 

 

 

Was this article helpful?

Still need help?

Contact us