当前位置:网站首页>Select structure if branch structure
Select structure if branch structure
2022-07-25 22:27:00 【Raise your eyes and look far away】
if Branching structure
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 complex the conditions are written , The final result should be Boolean , Either for true Either for false
// establish Scanner Class object
Scanner sc = new Scanner(System.in);
// get data
System.out.println(" Zhang Hao's score is :");
double score =sc.nextDouble();
// The conditions for Zhang Hao to get the reward
if (score>90){
System.out.println(" Congratulations on your success 1 Second mieba experience volume ");
}
System.out.println(" The program is finished ");
Double branch if-else structure :
Grammatical structure :
if( Conditions ){
// Code block 1
}else{
// Code block 2
}
The law of execution :
If the condition is true, Execute code block 1, After executing the code block 1 after , End the whole thing if-else structure , Keep going back if-else The code behind the structure
If the condition is false, Execute code block 2, After executing the code block 2 after , End the whole thing if-else structure , Keep going back if-else The code behind the structure
Be careful :
Conditions are Boolean
/*
* If Zhang Hao Java The test score is greater than 90 branch , The teacher rewarded him with one iPhone6s Otherwise, the teacher will punish him for squatting
*/
// establish Scanner Class object
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter Zhang Hao's Java achievement :");
double javaScore = sc.nextDouble();
// Zhang Hao Java The results meet the conditions , Reward iPhone6s
if(javaScore>90){// When conditions are met , After the execution condition {} Code inside
System.out.println(" Teacher reward iPhone6s");
}else{// When conditions don't hold , perform else Back {} Code in
System.out.println(" do a martial-art squat ");
}
System.out.println(" The program is finished ");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 junction 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 execution 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 execution 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
/*
* Evaluate the examination results of students
* achievement >=80 : good
* achievement >=60 : secondary
* achievement <60 : Bad
*
*/
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter your score :");
double score = sc.nextDouble();
if(score>=80){
System.out.println(" good ");
}else if(score>=60){
System.out.println(" secondary ");
}else{
System.out.println(" Bad ");
}
System.out.println(" The program is finished ");
边栏推荐
- 编译器引论
- Data quality: the core of data governance
- Mitsubishi FX PLC free port RS command realizes Modbus Communication
- Usage of in in SQL DQL query
- IPv4地址已经完全耗尽,互联网还能正常运转,NAT是最大功臣!
- Output Yang Hui triangle with two-dimensional array
- Leetcode 106. construct binary tree from middle order and post order traversal sequence
- 平台架构搭建
- IFLYTEK smart office book air e-book reader makes my work life healthier
- Xiaobai programmer's sixth day
猜你喜欢
随机推荐
xss-收集常用的代码
3day
Xiaobai programmer's seventh day
QML module not found
Five constraints and three paradigms
ThreadLocal summary (to be continued)
Common source code for ArcGIS development
3dslicer import cone beam CT image
VIM usage record
Win10 set up a flutter environment to step on the pit diary
scrapy无缝对接布隆过滤器
Wechat official account application development (I)
IPv4地址已经完全耗尽,互联网还能正常运转,NAT是最大功臣!
淦,为什么 '𠮷𠮷𠮷' .length !== 3 ??
H5幸运刮刮乐抽奖 免公众号+直运营
What is the difference between character constants and string constants?
【集训DAY15】Boring【树形DP】
(1) DDL, DML, DQL, DCL and common data types
ThreadLocal 总结(未完待续)
LabVIEW develops PCI-1680U dual port can card









