一、控制结构
控制结构有顺序结构、分支结构、循环结构。
1、顺序结构。
从向往下依次执行。
2、分支结构
如果判断为真,执行模块A,否则执行模块B。
3、循环结构
第一步:如果判断为真,进入第二步;否则跳出。
第二步:执行循环体,跳转到第一步。
二、if语句
1、简单的if语句
语法:
if(逻辑表达式){
功能语句;
}
如果逻辑表达式为真,执行功能语句。为假跳出if语句。
class Test{
public static void main(String[] a){
int score = 56;
if(score >= 60){
System.out.println("成绩合格");
}
if(score < 60){
System.out.println("成绩不合格");
}
}
}
2、if...else语句
语法:
if(逻辑表达式){
功能语句1;
}else{
功能语句2;
}
如果逻辑表达式为真,执行功能语句1。为假执行功能语句2。
class Test{
public static void main(String[] a){
int score = 56;
if(score >= 60){
System.out.println("成绩合格");
}else{
System.out.println("成绩不合格");
}
}
}
3、if...else嵌套语句
语句:
if(){
if(){
}else{
}
}else{
if(){
}else{
}
}
class Test{
public static void main(String[] a){
int score = 65;
if(score >= 60){
if(score >= 80){
System.out.println("优秀");
}else{
System.out.println("及格");
}
}else{
System.out.println("不及格");
}
}
}
else的个数只能小于等于if的个数。