三大结构:
顺序:按照顺序一句一句执行顺序结构是最简单的算法结构。
选择:if(){}else{} ; switch if 多用于范围选择, switch 多用于多个单值的匹配;awitch多个单值匹配,java中的switch可以是整数,字符(java中char会自动转换成int)
循环:while(){};do{}while();for(){}
1:java中的switch可以是整数、字符。
2:while和do while的区别是do while必会执行一次。
3:还有就是边界值的问题:只要限定范围就需要注意边界值。
选择:if
*/
class JavaDemo7
{
public static void main(String[] args)
{
//选择
if(2>1){
System.out.println("成立");
}else{
System.out.println("不成立");
}
/*
nt score = 'a';
switch(score){
case 'a':System.out.println("优秀");break;
case 'b':System.out.println("良好");break;
case 'c':System.out.println("及格");break;
case 'd':System.out.println("差生");break;
default:System.out.println("不清楚");
}
while循环:
/*
int i = 0;
while(i<100){
System.out.println("i:"+i);
i++;
}
int j = 0;
do{
System.out.println("j:"+j);
j++;
}while(j<100);
for(int k=0;k<100;k++){
System.out.println("k:"+k);
}
*/
for循环:
public class Dome {
public static void main(String [] args) {
for(int x = 10; x < 20; x = x+1) {
System.out.print("value of x : " + x );
System.out.print("\n");
}
}
}
while 和 do while的区别是,dowhile至少会执行一次
特别注意:边界值问:只要限定范围就需要注意边界值
int i = 0;
while(i<0){
System.out.println("i:"+i);
i++;
}
System.out.println(i);
int j = 0;
do{
System.out.println("j:"+j);
j++;
}while(j<0);
System.out.println(j);
while和if的区别:结束的地方不一样,while条件结束,当满足条件执行完了后还需要进行一次条件判断就while
int a = 1;
int b = 2;
if(a<b){
System.out.println("a<b");
a=3;
}
int c = 1;
int d = 2;
while(c<d){
System.out.println("c<d");
c=3;
}
}
}
注意事项:
1:最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
2:然后,检测布尔表达式的值。如果为 true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
3:执行一次循环后,更新循环控制变量。
4:再次检测布尔表达式。循环执行上面的过程。