Program Flow Control
-Judging structure
-Select structure
-Cycle structure
Judging Structure
1. if (conditional expression)
{
Execute the statement;
}
2. if (conditional expression)
{
Execute the statement;
}
Else
{
Execute the statement;
}
3. if (conditional expression)
{
Execute the statement;
}
else if (conditional expression)
{
Execute the statement;
}
.....
Else
{
Execute the statement;
}
When three conditions are set, the value of the first condition is printed by default, and the preceding value is printed by default when two conditions are satisfied;
Execute the program from the top down.
1 Package cn.itcast.test;2 Public classIf_demo {//An example of if3 Public Static voidMain (string[] args) {4 intx=3;5 if(x>1)6 {7System. out. println ("A");8}Else if(x==3)9 {TenSystem. out. println ("B"); One } A Else - { -System. out. println ("C"); the }
- - intweek=2; - if(week==1) +System. out. println (week+"Representative Monday"); - if(week==2) +System. out. println (week+"Representative Tuesday");
A intMonth= -; at if(month==3|| month==4|| month==5) -System. out. println (month+"Spring"); - Else if(month==6|| month==7|| month==8) -System. out. println (month+"Summer"); - Else if(month==9|| month==Ten|| month== One) -System. out. println (month+"Autumn"); in Else if(month== A|| month==1|| month==2) -System. out. println (month+"Winter"); to Else +System. out. println ("could not find the corresponding season");
- the intMonth=1; * if(month>3& month<5) $System. out. println (month+"month corresponds to spring");Panax Notoginseng Else if(month>5& month<9) -System. out. println (month+"month corresponds to summer"); the Else if(month>9& month< A) +System. out. println (month+"month corresponds to autumn"); A Else if(month> A& month< the) theSystem. out. println (month+"month corresponds to winter"); + Else -System. out. println (month+"there is no corresponding season."); $ } $}
Select structure
switch (expression)
{
Case takes value 1:
Execute the statement;
Break
Case takes value 2:
Execute the statement;
Break
......
Default
Execute the statement;
break;//can be omitted
}
Switch statement features:
-only four types of statements are selected: Byte, short, int, char
-Case with default no order, first case executed, no matching case execution default
-End Switch statement: Encounter break, execute to switch statement end
-If the matching case or default does not have a corresponding break, the program will continue to execute the executed statement, knowing that a break or switch end is encountered
1 Package cn.itcast.test;2 Public classSwitch_demo {3 Public Static voidMain (string[] args) {4 intx=3;5 Switch(x+1)6 {7 Case 3:8System. out. println ("Hello");9 Break;Ten Case 5: OneSystem. out. println ("Hi there! "); A Break; - default: -System. out. println ("did not find the corresponding"); the }
- intNUM1 = -, num2 =4; - CharOPR ='*'; - Switch(OPR) + { - Case '+': +System. out. println (num1+num2); A Break; at Case '-': -System. out. println (num1-num2); - Break; - Case '*': -System. out. println ("num1*num2="+num1*num2); - Break; in Case '/': -System. out. println ("num1/num2="+num1/num2); to Break; + default: -System. out. println ("the corresponding operator could not be found"); the Break;//can omit * } $ }Panax Notoginseng}
Java day-04