branching statements in Java:If statement:
Four ways to do the IF statement:
(1 if (expression _ Boolean) {...} ( 2 if (expression _ Boolean) {...} else {...} ( 3 if (expression _ Boolean) {...} else if (expression _ Boolean) {...} ( 4 if (expression _ Boolean) {...} else if (expression _ Boolean) {...} else
An IF condition statement is an important programming statement that tells the program to execute a program in the case of a condition, and in another case executes another statement.
With the IF condition statement, you can choose whether to execute the statement immediately following the condition. The keyword if is followed by a Boolean expression that is the condition, and if the expression returns true, the statement that follows is executed, and if false, the statement after the IF condition is not executed. The IF condition statement can be divided into simple if conditional statements, if...else statements, and If...else if multi-branch statements.
Example of an IF statement:
Public classGetterm {//creating the main class Public Static voidMain (String args[]) {//Main Method intx = 20;//declaring an int type local variable if(x > 30) {//determine if the variable x is greater thanSystem.out.println ("A has a value greater than 30");//condition-setting output information}Else if(x > 10) {//determine if the variable x is greater than tenSystem.out.println (the value of "a" is greater than 10, but less than 30 ");//condition-setting output information}Else if(x > 0) {//determine if the variable x is greater than 0System.out.println (the value of "a" is greater than 0, but less than 10 ");//condition-setting output information}Else{//When none of the above conditions are true, the block of statements executedSystem.out.println (the value of "A is less than 0");//Output Information } }}Switch statement:
int a = 2; switch (a) { case 1 ... break ; case 3 ... break ; case 5 ... break ; default ...
}
The value of an expression in a switch statement must be an integer or a character type, and a constant value of 1~ constant value n must also be an integer or character type. The switch statement evaluates the expression first, and if the value of the expression is the same as the value of the variable following a case, then several statements after the instance statement are executed until a break statement is encountered. At this point, if there is no break statement in the case statement, several statements in the following cases continue to be executed until a break statement is encountered. If no value of a constant is the same as the value of the expression, the statement following the default is executed. The default statement is optional, and if it does not exist, and the value of the expression in the switch statement is not the same as the constant value of any case, switch does not do any processing.
Switch Statement Example:
Public classGetswitch {//Create Class Public Static voidMain (String args[]) {//Main Method intWeek = 2;//define INT type variable week Switch(week) {//the expression that specifies the switch statement is a variable week Case1://defines a constant of 1 in a case statementSystem.out.println ("Monday");//Output Information Break; Case2://defines a constant of 2 in a case statementSYSTEM.OUT.PRINTLN ("Tuesday"); Break; Case3://defines a constant of 3 in a case statementSYSTEM.OUT.PRINTLN ("Wednesday"); Break; default://Default StatementSystem.out.println ("Sorry,i don ' t Know"); } }}the difference between if switch:
The IF statement uses the scenario:
For the result is a Boolean type of judgment
For a range of judgments
Judging for several constant values
Switch statement usage scenario:
Judging for several constant values
Switch recommends determining the fixed value when
If it is recommended to determine the interval or range
* Can do with switch, if all can do, the reverse is not
looping statements in Java:
(1)
for (int i=0;i<10;i++) {
...
}
(2)
while (expression _ Boolean) {
}
(3)
do {
} while (expression _ Boolean value);
Three simple examples of circular statements:
Public classTest2 { Public Static voidMain (string[] args) {intI=0; for(; i<3;i++) {System.out.println ("For:" +i); } while(i<6) {System.out.println ("While:" +i); I++; } Do{System.out.println ("Do:" +i); I++; } while(i<9); }}
General Example 1:
How to enter a score:
ImportJava.util.Scanner; Public classlianxi{ Public Static voidMain (string[] args) {intI=1;
Using a dead loop to constantly judge for(; i<10;i--) {Scanner Scanner=NewScanner (system.in); String Str=Scanner.nextline (); intA =integer.parseint (str); if(str== "55") {i=11; System.out.println ("It's over."); } if(a<=100&&a>80) {System.out.println (Excellent); }Else if(a<=80&&a>60) {System.out.println (Good); }Else if(a<=60&&a>50) {System.out.println (Fair); }Else if(a<=50&&a>=0) {System.out.println (Not); }Else{System.out.println ("The input has an error!!! "); } } }}
General Example 2:
Output Diamond
Public classvar_02 { Public Static voidMain (String args[]) {intLineCount = 17;//How many rows of the output diamond, please assign to an odd number intMaxlinenum = (linecount + 1)/2;//Diamond up to one line for(inti = 1; I <= maxlinenum; i++) {//more and more lines of circular diamond number for(intSpace = 1; Space <= Maxlinenum-i; space++) {//output space, quantity = Last line-current number of rowsSystem.out.print (""); } for(intStar = 1; Star <= (I * 2)-1; star++) {//output asterisk, quantity = number of rows *2-1System.out.print ("*"); } System.out.println ();//line Break } intDeclinecount = Linecount-maxlinenum;//counting the remaining lines, the number of these planets is decreasing. for(inti = 1; I <= Declinecount; i++) {//Number of rows with fewer loops of diamond for(intSpace = 1; Space <= i; space++) {//output space, quantity equals current number of rowsSystem.out.print (""); } for(intStar = 1; Star <= (declinecount-i + 1) * 2-1; star++) {//output asterisk, quantity equals (total-current number of lines) *2-1System.out.print ("*"); } System.out.println (); } }}
Add:
foreach statement:
is a special simplified version of the for statement, but the foreach statement does not completely replace the for statement, but any foreach statement can be rewritten as a version of the for statement. foreach is not a keyword, and it is customary to call this special for-statement format a foreach statement. The foreach statement provides a great convenience to programmers in terms of traversing arrays (this book will be described in detail in the 6th chapter of the array), and the syntax is as follows:
for (element variable x: Traverse object obj) { The Java statement referencing x;}
Example:
Public classRepetition {//Create class Repetition Public Static voidMain (String args[]) {//Main Method intArr[] = {7, 10, 1};//declaring a one-dimensional arraySystem.out.println ("Elements in a one-dimensional array are:");//Output Information for(intX:arr) { //The foreach statement, the variable referenced by int x, arr specifies the array to iterate through, and finally the X outputSystem.out.println (x); } }}
Java's branching and looping