C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Basic Syntax of Switch Statement
  • Handling Fall-Through
  • Practical Tips

Was this helpful?

  1. Control Flow

Switch Statement

The switch statement in C provides a way to make multi-way decisions based on the value of an expression. It allows the program to choose a specific code block to execute from several alternatives. Let's explore the syntax and usage of the switch statement.

Basic Syntax of Switch Statement

#include <stdio.h>

int main() {
    int choice;

    // Prompt the user to enter a choice
    printf("Enter a choice (1, 2, or 3): ");
    scanf("%d", &choice);

    // Switch statement
    switch (choice) {
        case 1:
            printf("You chose Option 1\n");
            break;
        case 2:
            printf("You chose Option 2\n");
            break;
        case 3:
            printf("You chose Option 3\n");
            break;
        default:
            printf("Invalid choice\n");
    }

    return 0;
}

Explanation of the Program:

  1. We declare a variable choice to store the user's input.

  2. The printf function prompts the user to enter a choice, and scanf reads the input into the choice variable.

  3. The switch statement is used to check the value of choice against different cases.

  4. Each case represents a possible value of choice. If a match is found, the corresponding code block is executed.

  5. The break statement is used to exit the switch statement after a case is executed.

  6. The default case is optional and executed if none of the case values matches the value of choice.

Handling Fall-Through

Unlike some other programming languages, C allows fall-through behavior between cases. If there is no break statement, the control will fall through to the next case.

#include <stdio.h>

int main() {
    int day = 3;

    // Switch statement with fall-through
    switch (day) {
        case 1:
            printf("Monday\n");
        case 2:
            printf("Tuesday\n");
        case 3:
            printf("Wednesday\n");
        default:
            printf("Unknown day\n");
    }

    return 0;
}

In this example, if day is 3, the output will be:

Wednesday
Unknown day

Practical Tips

  • Each case in a switch statement should end with a break statement to avoid fall-through behavior unless intentional.

  • Use the default case to handle unexpected or invalid values.

Understanding and effectively using the switch statement enhances the flexibility of decision-making in C programs. If you have specific questions or if there are additional topics you'd like to explore, feel free to ask. Happy coding!

PreviousProgram: Weekly Pay CalculationNextFor Loop

Was this helpful?