Java entry-level (5): control process and java entry-level control process
In Java, the control flow is determined by the Condition Statement and loop structure. In this article, the control flow consists of four parts: block scope, Condition Statement, loop structure, and interrupt loop.
I. Block Scope
Block, also known as compound statements, refers to several Java statements enclosed by a pair of braces. The block determines the scope of the variable. A block can be nested with multiple blocks.
Ii. conditional statements
If it is determined that there are more than three layers, we recommend that you split them into multiple layers for better clarity.
Package javalearningday04;/*** Condition Statement * @ author Xiaochuan 94 * @ date July 22, January 31, 2018 */public class IfElseDemo {public static void main (String [] args) {testMethod (78);}/*** score judgment */public static void testMethod (int score) {if (score <0) {System. out. println ("enter the correct score! ");} Else {if (score >=90 & score <= 100) {System. out. println ("excellent");} else if (score> = 80) {System. out. println ("excellent performance");} else if (score> = 70) {System. out. println ("average score");} else if (score> = 60) {System. out. println ("pass");} else {System. out. println ("fail ");}}}}
Iii. Loop Structure
3.1 for Loop
Package javalearningday04;/*** for loop usage * @ author Xiaochuan 94 * @ date July 22, January 31, 2018 */public class ForDemo {public static void main (String [] args) {testMethod (); testMethod2 ();}/*** calculates, 3 ..., and */public static void testMethod () {int sum = 0; for (int I = 1; I <100; I ++) {sum + = I ;} system. out. println (sum); // sum = 5050} public static void testMethod2 () {String [] strArray = {"James", "Pony", "Tom "}; // foreach loop traversal for (String str: strArray) {System. out. println (str); // print Xiao Ming Xiao Wang in sequence} // The output results of both statements are the same for (int I = 0; I <strArray. length; I ++) {System. out. println (strArray [I]); // print Xiao Ming Xiaowang in sequence }}}
3.2 do {...} while () loop
Run the command first and then judge. A loop body is executed regardless of whether the while condition is true.
Package javalearningday04;/*** do {} while (); usage * @ author Ogawa 94 * @ date January 31, 2018 */public class DoWhileDemo {public static void main (String [] args) {testMethod (); testMethod2 ();} /*** calculate 1, 2, 3 ..., 100 and * are executed first, and then */public static void testMethod () {int I = 1; int sum = 0; do {sum + = I; I ++ ;} while (I <101); System. out. println (sum); // sum = 5050 System. out. println (I); // I = 101}/*** do {} while (); the loop structure will execute at least one loop body * When the while condition is invalid, you have executed a loop body */public static void testMethod2 () {int I = 1; int sum = 0; do {sum + = I; I ++ ;} while (I <1); System. out. println (sum); // sum = 1 System. out. println (I); // I = 2 }}
3.3 while () {...} Loop
First judge and then execute. The loop body is entered only when the condition is true.
Package javalearningday04;/*** usage of while () {} * @ author Ogawa 94 * @ date July 15, January 31, 2018 */public class WhileDemo {public static void main (String [] args) {testMethod (); testMethod2 ();}/*** calculates, 3 ..., 100 and * first judge, then execute */public static void testMethod () {int I = 1; int sum = 0; while (I <101) {sum + = I; I ++;} System. out. println (sum); // sum = 5050 System. out. println (I); // I = 101}/*** only the conditions in while () are true. * If the condition in while () is always true, it will enter an endless loop, causing very serious consequences to the program, * The cycle condition must be strictly determined during development! Avoid endless loops */public static void testMethod2 () {int I = 1; int sum = 0; while (I <1) {sum + = I; I ++ ;} system. out. println (sum); // sum = 0 System. out. println (I); // I = 1 }}
3.4 unique switch {}
Package javalearningday04;/*** switch {} usage * @ author Ogawa 94 * @ date July 22, January 31, 2018 */public class SwitchCaseDemo {public static void main (String [] args) {testMethodWithInt (3); testMethodWithByte (byte) 3); testMethodWithChar (char) 3); testMethodWithShort (short) 3); testMethodWithString ("MONDAY "); testMethodWithEnum (SIZE. MEDIUM); testSwitchWithoutBreak ("MONDAY");} // supports int public static void testMethodWithInt (int num) {switch (num) {case 0: System. out. println ("num equals 0"); break; default: System. out. println (num); break ;}// byte public static void testMethodWithByte (byte num) {switch (num) {case 0: System. out. println ("num equals 0"); break; default: System. out. println (num); break ;}// supports char public static void testMethodWithChar (char num) {switch (num) {case 0: System. out. println ("num equals 0"); break; default: System. out. println (num); break ;}// supports short public static void testMethodWithShort (short num) {switch (num) {case 0: System. out. println ("num equals 0"); break; default: System. out. println (num); break ;}// supports the public static void testMethodWithString (String str) {switch (str) {case "MONDAY": System. out. println ("Monday"); break; default: System. out. println (str); break;} public enum SIZE {// SMALL, // MEDIUM, // LARGE;} public SIZE size; public SwitchCaseDemo (SIZE size SIZE) {this. size = size;} // public static void testMethodWithEnum (SIZE size) {switch (size) {case SMALL: System. out. println ("small"); break; case MEDIUM: System. out. println ("medium"); break; case LARGE: System. out. println ("large"); break; default: System. out. println ("no other number"); break ;}// if you do not write a break statement, the public static void testSwitchWithoutBreak (String str) {switch (str) is executed in each case) {case "MONDAY": System. out. println ("eat steamed stuffed bun"); case "SUNDAY": System. out. println ("noodles"); default: System. out. println ("Porridge") ;}}// data of the long type is not supported./* public static void testMethodWithLong (long num) {switch (num) {case 0: System. out. println ("num equals 0"); break; default: System. out. println (num); break;} * // data of the double type is not supported/* public static void testMethodWithDouble (double num) {switch (num) {case 0: System. out. println ("num equals 0"); break; default: System. out. println (num); break ;}}*/}
Iv. Interrupt Loop
The interrupt loop requires two keywords: continue and break.
Continue refers to skipping the execution statement following it and entering the next new loop. The entire loop structure is still running and has not been terminated.
Break refers to the code after the entire loop structure is ended and executed.
Package javalearningday04;/*** use of continue and break * @ author Ogawa 94 * @ date July 22, January 31, 2018 */public class ContinueBreakDemo {public static void main (String [] args) {testContinue (); testBreak () ;}/ *** calculate the positive number and * continue in the array: After the continue statement is executed, the code after it is no longer executed, * ends this loop and enters the next loop. The entire loop structure is still being executed. */public static void testContinue () {int [] arr = {1, 2, 4, 4,-5, 6 }; int sum = 0; for (int I = 0; I <arr. length; I ++) {if (arr [I] <0) {// filter negative continue in the array;} else {sum + = arr [I];} // skip-5 and calculate the values of 1 + 2 + 3 + 4 + 6 and System. out. println (sum); // sum = 16}/*** break: after the break statement is executed, the subsequent code is no longer executed. * The entire loop structure is ended, */public static void testBreak () {int [] arr = {1, 2, 4,-5, 6}; int sum = 0; for (int I = 0; I <arr. length; I ++) {if (arr [I] <0) {break; // same as the above Code, replace continue with break} else {sum + = arr [I] ;}// only calculate the sum of 1 + 2 + 3 + 4 and System. out. println (sum); // sum = 10 }}
The above code has been uploaded to GitHub. The address is GitHub! If you have any questions, please leave a message below.
The article was first published on my personal public account:Happy book. I like to share my songs, movies, books, code, and deep-night meditation. Looking forward to your attention!
Enter the keyword"Java learning ebook", You can get 12 books related to Java learning. If the financial ability permits, please also support the author's genuine and paper books, which is not easy to create.