C Programming - Beginner to Brilliance / Control Flow Statements

Control Flow & Conditional Statements?

πŸ“Œ What is Control Flow?

In real life, you make decisions like:

  • β€œIf it’s raining, take an umbrella.”

  • β€œRepeat this workout 10 times.”

In programming, we do the same using control flow statements, which let you:

  • 🚦 Make decisions (conditions)

  • πŸ” Perform loops

  • πŸ›‘ Jump from one part of the program to another


✨ Types of Control Flow Statements in C

Type

Purpose

βž— Conditional Statements

Make decisions

πŸ” Looping Statements

Repeat code

🧯 Jump Statements

Exit or skip code

πŸ” What are Conditional Statements?

Conditional statements allow your program to choose different actions based on certain conditions.

🧠 Think of it like this:

"If the light is green, cross the road. Otherwise, wait."

In C, you write such logic using:

  • if

  • if-else

  • if-else if-else

  • switch

These help the program evaluate whether something is true or false, and then act accordingly.


βœ… if Statement

πŸ”Ή Syntax:

if (condition) {
    // code to run if condition is true
}

πŸ”Ή Key Points:

  • condition is usually a comparison like x > 0 or a == b.

  • If the condition is true (1), the code runs.

  • If the condition is false (0), the block is skipped.

πŸ“Œ Example:

int age = 20;
if (age >= 18) {
    printf("You are an adult.");
}

🧠 What happens:

  • age >= 18 is true

  • The message prints


❌ Common Mistake:

if (x = 5) { ... } // Wrong!

βœ… Use == for comparison, not = (which is assignment).


βœ… if-else Statement

Used when there are two possible outcomes.


πŸ”Ή Syntax:

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

πŸ“Œ Example:

int number = 5;
if (number % 2 == 0) {
    printf("Even");
} else {
    printf("Odd");

🎯 Real-life Analogy:

"If I have money, I’ll buy pizza. Else, I’ll cook at home."


βœ… if-else if-else Ladder

Used when you want to check multiple conditions in sequence.


πŸ”Ή Syntax:

if (condition1) {
    // block1
} else if (condition2) {
    // block2
} else if (condition3) {
    // block3
} else {
    // default block
}

πŸ“Œ Example:

int marks = 75;

if (marks >= 90) {
    printf("Grade A");
} else if (marks >= 75) {
    printf("Grade B");
} else if (marks >= 60) {
    printf("Grade C");
} else {
    printf("Fail");
}
Grade BπŸ” How it works:
  • Checks each condition in order

  • As soon as one is true, it executes that block and skips the rest


πŸ”„ switch Statement

Useful when you need to compare one variable to many values.

πŸ”Ή Syntax:

switch(expression) {
    case value1:
        // block1
        break;
    case value2:
        // block2
        break;
    default:
        // default block
}

πŸ“Œ Example:

int day = 2;

switch (day) {
    case 1: printf("Monday"); break;
    case 2: printf("Tuesday"); break;
    case 3: printf("Wednesday"); break;
    default: printf("Invalid day");
}
Tuesday

πŸ†š if-else vs switch

Feature

if-else

switch

Conditions

Complex expressions

Only compares variable to constants

Data Types

int, float, char, expressions

Only int or char (not float or string)

Use Case

Complex logic

Multiple constant comparisons

Flexibility

Very flexible

Limited but clean for fixed choices


🧠 Real-Life Analogy for All

Scenario

C Statement

"If it's raining, take umbrella."

if

"If it’s Monday, do work. Else, rest."

if-else

"If grade >= 90 then A, >=75 then B, else C."

if-else if-else

"Choose action based on button pressed (1-5)."

switch


πŸ““ Summary (Cheat Sheet)

Statement

Use For

if

Single condition

if-else

Two outcomes

if-else if-else

Multiple conditions

switch

Multiple constant values for 1 variable