Examples Progress
0%
Lesson 3  ·  Extra Examples

Conditionals — 5 More Examples

Five unique, real-world programs that use if, else if, else, and switch. Each one introduces a different use case you will encounter in real programs.

E1

Voting Eligibility Checker

Multiple AND conditions

Checks multiple conditions together using && (AND). A person can vote only if they are 18 or older AND have a valid voter ID. Both conditions must be true simultaneously.

Example E1 · vote_check.c
vote_check.c
C
#include <stdio.h>

int main() {
    int age, hasID;

    printf("Enter your age: ");
    scanf("%d", &age);

    printf("Do you have voter ID? (1=Yes 0=No): ");
    scanf("%d", &hasID);

    // Both conditions must be true
    if (age >= 18 && hasID == 1) {
        printf("✓ You are eligible to vote!\n");
    } else if (age < 18 && hasID == 1) {
        printf("✗ Too young. Come back in %d year(s).\n", 18 - age);
    } else if (age >= 18 && hasID == 0) {
        printf("✗ Right age but no voter ID card.\n");
    } else {
        printf("✗ Not eligible — too young and no ID.\n");
    }

    return 0;
}
terminal
output
age=22 hasID=1  →  ✓ You are eligible to vote!
age=16 hasID=1  →  ✗ Too young. Come back in 2 year(s).
age=25 hasID=0  →  ✗ Right age but no voter ID card.
age=15 hasID=0  →  ✗ Not eligible — too young and no ID.
💡 && = both must be true. || = at least one must be true. You can combine them: age >= 18 && (city == 1 || city == 2)
example 2
E2

Weather & Clothing Advisor

float + else if chain

Uses float input with an else if chain to give contextual advice. Shows that conditionals work with any data type — not just int. A practical example of how apps give weather-based suggestions.

Example E2 · weather_advice.c
weather_advice.c
C
#include <stdio.h>

int main() {
    float temp;

    printf("Enter current temperature (°C): ");
    scanf("%f", &temp);

    printf("\n--- Weather Report ---\n");

    if      (temp <= 5)  printf("❄  Freezing! Wear a heavy coat & gloves.\n");
    else if (temp <= 15) printf("🧥 Cold. A jacket is needed.\n");
    else if (temp <= 25) printf("😊 Pleasant. Light clothes work fine.\n");
    else if (temp <= 35) printf("☀  Hot. Wear cotton & drink water!\n");
    else                  printf("🔥 Extreme heat! Stay indoors.\n");

    if (temp > 30)
        printf("💧 Stay hydrated — drink 3L water today.\n");

    return 0;
}
terminal
output
Temperature: 3   →  ❄  Freezing! Wear a heavy coat & gloves.
Temperature: 18  →  😊 Pleasant. Light clothes work fine.
Temperature: 38  →  🔥 Extreme heat! Stay indoors.
                    💧 Stay hydrated — drink 3L water today.
example 3
E3

Triangle Type Identifier

Complex conditions with &&

Reads three side lengths and determines the triangle type. Uses complex compound conditions combining == and != with &&. Also validates whether a triangle is even possible before checking its type.

Example E3 · triangle_type.c
triangle_type.c
C
#include <stdio.h>

int main() {
    float a, b, c;

    printf("Enter three sides of a triangle: ");
    scanf("%f %f %f", &a, &b, &c);

    // First check if it is a valid triangle
    if (a + b > c && b + c > a && a + c > b) {

        if (a == b && b == c) {
            printf("Equilateral Triangle — all 3 sides equal\n");
        } else if (a == b || b == c || a == c) {
            printf("Isosceles Triangle — 2 sides equal\n");
        } else {
            printf("Scalene Triangle — all sides different\n");
        }

    } else {
        printf("Not a valid triangle!\n");
        printf("(Each side must be less than sum of other two)\n");
    }

    return 0;
}
terminal
output
5 5 5    →  Equilateral Triangle — all 3 sides equal
5 5 8    →  Isosceles Triangle — 2 sides equal
3 4 5    →  Scalene Triangle — all sides different
1 2 10   →  Not a valid triangle!
example 4
E4

Shopping Discount System

Tiered pricing with float

A real e-commerce style discount system. The more you spend, the bigger the discount. Shows how conditionals control float calculations — the actual discount percentage changes based on the total bill amount.

Example E4 · discount.c
discount.c
C
#include <stdio.h>

int main() {
    float bill, discount = 0, finalBill;
    int   isPremium;

    printf("Enter bill amount (Rs): ");
    scanf("%f", &bill);

    printf("Are you a premium member? (1=Yes 0=No): ");
    scanf("%d", &isPremium);

    // Tiered discount based on bill amount
    if      (bill >= 5000) discount = 20;
    else if (bill >= 2000) discount = 15;
    else if (bill >= 1000) discount = 10;
    else if (bill >= 500)  discount = 5;
    else                   discount = 0;

    // Premium members get 5% extra
    if (isPremium == 1) discount += 5;

    finalBill = bill - (bill * discount / 100);

    printf("\n--- RECEIPT ---\n");
    printf("Original bill : Rs %.2f\n", bill);
    printf("Discount      : %.0f%%\n",  discount);
    printf("Amount saved  : Rs %.2f\n", bill - finalBill);
    printf("Final bill    : Rs %.2f\n", finalBill);

    return 0;
}
terminal
output
Bill: 3000  Premium: 1
Original bill : Rs 3000.00
Discount      : 20%
Amount saved  : Rs 600.00
Final bill    : Rs 2400.00

Bill: 800  Premium: 0
Discount: 5%  Final bill: Rs 760.00
example 5
E5

Month to Season — switch with Fall-through

switch grouping cases

Demonstrates intentional fall-through in switch — multiple cases sharing the same outcome. Months 3,4,5 all map to Spring. No break between them so they fall through to the same printf. This is the one good use of intentional fall-through.

Example E5 · season_finder.c
season_finder.c
C
#include <stdio.h>

int main() {
    int month;

    printf("Enter month number (1-12): ");
    scanf("%d", &month);

    printf("Month %d is in: ", month);

    switch (month) {
        case 3:
        case 4:
        case 5:
            printf("🌸 Spring\n");
            break;

        case 6:
        case 7:
        case 8:
            printf("☀  Summer\n");
            break;

        case 9:
        case 10:
        case 11:
            printf("🍂 Autumn\n");
            break;

        case 12:
        case 1:
        case 2:
            printf("❄  Winter\n");
            break;

        default:
            printf("Invalid month!\n");
    }

    return 0;
}
terminal
output
Month 4   →  🌸 Spring
Month 7   →  ☀  Summer
Month 10  →  🍂 Autumn
Month 1   →  ❄  Winter
Month 13  →  Invalid month!
💡 Grouping cases without break is the one good use of fall-through. When multiple months share the same season, writing case 3: case 4: case 5: together is clean and intentional.
examples checklist

Examples Checklist

  • E1 — I can use && to check multiple conditions together
  • E1 — I understand that all four combinations of two conditions can be handled
  • E2 — I can use float variables inside if conditions
  • E3 — I can combine multiple comparisons with && and ||
  • E3 — I validate input before processing it further
  • E4 — I can use if to select a discount and apply it in a calculation
  • E5 — I understand intentional fall-through for grouping switch cases
  • I completed all 5 example programs