学习笔记(12):Java小白修炼手册-Java流程控制,编程逻辑加油站(一)

立即学习: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();
	}
	
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值