当前位置:网站首页>Today, let's talk about the if branch statement of the selection structure
Today, let's talk about the if branch statement of the selection structure
2022-07-25 22:28:00 【Start】
1. Single branch if Selection structure :
Grammatical structure :
if( Conditions ){
// Code block
}
The law of execution :
If the condition is true, perform {} Block of code in , After executing the code block , Carry on {} Later code
If the condition is false, Then skip if Selection structure , perform {} Later code
Be careful :
No matter how simple or responsible the conditions are written , The final result should be Boolean , Either for true Either for false
The following is a small example to experience
public static void main(String[] args) {
// Chuangjie Scanner object
Scanner sc = new Scanner(System.in);
// Get results
System.out.println(" Enter grades ");
double score = sc.nextDouble();
// Conditions for obtaining rewards
if(score>90){
System.out.println(" good , You can get a Ferrari 3 RMB coupon ");
}
sc.close();// Turn off keyboard input
}
2. Multiple branches if Select institutions
Grammatical structure :
if( Conditions 1){
// Code block 1;
}else if( Conditions 2){
// Code block 2;
}else if( Conditions 3){
// Code block 3;
}
}else{
Code block n;
}
The law of execution :
Judge the condition 1, If the condition 1 by true, Then execute the code block 1, After executing the code block 1, End the whole thing if-else-if structure , perform if-else-if The code behind the structure
If the condition 1 by false, Then continue to judge the conditions 2, If the condition 2 by true, Then execute the code block 2, After executing the code block 2, End the whole thing if-else-if structure , perform if-else-if The code behind the structure
If the condition 2 position false, Then continue to judge the conditions 3, If the condition 3 position true, Then execute the code block 3, After executing the code block 3, End the whole thing if-else-if structure , perform if-else-if The code behind the structure
If the condition 3 by false, Continued to ......
If all the conditions are not met , execute else{} Block of code in n
Be careful :
(1) All conditions are Boolean
(2)else if There can be many
(3)else Yes, it can be omitted , When all conditions are not met , And there's no else When , Then the whole if-else-if The code in the structure will not be executed
public static void main(String[] args) {
/*
* achievement >90, good
* achievement >80 <90, good
* achievement <60, fail,
*/
Scanner sc = new Scanner(System.in);
System.out.println(" Come on, come on , Tell me how many marks you got :");
double score = sc.nextDouble();
if(score>90){
System.out.println(" Excellent ");
}
else if(score>80&&score<90){
System.out.println(" Click on the tongue , Not a coward ");
}
else{
System.out.println(" cut , Enter the factory !");
}
}3. Let me introduce you with a topic if Nesting of
Use if Select the structure to sort the three integers , Output in the order from small to large .
import java.util.Scanner;
// Use if Select the structure to sort the three integers , Output in the order from small to large
public class Demo03 {
public static void main(String[] args) {
// establish scanner object
Scanner sc = new Scanner(System.in);
// Let you do three things
System.out.println(" Enter three different integers ");
int num1=sc.nextInt();
int num2=sc.nextInt();
int num3=sc.nextInt();
if(num1>num2){
if(num2>num3){
System.out.println(num1+">"+num2+">"+num3);
}
else if(num2<num3){
System.out.println(num1+">"+num3+">"+num2);
}
else
{
System.out.println(num3+">"+num1+">"+num2);
}
}
else//num1<num2
{
if(num1>num3){
System.out.println(num2+">"+num1+">"+num3);
}
if(num3>num2){
System.out.println(num3+">"+num2+">"+num1);
}
else{
System.out.println(num2+">"+num3+">"+num1);
}
}
}
边栏推荐
- Recursive case -c
- Based on if nesting and function call
- PySpark数据分析基础:pyspark.sql.SparkSession类方法详解及操作+代码展示
- What is the difference between minor GC and full GC?
- Formal parameters, arguments and return values in functions
- SMART S7-200 PLC通道自由映射功能块(DO_Map)
- 启牛商学院和微淼商学院哪个靠谱?老师推荐的开户安全吗?
- 【集训DAY13】Backpack【动态规划】【贪心】
- Usage of in in SQL DQL query
- Mitsubishi FX PLC free port RS command realizes Modbus Communication
猜你喜欢

ML-Numpy

Pyspark data analysis basis: pyspark.sql.sparksession class method explanation and operation + code display

Xiaobai programmer day 8

IPv4地址已经完全耗尽,互联网还能正常运转,NAT是最大功臣!

Data governance under data platform

【集训DAY12】X equation 【高精度】【数学】
![[C syntax] void*](/img/34/b29b7bbf8eae9f1730352cac1301a4.png)
[C syntax] void*

C语言逆序打印字符串的两种方法

Visitor mode

xxl-job中 关于所有日志系统的源码的解读(一行一行源码解读)
随机推荐
mysql: error while loading shared libraries: libncurses.so. 5: cannot open shared object file: No suc
D3.js 学习
科大讯飞智能办公本Air电纸书阅读器,让我的工作生活更加健康
Wechat official account application development (I)
Get together for ten years, tell your story, millions of gifts are waiting for you
三菱FX PLC自由口RS指令实现MODBUS通讯
英文术语对应的解释
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No suc
ML-Numpy
torchvision
SQL基本语句 DQL select与提取 DML插入删除
Xiaobai programmer's fourth day
[database learning] redis parser & single thread & Model
Using simple scripts to process data in 3dslicer
谷歌分析UA怎么转最新版GA4最方便
Gan, why '𠮷 𠮷'.Length== 3 ??
D3.js learning
Common source code for ArcGIS development
ORM common requirements
Perform Jieba word segmentation on the required content and output EXCEL documents according to word frequency