Range switch case statements

Asked 2 years ago, Updated 2 years ago, 25 views

case 1 ... 5 :

I can't because it's a vs.

Or is there a way to compare the size and size of the condition at all?

c++

2022-09-22 18:29

1 Answers

#include <cstdio>

int main(void)
{
    int num = 0;
    scanf("%d", &num);

    switch (num)
    {
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
        printf("First Method - %d\n", num);
        break;
    default:
        printf ("out of range of first method\n");
    }

    switch (1 <= num && num <= 5)
    {
    case 1: // or   case true:
        printf("Second Method - %d\n", num);
        break;
    default:
        printf ("Out of Second Method Range\n");
    }
    return 0;
}


2022-09-22 18:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.