Monday, April 28, 2025

C Tutorial - 9 - Switch Statement with User Input

#include <stdio.h>

int main()
{
    printf("1-Start Game \n");
    printf("2-Audio Settings \n");
    printf("3-Video Settings \n");
    printf("################ \n");

    int choice;

    printf("Enter Choice \n");
    scanf("%d", &choice);

    switch (choice)
    {
    case 1:
        printf("Start Game");
        break;
    case 2:
        printf("Audio Settings");
        break;
    case 3:
        printf("Video Settings");
        break;
    default:
        printf("Check Your Input:         \n");
        printf("1-Start, 2-Audio, 3-Video \n");
        printf("------------------------- \n");
    }

    return 0;
}

Result:

1-Start Game
2-Audio Settings
3-Video Settings
################
Enter Choice
3
Video Settings
Process returned 0 (0x0)   execution time : 2.090 s
Press any key to continue.

This C code presents a simple menu system for a game or application.

It starts by printing out a menu with options to start the game, access audio settings, and access video settings.

Then it prompts the user to enter their choice, reads the input using the scanf() function, and stores it in an integer variable called choice.

The program then uses a switch statement to check the value of choice and execute the corresponding code block. If the user enters 1, it prints "Start Game". If the user enters 2, it prints "Audio Settings". If the user enters 3, it prints "Video Settings". If the user enters any other value, it prints an error message asking the user to check their input and lists the available options.

Finally, the program ends by returning 0.

 

Why do we need "case" inside "switch"

The case keyword is used inside a switch statement to specify one of several possible paths of execution. When a switch statement is evaluated, the expression in the parentheses is compared to each of the case values in turn. If a match is found, the code block following that case is executed, and then control jumps to the end of the switch statement (unless a break statement is encountered, in which case control jumps to the end of the switch block).

If no match is found, the code block following the default keyword is executed. The default case is optional, and it is executed only if none of the other cases match.

Using a switch statement can be more efficient than using a series of if-else statements when there are multiple possible cases to consider. It can also make the code easier to read and understand, particularly when there are many possible cases.

 

Why do we need "default" inside "switch"

In a switch statement, default is used as a catch-all case for any value that does not match any of the case statements. If none of the case statements matches the value of the expression being tested, then the code inside the default block will be executed.

Having a default case is useful to handle unexpected or invalid values that may be input by the user or that may be generated during program execution. It can provide a way to gracefully handle unexpected behavior and prevent the program from crashing or producing unexpected results.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...