c语⾔条件语句⽰例_带有⽰例的CC++教程中的SwitchCase语
句
switch case判断字符串c语⾔条件语句⽰例
C/C++ programming languages provides switch ... case statement for selection according to given switch state. switch ... case can be used for different cases in order to control the flow of the application.
C / C ++编程语⾔提供了switch ... case语句,⽤于根据给定的switch状态进⾏选择。 switch ... case可以⽤于不同的情况,以控制应⽤程序的流程。
句法 (Syntax)
switch ... case has the following syntax.
switch ... case具有以下语法。
switch(EXPRESSION)
{
case CONDITION1:
CASE1_CODE;
break;
case CONDITION2:
CASE1_CODE;
break;
...
default:
DEFAULT_CODE;
}
`switch` is the keyword used to create a `switch … case` structure.
“ switch”是⽤于创建“ switch…case”结构的关键字。
`EXPRESSION` is the which will be checked against provided cases.
将根据提供的情况检查“ EXPRESSION”。
`case` keyword is used to create a new case with the specified condition.
“ case”关键字⽤于创建具有指定条件的新案例。
`CONDITION` specifies the conditions related to the case.
CONDITION指定与案件相关的条件。
`CASE_CODE` is used to execute if the given condition is met for the given variable.
如果给定变量满⾜给定条件,则使⽤CASE_CODE执⾏。
`break` is used to end the given case and exit from it. Normally after the matched case, the below cases will be tried to match but the `break` will end the complete switch block and do not check the following cases.
“ break”⽤于结束给定的案例并从中退出。 通常,在匹配⼤⼩写之后,将尝试匹配以下⼤⼩写,但是`break`将结束整个切换块,并且不检查以下⼤⼩写。
`default` case is used when no one of the previously defined cases are met.
如果没有满⾜任何先前定义的⼤⼩写,则使⽤“默认”⼤⼩写。
`DEFAULT_CODE` is used to run when the default case is executed.
DEFAULT_CODE`⽤于在执⾏默认⼤⼩写时运⾏。
切换…案例陈述⽰例 (Switch … Case Statement Example)
Well, the switch case statement is a bit hard to understand according to the other keywords and mechanisms because it contains a lot of code.
好吧,根据其他关键字和机制,switch case语句有点难以理解,因为它包含很多代码。
Switch … Case Statement Example
切换…案例陈述⽰例
#include <stdio.h>
int main () {
/* Variable which will be used inside the switch case */
char mygrade = 'B';
switch(mygrade) {
case 'A' :
printf("Your grade is A\n" );
break;
case 'B' :
printf("Your grade is B\n" );
break;
case 'C' :
printf("Your grade is C\n" );
break;
case 'D' :
printf("Your grade is D\n" );
break;
case 'E' :
printf("Your grade is E\n" );
break;
case 'F' :
printf("Your grade is F\n" );
break;
default :
printf("Invalid grade\n" );
}
return 0;
}
In this example, we will set the variable mygrade as B and this will match with the case B and print to the screen Your grade is B.
在此⽰例中,我们将变量mygrade设置为B ,这将与情况B匹配,并显⽰在屏幕上mygrade Your grade is B
切换语句规则 (Switch Statement Rules)
While using switch ... case statement there are some rules to obey.
在使⽤switch ... case语句时,需要遵守⼀些规则。
The expression should be a result of constant value.
该表达式应为常数值的结果。
Same value cannot be used for multiple cases.
相同的值不能⽤于多种情况。
The `default` statement is optional.
default语句是可选的。
`break` statement is optional but in generall it is used in most cases in order to stop the current check flow of the
switch case.
break语句是可选的,但通常在⼤多数情况下使⽤它,以停⽌当前开关箱的检查流程。
Multiple switch case blocks can be nested but it should be avoided because it will make the application hard to read and understand.
可以嵌套多个开关盒块,但应避免使⽤它,因为这会使应⽤程序难以阅读和理解。
通过If-Else了解更多Javascript决策
默认声明(Default Statement)
default statement is used to run code if there is no match in the existing cases. This can be very helpful to run code in unspecified cases. We will add the default ad the end of the cases and do not provide any case and just provide the default code block we want to run. In the following example we will provide mygrade as Z so it will match the default case and print screen Invalid grade.
如果在现有情况下不匹配,则使⽤default语句运⾏代码。 这对于在未指定的情况下运⾏代码⾮常有帮助。 我们将在案例结尾处添
加default⼴告,不提供任何案例,⽽仅提供我们要运⾏的默认代码块。 在下⾯的⽰例中,我们将mygrade提供为Z因此它将与默认情况和打印屏幕Invalid grade匹配。
#include <stdio.h>
int main () {
/* Variable which will be used inside the switch case */
char mygrade = 'Z';
switch(mygrade) {
case 'A' :
printf("Your grade is A\n" );
break;
case 'B' :
printf("Your grade is B\n" );
break;
case 'C' :
printf("Your grade is C\n" );
break;
case 'D' :
printf("Your grade is D\n" );
break;
case 'E' :
printf("Your grade is E\n" );
break;
case 'F' :
printf("Your grade is F\n" );
break;
default :
printf("Invalid grade\n" );
}
return 0;
}
违约声明 (Break Statement)
The normal behavior of the switch case is flowing from top to down and run matches cases code block and continue for the following cases. But this is generally unwanted situations where after a case is matched and code block executed we generally prefer to exit from switch case. We can use break statement after executing a case code block which will end the switch case completely and does not check for the following cases.
开关案例的正常⾏为是从上到下流动的,并且运⾏匹配案例代码块,并在以下情况下继续运⾏。 但这通常是不希望的情况,在匹配⼤⼩写并执⾏代码块之后,我们通常更喜欢退出切换⼤⼩写。 我们可以在执⾏案例代码块之后使⽤break语句,该代码块将完全结束切换案例,并且不会检查以下案例。
c语⾔条件语句⽰例
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论