1. Sequential structure
Code is executed from top to bottom
2. Selection structure
Also known as branch structure, it will choose different code execution according to the results of execution, there are the following two forms:
if statement
switch statement
2.1 if statement
2.1.1 The first form of an if statement
Format:
If (relational expression){ Sentence Style; } Execution process: First, judge the relational expression to see if the result is true or false. If true, execute the body of the statement If it is false, the statement body is not executed.
Cases
1 package com._51doit.javase.day04; 2 3 import java.util.Scanner; 4 5 public class ifDemo1 { 6 public static void main(String[] args) { 7 Scanner sc = new Scanner(System.in); 8 System.out.println("Please enter the first integer:"); 9 int a = sc.nextInt(); 10 if(a > 3) { 11 System.out.println("Ha-ha"); 12 13 } 14 System.out.print("Hey"); 15 } 16 17 18 }
Implementation results:
Notes for if statements
Braces in if statements can be omitted, once omitted, only to the end of the first statement (only to the first semicolon)
Don't write like this when we develop it. If something goes wrong, it's hard to debug.
For example, the code is as follows
package com._51doit.javase.day04; public class IfDemoTest { public static void main(String[] args) { if(3==4) System.out.println("Ha-ha"); System.out.println("Ha-ha"); } }
The result of the above code running is ha ___________.
Interpretation: if can only be controlled to the end of the first statement (that is, the first semicolon after if)
If you change if(3==4) to if(3==4), add a semicolon, and the result is haha.
The semicolon ";" corresponds to {;} (it represents empty sentences, indicating that honey beans do not exist)
Practice:
Enter two integers from the keyboard to determine whether the two data are equal.
If so, output: equal,
No, the output is not equal.
1. Use if statement to complete
2. Complete with an if statement
public class IfTest { public static void main(String[] args) { //1. Enter two integers from the keyboard Scanner sc = new Scanner(System.in); System.out.println("Please enter the first integer"); int a = sc.nextInt(); System.out.println("Please enter the second integer"); int b = sc.nextInt(); //2. Use if Sentences make judgments /*if(a == b) { System.out.println("Equality "; } if(a != b) { System.out.println("Unequal "; }*/ //Using a single statement String re ="Equal"; if(a!=b) { re = "Inequality"; } System.out.println(re); } }
2.1.2 The second form of if statement
format
If (Relational Expressions){ Style 1; }else { Sentence Body 2; } Execution process
First, judge the relational expression to see if the result is true or false.
If true, execute statement body 1
If false, execute statement body 2
Case:
/** Keyboard input an integer to determine whether the data is odd or even */ package com._51doit.javase.day04; import java.util.Scanner; public class IfDemo2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter a data"); int a = sc.nextInt(); if(a%2==0) { System.out.println("Even numbers"); }else { System.out.println("Odd number"); } } }
2.1.3 Third form of if statement
format
If (relational expression 1){ Style 1; } Other if (relational expression 2){ Sentence Body 2; } ... else { Statement body n+1; } Technological process: First, judge relational expression 1 to see if the result is true or false. If true, execute statement body 1 If it's false, go ahead and judge relational expression 2 to see if the result is true or false.
Execute statement body 2 if true
If it's false, continue to judge the relational expression. See if the result is true or false
If no relational expression is true, the statement body n+1 is executed.
case
public class IfDemo4 { //Enter an integer from the keyboard,Judgment is positive,negative,Or 0 public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter an integer"); int a = sc.nextInt(); if(a>0) { System.out.println("Positive number"); }else if(a==0) { System.out.println("It's 0."); }else { System.out.println("negative"); } } }
Exercise
1. Keyboard input x value, calculate y and output.
x>=3 y = 2x + 1;
-1<=x<1 y = 2x;
x<-1 y = 2x – 1;
The code is as follows:
1 package com._51doit.javase.day04; 2 import java.util.Scanner; 3 public class IfTest2 { 4 public static void main(String[] args) { 5 Scanner sc = new Scanner(System.in); 6 System.out.println("Please enter x Value"); 7 int x = sc.nextInt(); 8 if(x>=3) { 9 System.out.println("y="+(2*x+1)); 10 }else if(x<1&x>=-1) { 11 System.out.println("y="+2*x); 12 }else if(x<-1) { 13 System.out.println("y="+(2*x-1)); 14 }else { 15 System.out.println("x Wrongful"); 16 } 17 } 18 }
2.2 switch statement
Format
Switch (expression){ case constant 1: Style 1; break; case constant 2: Sentence Body 2; break; ... default: Statement body n+1; break; }
Execution process:
1. First calculate the value of the expression.
2. Comparing with case, once there is a corresponding value, the corresponding statement will be executed. In the process of execution, the break will end, otherwise it will go on.
3. Finally, if all case s do not match the value of the expression, the body part of the default statement is executed and the program ends.
Types of expressions:
byte, short,int, char, enumeration after jdk7 (new added): String
Case:
According to the keyboard input value 1, 2, 3,... 7 output corresponding to Monday, Tuesday,.... Sunday
package com._51doit.javase.day04; import java.util.Scanner; public class SwithDemo { public static void main(String[] args) { // Input 1-7 A number representing Monday to Sunday. Scanner sc = new Scanner(System.in); System.out.println("Please enter an integer"); int day = sc.nextInt(); switch(day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; default: System.out.println("Are you kidding?"); } } }
Matters needing attention:
1. case can only be followed by constants, not variables.
2. Constants after multiple case s cannot be the same
3. The sequence of cases is not required and can be placed anywhere.
4. default can also be placed anywhere
5. default can be avoided
6.break can be omitted, and if omitted, the code will continue to execute downward, regardless of whether the following case matches successfully, until it meets the break again, or until the end of the switch statement.
If all break s are not written in the code above, the result will be as follows: when you input 1: 1, the result will be printed with the result after it, when you input 2, the result of 21 will be printed with the result after it (1 will not).
When does the switch statement end: a break is encountered, or the code executes at the end of the switch statement
3. Cyclic structure
Let a piece of code be executed many times
3.1 for cycle
For (Initialization statement; Judgment conditional statement; Control conditional statement){ Circular sentence style; }
Execution process:
(1) Execute initialization statements
(2) Execute the judgment conditional statement to see if the result is true or false, and if it is false, the loop ends.
(3) Execution of loop statements
(4) Executing control condition statements
(5) Back to (2) Continue
Case:
1. Print 100 sentences
package com._51doit.javase.day04; // Print 100 sentences public class ForDemo{ public static void main(String[] args) { for(int i=1;i<100;i++) { System.out.println("I Learn Big Data"); } } }
2. Find the sum of odd and even numbers of 1-100
package com._51doit.javase.day04; public class ForDemo{ public static void main(String[] args) { int oldNumberCount = 0; int evenNumberCount = 0; for(int i=1;i<=100;i++) { if(i%2==0) { evenNumberCount += i; }else { oldNumberCount += i; } } System.out.println("1-100 The sum of even numbers in"+evenNumberCount); System.out.println("1-100 The sum of odd numbers in"+oldNumberCount);
3. Please count the number of data between 1 and 1000 that satisfy the following conditions
Divide 3 into 2; divide 5 into 3; divide 7 into 2;
/* Please count how many data between 1 and 1000 satisfy the following conditions Divide 3 into 2; divide 5 into 3; divide 7 into 2; **/ package com._51doit.javase.day04.loop; public class ForTest { public static void main(String[] args) { int a=0; int b=0; int c=0; for(int i=1; i<=1000; i++) { if(i%3 == 2) { a += 1; } if(i%5 ==3 ) { b += 1; } if(i%7 == 2) { c += 1; } } System.out.println(a); System.out.println(b); System.out.println(c); } }
Note that else if should not be used here, because else if only meets the condition, the later code will not run, but some data may satisfy both condition 1 and condition 2 or 3, which will be missed.
3.2 while cycle
Format:
Initialization statement; While (Judgment Conditional Statement){ Circular sentence style; Control condition statement; }
case
1. Print 1-100 numbers
package com._51doit.javase.day04.loop; public class WhileDemo { public static void main(String[] args) { int i = 1; while(i <= 100) { System.out.println(i); i++; } } }
2. The height of a mountain peak is 8848m. Add a piece of paper with a thickness of 0.01m. How many folds do you need to make sure that the thickness of the paper is not lower than the height of the mountain peak?
/* The height of a mountain peak is 8848m. Add a piece of paper big enough to cover it. The thickness is 0.01m. How many times do you need to fold it to protect it? The thickness of certificate paper is not less than the height of the mountain peak. **/ package com._51doit.javase.day04.loop; public class WhileDemo2 { public static void main(String[] args) { double hight = 0.01; int num = 0; while(hight < 8848) { hight *=2; num++; } System.out.println(num); } }
Comparison of the two cycles:
The for loop is suitable for operating on a range judgement
The while loop is suitable for not knowing the number of cycles or requiring the number of cycles