c

examples

examples.c🔧
/**
 * @file examples.c
 * @brief switch-case Statement Examples
 * 
 * This file demonstrates the switch-case statement in C.
 * 
 * Compile: gcc -Wall examples.c -o examples
 * Run: ./examples
 */

#include <stdio.h>

/* ============================================================
 * EXAMPLE 1: BASIC SWITCH STATEMENT
 * ============================================================ */
void example_basic_switch() {
    printf("\n=== Example 1: Basic switch Statement ===\n");
    
    int number = 2;
    
    printf("Number is: %d\n", number);
    printf("Result: ");
    
    switch (number) {
        case 1:
            printf("One\n");
            break;
        case 2:
            printf("Two\n");
            break;
        case 3:
            printf("Three\n");
            break;
        default:
            printf("Unknown number\n");
            break;
    }
}

/* ============================================================
 * EXAMPLE 2: DAY OF THE WEEK
 * ============================================================ */
void example_day_of_week() {
    printf("\n=== Example 2: Day of the Week ===\n");
    
    int day = 5;
    
    printf("Day number: %d\n", day);
    printf("Day name: ");
    
    switch (day) {
        case 1:
            printf("Monday\n");
            break;
        case 2:
            printf("Tuesday\n");
            break;
        case 3:
            printf("Wednesday\n");
            break;
        case 4:
            printf("Thursday\n");
            break;
        case 5:
            printf("Friday\n");
            break;
        case 6:
            printf("Saturday\n");
            break;
        case 7:
            printf("Sunday\n");
            break;
        default:
            printf("Invalid day (must be 1-7)\n");
            break;
    }
}

/* ============================================================
 * EXAMPLE 3: CHARACTER SWITCH
 * ============================================================ */
void example_char_switch() {
    printf("\n=== Example 3: Character switch ===\n");
    
    char grade = 'B';
    
    printf("Grade: %c\n", grade);
    printf("Message: ");
    
    switch (grade) {
        case 'A':
            printf("Excellent work!\n");
            break;
        case 'B':
            printf("Good job!\n");
            break;
        case 'C':
            printf("Satisfactory performance.\n");
            break;
        case 'D':
            printf("Needs improvement.\n");
            break;
        case 'F':
            printf("Please see the instructor.\n");
            break;
        default:
            printf("Invalid grade.\n");
            break;
    }
}

/* ============================================================
 * EXAMPLE 4: FALL-THROUGH BEHAVIOR
 * ============================================================ */
void example_fall_through() {
    printf("\n=== Example 4: Fall-through Behavior ===\n");
    
    int x = 1;
    
    printf("Without break (x = %d):\n", x);
    switch (x) {
        case 1:
            printf("  Case 1\n");
            // No break - falls through!
        case 2:
            printf("  Case 2\n");
            // No break - falls through!
        case 3:
            printf("  Case 3\n");
            break;
        default:
            printf("  Default\n");
    }
    
    printf("\nWith break (x = %d):\n", x);
    switch (x) {
        case 1:
            printf("  Case 1\n");
            break;
        case 2:
            printf("  Case 2\n");
            break;
        case 3:
            printf("  Case 3\n");
            break;
        default:
            printf("  Default\n");
    }
}

/* ============================================================
 * EXAMPLE 5: INTENTIONAL FALL-THROUGH
 * ============================================================ */
void example_intentional_fallthrough() {
    printf("\n=== Example 5: Intentional Fall-through ===\n");
    
    // Check if day is weekday or weekend
    int day = 6;
    
    printf("Day %d is a ", day);
    
    switch (day) {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            printf("Weekday\n");
            break;
        case 6:
        case 7:
            printf("Weekend\n");
            break;
        default:
            printf("Invalid day\n");
    }
    
    // Vowel check
    char ch = 'e';
    printf("Character '%c' is ", ch);
    
    switch (ch) {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
        case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U':
            printf("a vowel\n");
            break;
        default:
            printf("a consonant\n");
    }
}

/* ============================================================
 * EXAMPLE 6: DAYS IN MONTH
 * ============================================================ */
void example_days_in_month() {
    printf("\n=== Example 6: Days in Month ===\n");
    
    int month = 4;
    int year = 2024;
    int days;
    
    switch (month) {
        case 1: case 3: case 5: case 7:
        case 8: case 10: case 12:
            days = 31;
            break;
        case 4: case 6: case 9: case 11:
            days = 30;
            break;
        case 2:
            // Check for leap year
            if ((year % 4 == 0 && year % 100 != 0) || 
                (year % 400 == 0)) {
                days = 29;
            } else {
                days = 28;
            }
            break;
        default:
            days = 0;
            printf("Invalid month!\n");
    }
    
    if (days > 0) {
        printf("Month %d/%d has %d days\n", month, year, days);
    }
}

/* ============================================================
 * EXAMPLE 7: CALCULATOR
 * ============================================================ */
void example_calculator() {
    printf("\n=== Example 7: Simple Calculator ===\n");
    
    double num1 = 15, num2 = 4;
    char operator = '%';
    
    printf("Operation: %.1f %c %.1f = ", num1, operator, num2);
    
    switch (operator) {
        case '+':
            printf("%.2f\n", num1 + num2);
            break;
        case '-':
            printf("%.2f\n", num1 - num2);
            break;
        case '*':
            printf("%.2f\n", num1 * num2);
            break;
        case '/':
            if (num2 != 0) {
                printf("%.2f\n", num1 / num2);
            } else {
                printf("Error: Division by zero\n");
            }
            break;
        case '%':
            printf("%d\n", (int)num1 % (int)num2);
            break;
        default:
            printf("Unknown operator\n");
    }
}

/* ============================================================
 * EXAMPLE 8: MENU SYSTEM
 * ============================================================ */
void example_menu_system() {
    printf("\n=== Example 8: Menu System ===\n");
    
    printf("\n===== FILE MENU =====\n");
    printf("1. New File\n");
    printf("2. Open File\n");
    printf("3. Save File\n");
    printf("4. Save As\n");
    printf("5. Exit\n");
    printf("=====================\n");
    
    int choice = 2;
    printf("Selected option: %d\n", choice);
    printf("Action: ");
    
    switch (choice) {
        case 1:
            printf("Creating new file...\n");
            break;
        case 2:
            printf("Opening file dialog...\n");
            break;
        case 3:
            printf("Saving current file...\n");
            break;
        case 4:
            printf("Save As dialog...\n");
            break;
        case 5:
            printf("Exiting application...\n");
            break;
        default:
            printf("Invalid option. Please try again.\n");
    }
}

/* ============================================================
 * EXAMPLE 9: ENUM WITH SWITCH
 * ============================================================ */
typedef enum {
    STATE_IDLE,
    STATE_RUNNING,
    STATE_PAUSED,
    STATE_STOPPED,
    STATE_ERROR
} SystemState;

void example_enum_switch() {
    printf("\n=== Example 9: Enum with switch ===\n");
    
    SystemState currentState = STATE_RUNNING;
    
    printf("Current state code: %d\n", currentState);
    printf("State description: ");
    
    switch (currentState) {
        case STATE_IDLE:
            printf("System is idle, waiting for commands.\n");
            break;
        case STATE_RUNNING:
            printf("System is running normally.\n");
            break;
        case STATE_PAUSED:
            printf("System is paused, can resume.\n");
            break;
        case STATE_STOPPED:
            printf("System is stopped, needs restart.\n");
            break;
        case STATE_ERROR:
            printf("System encountered an error!\n");
            break;
        default:
            printf("Unknown state.\n");
    }
}

/* ============================================================
 * EXAMPLE 10: NESTED SWITCH
 * ============================================================ */
void example_nested_switch() {
    printf("\n=== Example 10: Nested switch ===\n");
    
    int department = 1;  // 1 = Engineering, 2 = Sales
    int level = 2;       // 1 = Junior, 2 = Senior, 3 = Lead
    
    printf("Department: %d, Level: %d\n", department, level);
    printf("Role: ");
    
    switch (department) {
        case 1:  // Engineering
            switch (level) {
                case 1:
                    printf("Junior Engineer\n");
                    break;
                case 2:
                    printf("Senior Engineer\n");
                    break;
                case 3:
                    printf("Lead Engineer\n");
                    break;
                default:
                    printf("Unknown Engineering level\n");
            }
            break;
            
        case 2:  // Sales
            switch (level) {
                case 1:
                    printf("Sales Associate\n");
                    break;
                case 2:
                    printf("Sales Manager\n");
                    break;
                case 3:
                    printf("Sales Director\n");
                    break;
                default:
                    printf("Unknown Sales level\n");
            }
            break;
            
        default:
            printf("Unknown department\n");
    }
}

/* ============================================================
 * EXAMPLE 11: VARIABLES IN CASE (WITH BRACES)
 * ============================================================ */
void example_case_variables() {
    printf("\n=== Example 11: Variables in case ===\n");
    
    int operation = 1;
    int value = 10;
    
    switch (operation) {
        case 1: {
            // Using braces to allow variable declaration
            int doubled = value * 2;
            printf("Doubled: %d\n", doubled);
            break;
        }
        case 2: {
            int squared = value * value;
            printf("Squared: %d\n", squared);
            break;
        }
        case 3: {
            int halved = value / 2;
            printf("Halved: %d\n", halved);
            break;
        }
        default:
            printf("Unknown operation\n");
    }
}

/* ============================================================
 * EXAMPLE 12: COMMAND PROCESSOR
 * ============================================================ */
void example_command_processor() {
    printf("\n=== Example 12: Command Processor ===\n");
    
    char command = 'h';
    
    printf("Command: '%c'\n", command);
    printf("Executing: ");
    
    switch (command) {
        case 'n':
        case 'N':
            printf("New document created.\n");
            break;
        case 'o':
        case 'O':
            printf("Open dialog displayed.\n");
            break;
        case 's':
        case 'S':
            printf("Document saved.\n");
            break;
        case 'q':
        case 'Q':
            printf("Quitting application.\n");
            break;
        case 'h':
        case 'H':
        case '?':
            printf("Displaying help...\n");
            printf("  n/N - New\n");
            printf("  o/O - Open\n");
            printf("  s/S - Save\n");
            printf("  q/Q - Quit\n");
            printf("  h/H/? - Help\n");
            break;
        default:
            printf("Unknown command. Press 'h' for help.\n");
    }
}

/* ============================================================
 * MAIN FUNCTION
 * ============================================================ */
int main() {
    printf("╔════════════════════════════════════════════════╗\n");
    printf("║    SWITCH-CASE STATEMENT - EXAMPLES            ║\n");
    printf("║    Multi-way branching in C                    ║\n");
    printf("╚════════════════════════════════════════════════╝\n");
    
    example_basic_switch();
    example_day_of_week();
    example_char_switch();
    example_fall_through();
    example_intentional_fallthrough();
    example_days_in_month();
    example_calculator();
    example_menu_system();
    example_enum_switch();
    example_nested_switch();
    example_case_variables();
    example_command_processor();
    
    printf("\n=== All Examples Completed! ===\n");
    
    return 0;
}
Examples - C Programming Tutorial | DeepML