通过使用条件语句、循环语句可以实现流程的控制。

3.8.1 块作用域(Block Scope)

块(Block)就是由一对花括号包围起来的部分。他指定了一个变量的生存范围,与一个方法的操作范围。 Java中不允许在嵌套块中重复定义变量。

3.8.2 条件语句

if (condition) statement

{
statement 1
statement 2
. . .
}
if (yourSales >= target)
{
performance = "Satisfactory";
bonus = 100 + 0.01 * (yourSales - target);
}
else
{
performance = "Unsatisfactory";
bonus = 0;
}
if (yourSales >= 2 * target)
{
performance = "Excellent";
bonus = 1000;
}
else if (yourSales >= 1.5 * target)
{
performance = "Fine";
bonus = 500;
}
else if (yourSales >= target)
{
performance = "Satisfactory";
bonus = 100;
}
else
{
System.out.println("You're fired");
}

3.8.3  循环语句

1.while 循环

while (condition) statement

while (balance < goal)
{
balance += payment;
double interest = balance * interestRate / 100;
balance += interest;
years++;
}
System.out.println(years + " years.");

2.do-while 循环

do statement while (condition); 注意有;。

do
{
balance += payment;
double interest = balance * interestRate / 100;
balance += interest;
year++;
// print current balance
. . .
// ask if ready to retire and get input
. . .
}while (input.equals("N"));

3.8.4 固定次数循环(Determinate loop)

1.for循环

for (int i = 10; i > 0; i--)
System.out.println("Counting down . . . " + i);

3.8.5 多重选择 Switch 语句

Scanner in = new Scanner(System.in);
System.out.print("Select an option (1, 2, 3, 4) ");
int choice = in.nextInt();
switch (choice)
{
case 1:
. . .
break;
case 2:
. . .
break;
case 3:
. . .
break;
case 4:
. . .
break;
default:
// bad input
. . .
break;
}

Case 标签(即Switch后跟的变量类型)可以是:

•  char , byte , short ,  int 及其包装类的常量表达式
• 枚举类型
• 从Java SE 7开始支持string 字面值。

String input = . . .;
switch (input.toLowerCase())
{
case "yes": // OK since Java SE 7
. . .
break;
. . .
}

3.8.6 跳出循环

1.虽然Java保留的goto关键字,但是不推荐使用goto语句。一般使用break跳出循环。Java支持带标签的break语句,用于跳出多重循环。相当于给每个循环起个名字,跳出时可以指定跳出哪个循环。

label:
{
. . .
if (condition) break label; // exits block
. . .
}
// jumps here when the break statement executes

示例如下:

while (years <= 100)
{
balance += payment;
double interest = balance * interestRate / 100;
balance += interest;
if (balance >= goal) break;
years++;
}
Scanner in = new Scanner(System.in);
int n;
read_data:
while (. . .) // this loop statement is tagged with the label
{
. . .
for (. . .) // this inner loop is not labeled
{
System.out.print("Enter a number >= 0: ");
n = in.nextInt();
if (n < 0) // should never happen—can't go on
break read_data;
// break out of read_data loop
. . .
}
}

2.continue 语句

跳出本次循环

CoreJavaE10V1P3.8 第3章 Java的基本编程结构-3.8 控制流程(Control Flow)的更多相关文章

  1. CoreJavaE10V1P3.5 第3章 Java的基本编程结构-3.5 操作符

    最基本的操作为赋值操作,= 即赋值操作符 基本的算术操作为加.减.乘.除取模.除取余数,其对应操作符为 +.-.*./.% 算术操作与赋值操作联合衍生为:+=:-=:*=:/=:%=: 由于处理器硬件 ...

  2. CoreJavaE10V1P3.10 第3章 Java的基本编程结构-3.10 数组(Arrays)

    数组是存储同一类型数据的数据结构 数组的声明与初始化 int[] a; int a[]; int[] a = new int[100]; int[] a = new int[100]; for (in ...

  3. CoreJavaE10V1P3.9 第3章 Java的基本编程结构-3.9 大数值(Big Numbers)

    如果基本的整型与浮点型不能满足需求,可以使用java.Math包提供的 BigInteger 和 BigDecimal 两个类,这两个类可以存储任意长度的数, BigInteger 实现的任意精度整数 ...

  4. CoreJavaE10V1P3.7 第3章 Java的基本编程结构-3.7 输入输出(Input ,Output)

    3.7.1 读取输入 Scanner in = new Scanner(System.in); System.out.print("What is your name? "); S ...

  5. CoreJavaE10V1P3.6 第3章 Java的基本编程结构-3.6 字符串 String

    String类(java.lang.String)就是Unicode字符序列,例如:"Java\u2122" 3.6.1 Substring 提取子串 String greetin ...

  6. CoreJavaE10V1P3.4 第3章 Java的基本编程结构-3.4 变量

    1.在Java中,每一个变量都必须有一个类型,在变量声明是,类型必须在变量名之前.示例如下: double salary; int vacationDays; long earthPopulation ...

  7. CoreJavaE10V1P3.3 第3章 Java的基本编程结构-3.3 数据类型

    3.3 数据类型 这里所说的数据类型是指 Java的8中基本数据类型,是原生就存在的. 不同进制数的字面值表示方法 进制 字面值表示方法 例子 是否默认 JDK版本支持 2进制 0b或0B前缀(每4位 ...

  8. CoreJavaE10V1P3.2 第3章 Java的基本编程结构-3.2 注释

    3.2 注释 1. //形式注释 System.out.println("We will not use 'Hello, World!'"); // is this too cut ...

  9. CoreJavaE10V1P3.1 第3章 Java的基本编程结构-3.1 Java 最简程序

    3.1Java最简程序 FirstSample.java public class FirstSample { public static void main(String[] args) { Sys ...

随机推荐

  1. Akka入门实例

    Akka入门实例 Akka 是一个用 Scala 编写的库,用于简化编写容错的.高可伸缩性的 Java 和 Scala 的 Actor 模型应用. Actor模型并非什么新鲜事物,它由Carl Hew ...

  2. 字符串子串查找strstr

    问题: 函数名: strstr 函数原型:char *strstr(const char *str1, const char *str2); 语法:* strstr(str1,str2) str1: ...

  3. ASP.NET MVC 用户登录Login

    ASP.NET MVC 用户登录Login一.先来看个框架例子:(这个是网上收集到的)  第一步:创建一个类库ClassLibrary831.            第二步:编写一个类实现IHttpM ...

  4. 我的Pandas应用场景(2)

    上文交代了一些啰嗦事,本文开始,就要来点实际的了. 先来一个比较简单的场景: Given:一个包括N(极其复杂,这里取3个)个列的DataFrame:df,df包括index: And:对df所有列元 ...

  5. RILC

    RILC RIL层的作用大体上就是将上层的命令转换成相应的AT指令,控制modem工作.生产modem的厂家有很多:Qualcomm, STE, Infineon... 不同的厂家都有各自的特点,当然 ...

  6. poj 3897 Maze Stretching 二分+A*搜索

    题意,给你一个l,和一个地图,让你从起点走到终点,使得路程刚好等于l. 你可以选择一个系数,把纵向的地图拉伸或收缩,比如你选择系数0.5,也就是说现在上下走一步消耗0.5的距离,如果选择系数3,也就是 ...

  7. 内存错误:CRT detected that the application wrote to memory after end of heap buffer

    今天调试测试代码时,发现在用完了new出来的内存buf后,在执行delete时报错了,具体信息为: HEAP_CORRUPTION_DETECTED: after Normal block(#908) ...

  8. java基础知识拾遗(二)

    1.finally public static int func (){ try{ return 1; }catch (Exception e){ return 2; }finally { retur ...

  9. 结构-行为-样式-angularJs笔记

    0.关于Ng-app   通过ngApp指令来引导Angularjs应用是一种简洁的方式 ,适合大多数情况.在高级开发中,例如使用脚本装载应用,您也可以使用Bootstrap手动引导AngularJs ...

  10. Python第一天——入门Python(1)数据定义

    数据类型: 什么是数据? 在计算机科学中,数据是指所有能输入到计算机并被计算机程序处理的符号的介质的总称,是用于输入电子计算机进行处理,具有一定意义的数字字母.符号和模拟量等的统称.现在计算机存储和处 ...