立即学习:https://edu.csdn.net/course/play/27274/361062?utm_source=blogtoedu
Java流程控制
程序的执行顺序
Java从入口程序main开始执行
程序自上而下顺序执行
遇到方法调用,程序跳转到方法调用处继续执行,方法执行完毕,又回到调用方法处
Java循环
for
for each
// 先判断再执行
while 循环
// 先执行再判断
do whilie 循环
// 跳出本次循环,再执行一次循环
continue
// 跳出循环,执行循环下一个语句
break
Java分支
if
if else
if else if else
switch case: break;
package com.zsk;
public class Demo07 {
static void testWhile() {
int sum = 0, i = 1;
while(i <= 10) {
sum += i;
i++;
}
System.out.println(sum);
}
static void testDoWhile() {
int sum = 0, i = 1;
do {
sum += i;
i++;
}while(i <= 10);
System.out.println(sum);
}
static void testContinue() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
System.out.println(i);
}else {
continue;
}
// System.out.println("here..");
}
}
static void testBreak() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
System.out.println(i);
}else {
break;
}
// i = 1,打印语句不执行
System.out.println("here..");
}
}
static void testSwitch() {
char grade = 'B';
switch (grade) {
case 'A':
System.out.println("优秀");
break;
case 'B':
case 'C':
System.out.println("良好");
break;
case 'D':
System.out.println("一般");
break;
default:
System.out.println("不及格");
break;
}
System.out.println("end...");
}
static void testFor() {
int[] a = {1,2,3,4};
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
static void testForEach() {
int[] a = {2,3,4,5};
for (int i : a) {
System.out.println(i);
}
}
public static void main(String[] args) {
// testForEach();
// testDoWhile();
// testContinue();
// testBreak();
testSwitch();
}
}