JAVA学习日志(循环)

循环

基础语法循环


1.Switch (整型和字符)  

整数型(byte\char \ int \ short JDK 1.7以上可以使用字符串 


问题 为什么byteshort能在switch后面 但是long却不能?

    

  因为 byteshortint 小可以通过隐式转换,而long如果转换就是强制类型转换



Swicth (整型数 {

  Case (常量     break;

  Case     常量     break

 ………..

Default

}


2. 三种循环 

For循环  

for (初始化计数;条件测试;再求值参数){

 语句 //如果是符合语句不能省略 

}

第一种

For (int i = 1; i <=100; i ++){

  System.out.println(i+”\t”);

}

  第二种 

     

      int i = 1;

for( ; i<=100;i++){

 system.out.println(i+”\t”);

}

 第三种 

 

   int i = 1;

for (; i<100; ){

 system.out.println(i+”\t”);

}

死循环

for ( ; ; ){

 system.out.println(“a”);

}


列题  输出1100 ,并且每五个数字换一行

For (int I = 1 ; i<100; i++){

  If(i%5==0)

   System.out.println();

   Sytem.out.print(“\n”);

}



##break and Continue

 

注意:嵌套不能用过多,一般不能超过三层 

For( int I = 0; i<=100;i++){

   For (int j = 0 ; j <=100;j++){

 

}

 

}

  注:在写system.out.print() 不能里面是空的

 


Break 语句  // break是结束整个循环体  如果在多层循环中 break 向外跳出一层


  For (int I =1 ; i<5; i++){

     If(i==5)break;

   System.out.println(i);

}   答案是 1 2 3 4 


Continue语句 是结束单次循环 


Forint i= 0  i<8; i++{

  If(i== 5 )continue;

System.out.println(i);

}  结果是 0 1 2 3 4 6 7 8


##While循环 and  Do while循环 

while (表达式){

   语句 

}Do{

  语句

}while (表达式)


 while  for  do while 分别什么时候使用?

如果循环次数固定 ,一般使用for循环


如果循环次数未知,一般使用while do while(至少可以允许一次)


发布了43 篇原创文章 · 获赞 1 · 访问量 7万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章