/**
* while(A表达式)
* {
* B语句块;
* }
*
* 执行步骤:
* 1.判断A表达式
* 返回true,执行B语句块,继续进行步骤1的判断
* 返回false,结束循环
* =========================================================
* do{
* B语句块
* }while(A表达式);
*
* 执行步骤:
* 1.执行B语句块
* 2.判断A表达式
* 返回true,执行B语句块,继续进行步骤2的判断
* 返回false,结束循环
*
*
* while: 先判断,再执行
* do..while: 先执行一次,再判断
*
*/
//while 循环输出1~10之间的整数
int i = 1;
while( i<=10 )
{
//System.out.println(i);
i++;
}
//do..while..循环输出1~10之间的整数
int k = 1;
do
{
System.out.println(k);
k++;
}while(k<=10);//分号必须有
//死循环
/*while(true)
{
System.out.println("死循环");
}*/
/*int j =1;
while( j<=10 )
{
System.out.println(j);
//j++; //忘记自增条件
}*/
Scanner scanner = new Scanner(System.in);
//死循环
while(true)
{
System.out.println("死循环说:");
String content = scanner.nextLine();
if(!"exit".equals(content))
{
System.out.println(content);
}
else
{
break;//创造条件终止循环
}
}
/**
*
* for(初始化表达式;循环条件表达式;循环后的操作表达式)
* {
* 语句块;
* }
*
* 执行步骤:
* A初始化表达式(仅执行一次)
* B循环条件表达式
C语句块 1
---------------------------
D循环后的操作表达式
B循环条件表达式
C语句块 2
---------------------------
D循环后的操作表达式
B循环条件表达式
C语句块 3
---------------------------
D循环后的操作表达式
B循环条件表达式,不满足条件,终止循环
*
*/
public static void main(String[] args) {
//输出1~10之间的整数
for(int i=1;i<=10;i++)
{
//System.out.println(i);
}
//测试for执行步骤
int i = 1;
for (System.out.println("A初始化表达式"); i <= 3; System.out.println("D循环后的操作表达式")) {
System.out.println("C语句块 " + i);
i++;
System.out.println("---------------------------");
}
/**
* 终止循环语句:
* break语句:终止整个循环
* continue语句:终止当前循环
* break label:终止指定的名称为lable的循环
*/
public static void main(String[] args) {
//break语句
/*int i=1;
while(i<=10)
{
if(i%3==0)
{
break; //跳出循环,终止整个循环
}
System.out.println(i);
i++;
}*///1 2
//continue语句
/*int j=0;
while(j<=10)
{
if(j%3==0)
{
j++;
continue;//3 6 9终止本次循环,进入下一次循环
}
System.out.println(j);
j++;
}*///1 2 4 5 7 8 10
//break label语句
//外部循环
outer:for (int i = 1; i <= 10; i++) {
System.out.println("outer:"+i);
//内部循环
inner:while(true)
{
Scanner scanner = new Scanner(System.in);
String content = scanner.nextLine();
System.out.println("inner:" + content);
if("exit inner".equals(content))
{
break inner;//终止内部循环
}
else if("exit outer".equals(content))
{
break outer;//终止外部循环
}
}
}