java学习系列(五)流程语句

1.1. 基本语句

1.1.1. if语句

1.格式一:

if ( 条件表达式 ) {

执行语句;

}

例子:

注意:

如果if语句中只有一条语句,那么可以不写大括号。

2.格式二:

if ( 条件表达式 ) {

执行语句;

} else {

执行语句;

}

例子1:

例子2:

注意:

三元运算符就是if else语句的简写格式。

例如:b = a > 1 ? 100 : 200;就可以实现和上面同样的功能。

简写格式什么时候用?

当if else运算后,有一个具体的结果时,可以简化写成三元运算符。

3.格式三:

if(条件表达式) {

执行语句;

} else if (条件表达式) {

执行语句;

}

……

else {

执行语句;

}

例子:

if语句特点:

1、每一种格式都是单条语句。

2、第二种格式与三元运算符的区别:三元运算符运算完要有值出现。好处是:可以写在其他表达式中。

3、条件表达式无论写成什么样子,只看最终的结构是否是true或者false。

练习1:

IfDemo.java

class IfDemo {

public static void main(String[] args) {

int x = 1;

if (x > 1) {

System.out.println("yes");

} else {

System.out.println("a");

}

}

}

/*

if else 结构 简写格式: 变量 = (条件表达式)?表达式1:表达式2;

三元运算符:

好处:可以简化if else代码。

弊端:因为是一个运算符,所以运算完必须要有一个结果。

*/

int a = 9, b;

b = (a > 1) ? 100 : 200;

if (a > 1)

b = 100;

else

b = 200;

int n = 3;

if (n > 1)

System.out.println("a");

else if (n > 2)

System.out.println("b");

else if (n > 3)

System.out.println("c");

else

System.out.println("d");

/*

* if(n>1) System.out.println("a");

* if(n>2) System.out.println("b");

* if(n>3) System.out.println("c");

* else System.out.println("d");

*/

System.out.println("over");

结果:

练习2:

IfDemo.java

1.1.2. 选择结构,switch语句

格式:

switch(表达式)

{

case 取值1:

执行语句;

break;

case 取值2:

执行语句;

break;

……

default:

执行语句;

break;

}

switch语句特点:

1、switch语句选择的类型只有四种:byte,short,int,char。

2、case与default没有顺序。先执行第一个case,没有匹配的case执行default。

3、结束switch语句的两种情况:①遇到break,②执行到switch语句结束。

4、如果匹配的case或者default没有对应的break,那么程序会继续向下执行,运行可以执行的语句,直到遇到break或者switch结尾结束。

5、进入switch语句后,执行顺序是先执行case,然后从上到下,最后再执行default。即使default放在case上面,执行顺序也不变。

例子1:

SwitchDemo.java

class SwitchDemo {

public static void main(String[] args) {

int x = 3;

switch (x) {

default:

System.out.println("d");

break;

case 4:

System.out.println("a");

break;

case 2:

System.out.println("b");

break;

case 3:

System.out.println("c");

break;

}

}

}

结果:

c

例子2:

SwitchDemo.java

class SwitchDemo {

public static void main(String[] args) {

int a = 4, b = 2;

char opr = '-';

switch (opr) {

case '+':

System.out.println(a + b);

break;

case '-':

System.out.println(a - b);

break;

case '*':

System.out.println(a * b);

break;

case '/':

System.out.println(a / b);

break;

default:

System.out.println("无法运算,号不支持");

break;

}

}

}

结果:

例子3:部分case和default中没有break语句的情况。

SwitchDemo.java

class SwitchDemo {

public static void main(String[] args) {

int x = 3;

switch (x) {

case 4:

System.out.println("a");

break;

case 2:

System.out.println("b");

case 3:

System.out.println("c");

default:

System.out.println("d");

}

}

}

结果:

例子4:

SwitchDemo.java

class SwitchDemo {

public static void main(String[] args) {

/*

* 用户输入的数据对应的星期。

*/

int week = 4;

switch (week) {

case 1:

System.out.println(week + "对应的是星一");

break;

case 2:

System.out.println(week + "对应的是星二");

break;

case 3:

System.out.println(week + "对应的是星三");

break;

case 4:

System.out.println(week + "对应的是星四");

break;

case 5:

System.out.println(week + "对应的是星五");

break;

case 6:

System.out.println(week + "对应的是星六");

break;

case 7:

System.out.println(week + "对应的是星日");

break;

default:

System.out.println(week + "没有对应的期");

break;

}

}

}

结果:

例子5:多个case同一种处理方式的情况。

SwitchDemo.java

class SwitchDemo {

public static void main(String[] args) {

int month = 3;

switch (month) {

case 3:

case 4:

case 5:

System.out.println(month + "月对应的春季");

case 6:

case 7:

case 8:

System.out.println(month + "月对应的夏季");

case 9:

case 10:

case 11:

System.out.println(month + "月对应的秋季");

case 12:

case 1:

case 2:

System.out.println(month + "月对应的冬季");

default:

System.out.println(month + "没有对应季节");

}

}

}

结果:

1.1.3. if和switch的应用:

if:

1、对具体的值进行判断。

2、对区间判断。

3、对运算结果是boolean类型的表达式进行判断。

switch:

1、对具体的值进行判断。

2、值的个数通常是固定的。

对于几个固定的值判断,建议使用switch语句,因为switch语句会将具体的答案都加载进内存,效率相对高。

1.1.4. 循环结构

1.1.4.1. while循环

关键词:while,do while,for。

while语句格式:

while( 条件表达式 ) {

执行语句;

}

例子:

WhileDemo.java

class WhileDemo {

public static void main(String[] args) {

int x = 1;

while (x < 3) {

System.out.println("x = " + x);

x++;

}

}

}

结果:

注意:

一定要注意不要写while(x < 3);这样的语句,后面的分号就是循环体,代表不执行任何语句,这个循环就成了一个死循环。

do while语句格式:

do {

执行语句;

} while( 条件表达式 );

例子:

DoWhileDemo.java

class DoWhileDemo {

public static void main(String[] args) {

int x = 1;

do {

System.out.println("x = " + x);

x++;

} while (x < 3);

}

}

结果:

while和do while的区别

do while语句的特点:无论条件是否满足,循环体至少执行一次。

while如果条件不满足,循环体一次都不会执行。

练习1:获取1到10,10个数字的和。

累加思想

WhileDemo.java

class WhileDemo {

public static void main(String[] args) {

int x = 1;// 记录参与加法的数据。

int sum = 0;// 记录住每一次的和

while (x <= 10) {

sum += x;

x++;

}

System.out.println("sum = " + sum);

}

}

结果:

练习2:1~100之间,6的倍数出现的次数。

计数器思想

WhileDemo.java

class WhileDemo {

public static void main(String[] args) {

int x = 1;

int count = 0;

while (x <= 100) {

if (x % 6 == 0) {

count++;

}

x++;

}

System.out.println("count = " + count);

}

}

结果:

1.1.4.2. for循环

For语句格式:

for(初始化表达式;循环条件表达式;循环后的操作表达式)

{

执行语句;(循环体)

}

for里面的三个表达式运行的顺序,初始化表达式只读一次,判断循环条件,为真就执行循环体,然后再执行循环后的操作表达式,接着继续判断循环条件,重复找个过程,直到条件不满足为止。

ForDemo.java

class ForDemo {

public static void main(String[] args) {

for (int x = 0; x < 3; x++) {

System.out.println("x = " + x);

}

}

}

结果:

注意:for循环的初始化表达式、循环后的操作表达式可以是多个表达式,通过逗号分隔。

例如:

for(int a = 1,b =2; a < 2 & b < 3; a++,b++){

}

练习1:

ForDemo.java

class forDemo {

public static void main(String[] args) {

int x = 1;

for (System.out.println("a"); x < 3; System.out.println("c")) {

System.out.println("d");

x++;

}

}

}

结果:

1.1.4.3. 循环注意点

1、while与for可以互换,区别在于for为了循环而定义的变量在for循环结束就在内存中释放。而while循环使用的变量在循环结束后还可以继续使用。

下面通过两个例子对比:

WhileDemo.java

class whileDemo {

public static void main(String[] args) {

// 打印1~10十个数字

int x = 1;

while (x < 10) {

System.out.println("x = " + x);

x++;

}

System.out.println("x = " + x);

}

}

结果:

ForDemo.java

class ForDemo {

public static void main(String[] args) {

for (int y = 1; y < 10; y++) {

System.out.println("y = " + y);

}

System.out.println("y = " + y);

}

}

结果:

2、最简单无限循环格式:while(true) , for(;;),无限循环存在的原因是并不知道循环多少次,而是根据某些条件,来控制循环。

3、在使用循环时候,一定要明确哪些语句需要参与循环,哪些不需要。循环通常情况下,需要定义条件,需要控制次数。

1.1.4.4. 循环嵌套

For循环嵌套例子:

例子1:

ForForDemo.java

class ForForDemo {

public static void main(String[] args) {

for (int x = 0; x < 3; x++) {

for (int y = 0; y < 4; y++) {

System.out.println("ok");

}

}

}

}

结果:

例子2:

ForForDemo.java

class ForForDemo {

public static void main(String[] args) {

for (int x = 0; x < 4; x++) { // 外循环控制的是行数

for (int y = 0; y < 5; y++) {

System.out.print("*");

}

System.out.println();

}

}

}

结果:

练习1:

打印出下格式的内容:

*****

****

***

**

*

答案1:

ForForDemo.java

class ForForDemo {

public static void main(String[] args) {

int z = 5;

for (int x = 0; x <= 4; x++) {

for (int y = 1; y <= 5 - x; y++) {

System.out.print("*");

}

System.out.println();

z--;

}

}

}

结果:

答案2:

class ForForDemo {

public static void main(String[] args) {

for (int x = 1; x <= 5; x++) {

for (int y = x; y <= 5; y++) {

System.out.print("*");

}

System.out.println();

}

}

}

结果:

练习2:

打印出下格式的内容:

*

**

***

****

*****

答案:

ForForDemo.java

class ForForDemo {

public static void main(String[] args) {

for (int x = 1; x <= 5; x++) {

for (int y = 1; y <= x; y++) {

System.out.print("*");

}

System.out.println();

}

}

}

结果:

练习3:

打印出下格式的内容:

54321

5432

543

54

5

答案:

ForForDemo.java

class ForForDemo {

public static void main(String[] args) {

for (int x = 1; x <= 5; x++) {

for (int y = 5; y >= x; y--) {

System.out.print(y);

}

System.out.println();

}

}

}

结果:

练习4:

打印出下格式的内容:

1

22

333

4444

55555

答案:

ForForDemo.java

class ForForDemo {

public static void main(String[] args) {

for (int x = 1; x <= 5; x++) {

for (int y = 1; y <= x; y++) {

System.out.print(x);

}

System.out.println();

}

}

}

结果:

练习5:

打印九九乘法表成如下形式:

1*1=1

1*2=2 2*2=4

1*3=3 2*3=6 3*3=9

...

答案:

ForForDemo.java

class ForForDemo {

public static void main(String[] args) {

for (int x = 1; x <= 9; x++) {

for (int y = 1; y <= x; y++) {

System.out.print(y + "*" + x + "=" + (x * y) + "\t");

}

System.out.println();

}

}

}

结果:

注意:

代码中的"\t"是一个转义字符,也就是制表符。还有其他的一些转义字符:

\n:回车,\b:退格,\r:按下回车符。

windows系统中回车符其实是由两个转义字符组成的:\r\n。

linux中回车符是\n。

例子:

//打印"hello world":

System.out.println("\"hello word\"");

//打印\hello world\:

System.out.println("\\hello word\\");

练习6:

打印出下格式的内容:

* * * * *

* * * *

* * *

* *

*

答案:

ForForDemo.java

class ForForDemo {

public static void main(String[] args) {

for (int x = 1; x <= 5; x++) {

// 首先打印出*前面的空格

for (int y = 1; y < x; y++) {

System.out.print(" ");

}

// 再打印出*

for (int z = x; z <= 5; z++) {

System.out.print("* ");

}

System.out.println();

}

}

}

结果:

1.1.4.5. break(跳出)与continue(继续)

break语句:

应用范围:选择结构和循环结构。

例子1:

BreakDemo.java

class BreakDemo {

public static void main(String[] args) {

for (int x = 0; x < 3; x++) {

System.out.println("x = " + x);

break;

}

}

}

结果:

例子2:

BreakDemo.java

class BreakDemo {

public static void main(String[] args) {

for (int x = 0; x < 3; x++) {

if (x == 1)

break;

System.out.println("x = " + x);

}

}

}

结果:

例子3:

class BreakDemo {

public static void main(String[] args) {

for (int x = 0; x < 3; x++) {

for (int y = 0; y < 4; y++) {

System.out.println("x = " + x);

break;

}

}

}

}

结果:

continue语句:

应用范围:循环结构。

continue语句是结束本次循环继续下次循环。

例子:

ContinueDemo.java

class ContinueDemo {

public static void main(String[] args) {

for (int x = 0; x < 11; x++) {

if (x % 2 == 0)

continue;

System.out.println("x = " + x);

}

}

}

结果:

注意:

1、这两个语句离开应用范围,存在是没有意义的。

2、这个两个语句单独存在下面都不可以有语句,因为执行不到。

例子1:

BreakDemo.java

class BreakDemo {

public static void main(String[] args) {

for (int x = 0; x < 3; x++) {

break;

System.out.println("x = " + x);

}

}

}

结果:

例子2:

ContinueDemo.java

class ContinueDemo {

public static void main(String[] args) {

for (int x = 0; x < 3; x++) {

continue;

System.out.println("x = " + x);

}

}

}

结果:

3、标号的出现,可以让这两个语句作用于指定的范围。

例子1:

BreakDemo.java

class BreakDemo {

public static void main(String[] args) {

out: for (int x = 0; x < 3; x++) {

in: for (int y = 0; y < 4; y++) {

System.out.println("x = " + x);

break out;

}

}

}

}

结果:

例子2:

ContinueDemo.java

class ContinueDemo {

public static void main(String[] args) {

out: for (int x = 0; x < 3; x++) {

in: for (int y = 0; y < 4; y++) {

System.out.println("x = " + x);

continue out;

}

}

}

}

结果:

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,809评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,189评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,290评论 0 359
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,399评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,425评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,116评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,710评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,629评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,155评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,261评论 3 339
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,399评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,068评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,758评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,252评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,381评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,747评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,402评论 2 358

推荐阅读更多精彩内容