Java流程控制

1. 顺序控制

// 顺序结构
public class Demo01Sequence {
	public static void main(String[] args) {
		System.out.println("今天天气不错");
		System.out.println("挺风和日丽的");
		System.out.println("我们下午没课");
		System.out.println("这的确挺爽的");
	}
}

2. if

2.1 if 单分支

// 单if语句
public class Demo02If {
	public static void main(String[] args) {
		System.out.println("今天天气不错,正在压马路……突然发现一个快乐的地方:网吧");
		int age = 19;
		if (age >= 18) {  // go中不用写括号
			System.out.println("进入网吧,开始high!");
			System.out.println("遇到了一群猪队友,开始骂街。");
			System.out.println("感觉不爽,结账走人。");
		}
		System.out.println("回家吃饭");
	}
}

2.2 if 双分支

// 标准的if-else语句
public class Demo03IfElse {
	public static void main(String[] args) {
		int num = 666;
		
		if (num % 2 == 0) { // 如果除以2能够余数为0,说明是偶数
			System.out.println("偶数");
		} else {  // go中else不能写到下面去,且go中不用写分号(写上也不会报错),编译器会自动补上
			System.out.println("奇数");
		}
	}
}

2.3 多分支

public class Demo05IfElsePractise {
	public static void main(String[] args) {
		int score = 120;
		if (score >= 90 && score <= 100) {
			System.out.println("优秀");
		} else if (score >= 80 && score < 90) {
			System.out.println("好");
		} else if (score >= 70 && score < 80) {
			System.out.println("良");
		} else if (score >= 60 && score < 70) {
			System.out.println("及格");
		} else if (score >= 0 && score < 60) {
			System.out.println("不及格");
		} else { // 单独处理边界之外的不合理情况
			System.out.println("数据错误");
		}
	}
}

3. switch

public class Demo07Switch {
	public static void main(String[] args) {
		int num = 10;
		
		switch (num) {
			case 1:
				System.out.println("星期一");
				break;
			case 2:
				System.out.println("星期二");
				break;
			case 3:
				System.out.println("星期三");
				break;
			case 4:
				System.out.println("星期四");
				break;
			case 5:
				System.out.println("星期五");
				break;
			case 6:
				System.out.println("星期六");
				break;
			case 7:
				System.out.println("星期日");
				break;
			default:
				System.out.println("数据不合理");
				break; // 最后一个break语句可以省略,但是强烈推荐不要省略
		}
	}
}

3.1 注意事项

/*
switch语句使用的注意事项:

1. 多个case后面的数值不可以重复。

2. switch后面小括号当中只能是下列数据类型:
基本数据类型:byte/short/char/int
引用数据类型:String字符串、enum枚举

3. switch语句格式可以很灵活:前后顺序可以颠倒,而且break语句还可以省略。
“匹配哪一个case就从哪一个位置向下执行,直到遇到了break或者整体结束为止。”
*/
public class Demo08SwitchNotice {
	public static void main(String[] args) {
		int num = 2;
		switch (num) {
			case 1:
				System.out.println("你好");
				break; //go中不用写break,默认自带的,也不用写;
			case 2:
				System.out.println("我好");
				// break;
			case 3:
				System.out.println("大家好");
				break;
			default:
				System.out.println("他好,我也好。");
				break;
		} // switch
	}
}

4. for循环

/*
循环结构的基本组成部分,一般可以分成四部分:

1. 初始化语句:在循环开始最初执行,而且只做唯一一次。
2. 条件判断:如果成立,则循环继续;如果不成立,则循环退出。
3. 循环体:重复要做的事情内容,若干行语句。
4. 步进语句:每次循环之后都要进行的扫尾工作,每次循环结束之后都要执行一次。
*/
public class Demo09For {
	public static void main(String[] args) {
		for (int i = 1; i <= 100; i++) {
			System.out.println("我错啦!原谅我吧!" + i);
		}
		System.out.println("程序停止");
	}
}

5. while

/*
while循环有一个标准格式,还有一个扩展格式。

标准格式:
while (条件判断) {
	循环体
}

扩展格式:

初始化语句;
while (条件判断) {
	循环体;
	步进语句;
}
*/
public class Demo10While {
	public static void main(String[] args) {
		for (int i = 1; i <= 10; i++) {
			System.out.println("我错啦!" + i);
		}
		System.out.println("=================");
		
		int i = 1; // 1. 初始化语句
		while (i <= 10) { // 2. 条件判断
			System.out.println("我错啦!" + i); // 3. 循环体
			i++; // 4. 步进语句
		}
	}
}

5.1 死循环

/*
永远停不下来的循环,叫做死循环。

死循环的标准格式:
while (true) {
	循环体
}
*/
public class Demo16DeadLoop {
	public static void main(String[] args) {
		while (true) {
			System.out.println("I Love Java!");
		}
		
		// System.out.println("Hello");
	}
}

6. do while

/*
do-while循环的标准格式:

do {
	循环体
} while (条件判断);

扩展格式:

初始化语句
do {
	循环体
	步进语句
} while (条件判断);
*/
public class Demo11DoWhile {
	public static void main(String[] args) {
		for (int i = 1; i <= 10; i++) {
			System.out.println("原谅你啦!起来吧!地上怪凉!" + i);
		}
		System.out.println("===============");
		
		int i = 1; // 1. 初始化语句
		do {
			System.out.println("原谅你啦!起来吧!地上怪凉!" + i); // 3. 循环体
			i++; // 4. 步进语句
		} while (i <= 10); // 2. 条件判断
	}
}
// do死循环
int i = 1; // 1. 初始化语句
        do {
            System.out.println("原谅你啦!起来吧!地上怪凉!" + i); // 3. 循环体
            //i++; // 4. 步进语句
        } while (true); // 2. 条件判断

7. 三种循环的区别

/*
三种循环的区别。

1. 如果条件判断从来没有满足过,那么for循环和while循环将会执行0次,但是do-while循环会执行至少一次。
2. for循环的变量在小括号当中定义,只有循环内部才可以使用。while循环和do-while循环初始化语句本来就在外面,所以出来循环之后还可以继续使用。
*/
public class Demo13LoopDifference {
	public static void main(String[] args) {
		for (int i = 1; i < 0; i++) {
			System.out.println("Hello");
		}
		// System.out.println(i); // 这一行是错误写法!因为变量i定义在for循环小括号内,只有for循环自己才能用。
		System.out.println("================");
		
		int i = 1;
		do {
			System.out.println("World");
			i++;
		} while (i < 0);
		// 现在已经超出了do-while循环的范围,我们仍然可以使用变量i
		System.out.println(i); // 2
	}
}

8. break/continue

/*
break关键字的用法有常见的两种:

1. 可以用在switch语句当中,一旦执行,整个switch语句立刻结束。
2. 还可以用在循环语句当中,一旦执行,整个循环语句立刻结束。打断循环。

关于循环的选择,有一个小建议:
凡是次数确定的场景多用for循环;否则多用while循环。
*/
public class Demo14Break {
	public static void main(String[] args) {
		for (int i = 1; i <= 10; i++) {
			// 如果希望从第4次开始,后续全都不要了,就要打断循环
			if (i == 4) { // 如果当前是第4次
				break; // 那么就打断整个循环
			}
			System.out.println("Hello" + i);
		}
	}
}
/*
另一种循环控制语句是continue关键字。
一旦执行,立刻跳过当前次循环剩余内容,马上开始下一次循环。
*/
public class Demo15Continue {
	public static void main(String[] args) {
		for (int i = 1; i <= 10; i++) {
			if (i == 4) { // 如果当前是第4层
				continue; // 那么跳过当前次循环,马上开始下一次(第5层)
			}
			System.out.println(i + "层到了。");
		}
	}
}
原文地址:https://www.cnblogs.com/yzg-14/p/12189198.html