程序结构

java和一般语言一样,都有if,for,while 流程控制程序,但是不支持go to

IF

if (condition)
    statement // 一行
    
if (condition) {
    statement //多行
}

if (condition){
    statement
} else if(condition2){
    statement
} else {
    statement
}

FOR

for (statement; condition; statement){
    statement
}
例子:
for (int i = 0; i <  10;i++){
    statement
}

WHILE

while(condition){
    statement
}

do{
    statement
}while(condition);

注意:break可以退出当前循环,continue跳过当前循环,也可以和label联用(自己搜索资料)

SWITCH..CASE

switch(d){
    case m1:
        statement
        break;//退出,如果没有就会往下执行,有些语言不需要手动退出
    case m2:
        statement 
        break;
    default :
        statement
}

Last updated

Was this helpful?