In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand the control flow statement in Java". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the control flow statement in Java".
Catalogue
Preface
If-then
If-then-else
Switch
Use String
While
Do-while
For
Break
Continue
Return
Summary
Preface
Flow control statements are statements used to control the execution order of statements in the program, which can be combined into small logic modules that can complete certain functions.
Control statements are divided into three categories: order, selection, and loop.
Sequential structure: represents the logic of "execute a before b".
Select structure: stands for "if..., then..." The logic of.
Loop structure: represents "if..., then repeat the execution of..." The logic of.
In fact, any software or program, from an exercise to an operating system, is essentially made up of "variables, selection statements, and loop statements".
These three basic logical structures support each other, and they together constitute the basic structure of the algorithm. No matter how complex the logical structure is, it can be expressed through them.
If-then
It tells you to execute specific code only if the if is followed by a true.
Void applyBrakes () {/ / the "if" clause: bicycle must be moving if (isMoving) {/ / the "then" clause: decrease current speed currentSpeed--;}}
If if is followed by false, skip to the if-then statement. Statements can be omitted from parentheses, but are not recommended in coding specifications, such as:
Void applyBrakes () {/ / same as above, but without braces if (isMoving) currentSpeed--;} if-then-else
This statement provides a second execution path when the if is followed by false.
Void applyBrakes () {if (isMoving) {currentSpeed--;} else {System.err.println ("The bicycle has already stopped!");}}
Here is a complete example:
Class IfElseDemo {/ * @ param args * / public static void main (String [] args) {int testscore = 76; char grade; if (testscore > = 90) {grade = 'Agar;} else if (testscore > = 80) {grade =' baked;} else if (testscore > = 70) {grade ='C' } else if (testscore > = 60) {grade = 'Downs;} else {grade =' Fitch;} System.out.println ("Grade =" + grade);}}
The output is: Grade = C
Switch
Switch statements can have many possible execution paths. You can use byte, short, char, and int basic data types, or enumerated types (enumerated types), String, and a small number of primitive types of wrapper classes Character, Byte, Short, and Integer.
Here is an example of SwitchDemo:
Class SwitchDemo {/ * @ param args * / public static void main (String [] args) {int month = 8; String monthString; switch (month) {case 1: monthString = "January"; break; case 2: monthString = "February"; break Case 3: monthString = "March"; break; case 4: monthString = "April"; break; case 5: monthString = "May"; break; case 6: monthString = "June"; break; case 7: monthString = "July" Break; case 8: monthString = "August"; break; case 9: monthString = "September"; break; case 10: monthString = "October"; break; case 11: monthString = "November"; break Case 12: monthString = "December"; break; default: monthString = "Invalid month"; break;} System.out.println (monthString);}}
The break statement is designed to prevent fall through.
Class SwitchDemoFallThrough {/ * @ param args * / public static void main (String [] args) {java.util.ArrayList futureMonths = new java.util.ArrayList (); int month = 8; switch (month) {case 1: futureMonths.add ("January"); case 2: futureMonths.add ("February") Case 3: futureMonths.add ("March"); case 4: futureMonths.add ("April"); case 5: futureMonths.add ("May"); case 6: futureMonths.add ("June"); case 7: futureMonths.add ("July"); case 8: futureMonths.add ("August") Case 9: futureMonths.add ("September"); case 10: futureMonths.add ("October"); case 11: futureMonths.add ("November"); case 12: futureMonths.add ("December"); break; default: break } if (futureMonths.isEmpty ()) {System.out.println ("Invalid month number");} else {for (String monthName: futureMonths) {System.out.println (monthName);}
The output is:
August
September
October
November
December
Technically, the last break is not necessary because the process jumps out of the switch statement. However, break is still recommended, which makes it easier to modify the code and prevent errors. Default handles all cases of unknown values.
The following example shows the case of multiple case in a local area.
Class SwitchDemo2 {/ * @ param args * / public static void main (String [] args) {int month = 2; int year = 2000; int numDays = 0; switch (month) {case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31 Break; case 4: case 6: case 9: case 11: numDays = 30; break; case 2: if (year% 4 = = 0) & &! (year% 100 = = 0)) | | (year% 400 = = 0) numDays = 29; else numDays = 28 Break; default: System.out.println ("Invalid month."); break;} System.out.println ("Number of Days =" + numDays);}}
The output is: Number of Days = 29
Use String
Starting with Java SE 7, you can use String in the switch statement. Here is an example
Class StringSwitchDemo {public static int getMonthNumber (String month) {int monthNumber = 0; if (month = = null) {return monthNumber;} switch (month.toLowerCase ()) {case "january": monthNumber = 1; break; case "february": monthNumber = 2; break Case "march": monthNumber = 3; break; case "april": monthNumber = 4; break; case "may": monthNumber = 5; break; case "june": monthNumber = 6; break; case "july": monthNumber = 7 Break; case "august": monthNumber = 8; break; case "september": monthNumber = 9; break; case "october": monthNumber = 10; break; case "november": monthNumber = 11; break Case "december": monthNumber = 12; break; default: monthNumber = 0; break;} return monthNumber;} public static void main (String [] args) {String month = "August"; int returnedMonthNumber = StringSwitchDemo.getMonthNumber (month) If (returnedMonthNumber = = 0) {System.out.println ("Invalid month");} else {System.out.println (returnedMonthNumber);}
Output is: 8
Note: null cannot be in an switch statement expression.
While
The while statement executes the statement block when it determines that the condition is true. The syntax is as follows:
While (expression) {statement (s)}
The expression evaluated by the while statement must return a boolean value. If the expression evaluates to a true,while statement, all statements of the while block are executed. The while statement continues to test the expression and then executes its block until the expression evaluates to false. A complete example:
Class WhileDemo {/ * @ param args * / public static void main (String [] args) {int count = 1; while (count < 11) {System.out.println ("Count is:" + count); count++;}
Implement an infinite loop with a while statement:
While (true) {/ / your code goes here} do-while
The syntax is as follows:
Do {statement (s)} while (expression)
The difference between the do-while statement and the while statement is that do-while evaluates its expression at the bottom of the loop, not at the top. Therefore, the statement of the do block will be executed at least once, as shown in the DoWhileDemo program:
Class DoWhileDemo {/ * @ param args * / public static void main (String [] args) {int count = 1; do {System.out.println ("Count is:" + count); count++;} while (count < 11);}}
The output is:
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10
For
The for statement provides a compact way to traverse a range value. Programs are often referred to as "for loops" because they loop over and over again until certain conditions are met. The usual form of the for statement is as follows:
For (initialization; termination; increment) {statement (s)}
Make the for statement pay attention to:
Initialization initializes the loop; it executes once as the start of the loop.
When termination evaluates to false, the loop ends.
Increment executes at each iteration of the loop; the expression can accept increments or decrements
Class ForDemo {/ * * @ param args * / public static void main (String [] args) {for (int iTunes 1; 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.