流程控制语句

流程控制语句

 

条件语句:if/switch

if语句:

if(条件){条件成立时执行的代码}

             if(){}   else{ }

            if(条件1){代码块1}else if(条件2){代码块2}else{代码}

 

switch: (有些注意的点***)

switch(表达式)

{

case1:执行代码1;

  sysout();

break;

case值n:

执行代码n;

break;

default:默认执行的代码; }

 

循环语句:

while/do  while/for

for和while 的区别:  for更优化内存

 

while:

语法:while(判断条件){循环操作} 先判断后执行.

for循环:

语法:for(循环变量;循环条件;循环变量变化){循环操作}

do...while

语法:do{循环操作}while(判断条件) 先执行后判断,至少被执行一次

 

 

break及continue

break语句会跳出循环,执行后面的代码。
continue的作用是跳过循环体中剩余的语句执行下一次循环。

 

 

 

for嵌套运用

 

例子

九九乘法:

//9*9乘法
public class fortest {
public static void main(String[] args) {
         for(int x=1;x<=9;x++){
               for(int y=1;y<=x;y++){
               System.out.print(x+"*"+y+"="+(x*y)+"\t");
               }
          System.out.println();
        }
}
}

 

菱形:

for(int x=1;x<=5;x++){
for(int y=1;y<=5-x;y++){
System.out.print(" ");
}
for(int z=1;z<=2*x-1;z++){
System.out.print("*");
}
System.out.println();
}
for(int x=1;x<=5;x++){
for(int y=1;y<=x;y++){
System.out.print(" ");
}
for(int z=1;z<=2*(5-x)-1;z++){
System.out.print("*");
}
System.out.println();
}

 

posted @ 2018-10-14 14:44  StingLon  阅读(131)  评论(0)    收藏  举报