1、if 语句
if(boolean_expression) { //执行语句 }
2、if…else语句
if(boolean_expression) { //执行语句 } else { //执行语句 }
3、if…else if…else
if(boolean_expression 1) { //执行语句 } else if(boolean_expression 2) { //执行语句 } else if(boolean_expression 3) { //执行语句 } else { //执行语句 }
4、switch语句
switch(expression) { case constant-expression-1: //执行语句 break; //可选 case constant-expression-2: //执行语句 break; //可选 //...任意数量的case语句 default: //可选,如果所有case都不执行,则执行default //执行语句 }
5、嵌套判断语句
(1)if 嵌套
if(boolean_expression 1) { if(boolean_expression 2) { //执行语句 } }
(2)switch 嵌套
switch(expression1) { case constant-expression-1A: switch(expression2) { case constant-expression-1B: //执行语句 break; //可选 case constant-expression-2B: //执行语句 break; //可选 } break; //可选 case constant-expression-2A: switch(expression3) { case constant-expression-1C: //执行语句 break; //可选 case constant-expression-2C //执行语句 break; //可选 } break; //可选 //...任意数量的case语句 default: //可选,如果所有case都不执行,则执行default //执行语句 }
6、运算符“ ?: ”
int a = 10, b = 5; int min =(a < b) ? a : b;
int a = 10, b = 5, c = 15; int min = (((a < b) ? a : b)<c) ? min : c;