Control Flow

Now we're stepping into the control flow of C, which is how we make decisions in our code and control the order in which statements execute. Control flow gives your program the ability to branch, loop, and handle decisions based on conditions.

Let’s break it down into two major categories: decision-making and loops.


1. Decision-Making:

In C, decision-making statements allow you to execute a block of code only if a certain condition is true. The main decision-making structures are:

  • if statement

  • else statement

  • else if ladder

  • switch statement

a. if statement

The if statement is used to check a condition. If the condition is true, the block of code inside the if statement is executed.

Syntax:

if (condition) {
    // code to execute if the condition is true
}

Example:

int age = 20;
if (age >= 18) {
    printf("You are an adult.\n");
}
  • The condition (age >= 18) is true, so "You are an adult." will be printed.

b. else statement

The else block executes when the if condition is false.

Syntax:

Example:

  • Since age is 16, the else block will execute, printing "You are a minor."

c. else if ladder

If you need multiple conditions to check, you can use else if.

Syntax:

Example:

  • Here, marks is 85, so it prints "Grade: B".

d. switch statement

When you have multiple values to compare a single variable against, a switch statement can be more efficient than using multiple if-else statements.

Syntax:

Example:

  • Since day is 2, it prints "Tuesday".


2. Loops:

Loops allow you to execute a block of code repeatedly until a condition is met. C provides three types of loops:

  • while loop

  • do-while loop

  • for loop

a. while loop

The while loop repeats a block of code as long as the condition is true.

Syntax:

Example:

  • The condition i <= 5 is checked each time, and the loop stops once i exceeds 5.

b. do-while loop

The do-while loop is similar to while, but the code block is executed at least once, even if the condition is false, because the condition is checked after the loop body.

Syntax:

Example:

  • Here, i starts as 6, so the block runs once, but after that, the condition i <= 5 is false, so the loop stops.

c. for loop

The for loop is used when you know exactly how many times you want to loop.

Syntax:

Example:

  • The loop runs as long as the condition i <= 5 is true. After each iteration, i is incremented by 1.


3. Breaking Out of Loops

You can control the flow of loops using:

  • break: Immediately exits the loop.

  • continue: Skips the current iteration and moves to the next one.

a. break Example:

b. continue Example:


Control Flow Summary

Statement
Purpose

if-else

Conditional branching based on true/false

switch

Multi-way branching based on variable value

while

Loop as long as the condition is true

do-while

Loop at least once, then repeat if true

for

Loop a fixed number of times

break

Exit the loop entirely

continue

Skip the current iteration of the loop


Last updated

Was this helpful?