Fortune Telling Collection - Zodiac Analysis - What are some examples of switch usage in C language?
What are some examples of switch usage in C language?
# include & ltstdio.h & gt
int main(){
int a;
Printf ("Enter integer:");
scanf("%d ",& ampa);
Switch (a)
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 value: printf ("error \ n"); Break;
}
Returns 0;
}
Running results:
Enter the integer number:4↙.
Thursday
Analysis:
1 and default are placed before or after the case, and the case is executed after the case is executed (the default statement can be omitted).
2. The constant expression after a case can only be an integer, a character or a statement enumerating constants, and it must be broken after it is finished, otherwise the next case will be executed.
Obey the rules of the switch statement
The switch statement is very useful, but it must be used with caution. Any switch statement you write must follow the following rules:
Switch can only be used for integer types in basic data types, including int, char, etc. For other types, if statements must be used.
The parameter type of switch () cannot be a real number.
The case tag must be a constant expression, such as 42 or "4".
The case label must be a unique expression; That is, two case are not allowed to have the same value.
- Related articles
- Who knows what constellation monkeys are?
- Where was Ju Xianhao born?
- Help me calculate the horoscope of a man born on July 24th, 984 and June 26th of the lunar calendar.
- Which constellations don't know how to cherish feelings and are stubborn?
- What is the guardian of the twelve constellations?
- Ai Hua belongs to the stars at night.
- How to make lanterns with paper cups
- What is Capricorn? Capricorn is an earth sign, right?
- Which constellation men are a little tired and slow in their feelings?
- Six constellations with strong reasoning ability _ What are the six constellations with strong reasoning ability?