一、判断语句

1. if 判断

if(关系表达式){
语句体;
} int age = 16;
if(age >= 18){
System.out.println("hello");
}

2. if-else判断

if(判断条件){
语句体1;
} else{
语句体2;
} int num = 13;
if(num % 2 == 0){
System.out.println("偶数");
} else{
System.out.println("基数");
}

3. if-else if-else

if(判断条件1){
执行语句1;
} else if(判断条件2){
执行语句2;
}
...
else if(判断条件n){
执行语句n;
} else{
执行语句n+1;
}

二、选择语句

switch(表达式){
case 常量值1:
语句体1;
break;
case 常量值2:
语句体1;
break;
...
default:
语句体n+1;
break;
}

三、循环语句

1. for 循环

// 次数确定的场景,多采用for
for(初始化表达式1; 布尔表达式2; 步进表达式3){
循环体;
} for(int i=1; i<10; i++){
System.out.println("hello");
}

2. while 循环

while(条件判断){
循环体;
} 初始化语句;
while(条件判断){
循环体;
步进语句;
} int i = 1;
while(i<=10){
System.out.println("hello");
i++;
}

3. do-while 循环

初始化表达式;
do{
循环体;
步进语句;
}while(布尔表达式); int i = 1;
do{
System.out.println("hello");
i++;
}while(i<=10);

4. 例子

public class test {
public static void main(String[] args) {
int sum = 0;
for(int i=1; i<=100;i++){
if(i % 2 == 0){
System.out.println(i);
sum += i;
}
}
System.out.println(sum);
}
}

5. break 语句

public class test {
public static void main(String[] args) {
for(int i=1; i<=100;i++){
if(i == 90){
// 如果i是90,打断整个循环
break;
}
System.out.println(i);
}
}
}

6. continue 语句

public class test {
public static void main(String[] args) {
for(int i=1; i<=100;i++){
if(i == 90){
// 如果i是90,打断本次循环,继续下一次循环
continue;
}
System.out.println(i);
}
}
}

7. 死循环

public class test {
public static void main(String[] args) {
while(true){
System.out.println("hello world");
}
}
}

8. 循环嵌套

public class test {
public static void main(String[] args) {
for (int i = 0; i < 60; i++) {
for (int j = 0; j < 60; j++) {
System.out.println("当前时间:" + i + "时" + j + "秒");
}
}
}
}

最新文章

  1. Unable to download data from http://ruby.taobao.org/ &amp; don&#39;t have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
  2. DEDE后台登录和前台验证码不显示的解决方法
  3. POJ3468 A Simple Problem with Integers
  4. python数据结构-列表-基本操作
  5. Go视频教程整理
  6. 报错:org.apache.jasper.JasperException: /index.jsp (line: 1, column: 17) equal symbol expected
  7. Java 装饰模式 (Decorator)
  8. Linux启动或禁止SSH用户及IP的登录
  9. HDU 5818 Joint Stacks(左偏树)
  10. 对于Netty的十一个疑问(转)
  11. 【CSS学习笔记】整齐的表格
  12. 图的两种遍历:DFS&amp;BFS
  13. .net之简单工厂模式
  14. 【记录一次windows技术学习】使用笔记本DOS命令搭建WLAN热点
  15. Data Center手册(2): 安全性
  16. Python简介之探观止矣
  17. vue 3.0的搭建
  18. 用shell统计表格数据
  19. MobX基础 ----- 类的静态属性和装饰器
  20. ORM操作mysql

热门文章

  1. gradle环境搭建
  2. Jsf中进度条的用法
  3. 详解Python函数参数定义及传参(必备参数、关键字参数、默认可省略参数、可变不定长参数、*args、**kwargs)
  4. PWA 学习笔记(一)
  5. MongoDB 中聚合统计计算--$SUM表达式
  6. sqlplus登录时密码有特殊符号解决方法
  7. diango下载、创建、启动
  8. centos7.6 jumpserver 堡垒机 重启启动顺序
  9. Python+Unittest+Requests+PyMysql+HTMLReport 接口自动化框架
  10. Redis与Redis 伪集群环境的搭建