Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize logical Control in Java

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

这篇文章主要介绍了Java中如何实现逻辑控制,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

1 顺序结构

顺序结构比较简单,就是代码一行一行的执行,本节之前写的所有代码都是顺序结构。

例如:

public static void main(String[] args) { int a=10; System.out.println(a); System.out.println("hhhh"); double f=10.732; System.out.println(f);2 分支结构2.1 if语句基本语法形式1:

if(布尔表达式){

//条件满足时的要执行的语句

}

示例代码1:public static void main(String[] args) { int a=10; int b=20; if(ab条件不成立不执行该语句 }

基本语法形式2:

if(布尔表达式){

//条件成立是执行的语句

}

else{

//条件不成立时执行的语句

}

示例代码2:public static void main(String[] args) { int a=10; int b=20; if(a>b){ System.out.println(a);//a是10 b是20 a>b条件不成立不执行该语句 } else{ System.out.println(b);//a是10 b是20 a>b条件成立执行该语句 } }

基本语法形式3:

if(布尔表达式1){

//表达式1为true,执行的代码

}

else if(布尔表达式2){

//表达式2为true,执行的代码

}

else{

//表达式1和表达式2都为falses,执行的代码

}

示例代码3:public static void main(String[] args) { int a=10; int b=10; if(a>b){ System.out.println(a);//表达式1不成立,该语句不执行 } else if(b>a){ System.out.println(b);//表达式2不成立。该语句不执行 } else{ System.out.print("a=b=");//表达式1和表达式2都不成立,该语句执行 System.out.println(a);//注意:表达式成立时执行的语句可以是多条 } }

Note 1:

If statements can be nested, when the expression in if (Boolean expression) is true, the corresponding statement block will be executed, and if statements can also be used in this corresponding statement block.

For example, find the largest of three numbers.

public static void main(String[] args) { int a=50; int b=70; int c=20; System.out.print("Maximum number is"); if(a>b){ //a>b does not hold true, so do not execute the change statement block if(c>a){ System.out.println(c); } else{ System.out.println(a); } } else{ //a>b does not hold true, so execute this statement block if(c>b){ System.out.println(c);//c>b does not hold, do not execute the statement } else{ System.out.println(b);//c>b does not hold, so execute the change statement } } }

Note 2:

When the Boolean expression in an if statement is true, the statement block corresponding to if is executed. If "{}" is not added, the following statement is executed

For example:

public static void main(String[] args) { int a=10; int b=20; if(a>b) System.out.println(a);//If the if condition does not hold, this statement will not be executed System.out.println(b);//Although the if condition does not hold, this statement has exceeded the if //statement block, so this statement will also be executed }

2.2 switch statement basic syntax format

switch (integer| enumeration| character| String){

Case 1 : {

execute the sentence when the content is satisfied;

[ break ;]

}

Case 2 : {

execute the sentence when the content is satisfied;

[ break ;]

}

...

default :{

when the content is not satisfied, execute the sentence;

[ break ;]

}

public static void main(String[] args) { int day=5; switch (day){ case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; default: System.out.println("Input Error"); break; } }

Depending on the value in the switch, the corresponding case statement is executed. A break ends the case statement. If the value in switch does not match the case, the statement in default is executed. We recommend that a switch statement always include default.

3 Loop Structure 3.1 While Loop Basic Syntax Format:

while (Boolean expression){

//the statement block to execute;

}

When the Boolean expression is true, the loop executes the block of statements to be executed, otherwise the loop ends.

Example code: Output 1~10 public static void main(String[] args) { int i=1; while(i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report