One ,true and false

        java A number is not allowed as a Boolean value , But in the C/C++ Is allowed in , as follows :
void example15(){ if(1){// stay C In language ,1 It represents the truth ,0 It's a fake ,C++ It's the same thing printf("true"); }else{
printf("false"); } }
        java Must use a conditional expression to convert it to a boolean type value . of course , Boolean values can also be used directly , as follows :
public void example1(){ if(true){// Direct use of true and false values System.out.println("true"); } int t
= 3; if(t>3){// Conditional expression System.out.println("true"); } }
Two ,if-else

if-else Is the most basic form of control program flow , as follows :
public void example2(){ /** * if-else */ int score = 90; if(score>0){
System.out.println(" You passed !"); }else {//else Statements can be omitted System.out.println(" You failed !");
} /** * if-else-if-else */ int week = 2; if(week == 1){
System.out.println(" Today is Monday "); }else if(week == 2){
System.out.println(" Today is Tuesday "); }else { //...... } }
Three , iteration

1,while
public void example3(){ int i = 0; int j = 0; while (i<5){ j += i; i++; }
System.out.println(j);//0+1+2+3+4=10 }
2,do-while
@Test public void example4(){ int i = 0; int j = 0; do{ j += i; i++; }while
(i<5); System.out.println(j);//0+1+2+3+4=10, It follows while The difference is that it is executed once }
3,for
/** * from 1 Add 100 */ @Test public void example5() { int j = 0; for (int i = 1; i
<= 100; i++) { j += i; } System.out.println(j);//5050 }
4,foreach
public void example6(){ /** * foreach array */ int[] nums = {10,20,30,40,50}; int
result = 0; for(int i:nums){ result += i; } System.out.println(result);//150
/** * foreach list aggregate */ List<String> strings = new ArrayList<>();
strings.add(" Zhang San "); strings.add(" Li Si "); strings.add(" WangTwo "); String str =
" There are three people in this collection , namely :"; for(String s:strings){ str += s + ","; } str =
str.substring(0,str.length()-1);
System.out.println(str);// There are three people in this collection , namely : Zhang San , Li Si , WangTwo /** * foreach map aggregate */
Map<String,String> map = new HashMap<>(); map.put(" Zhang San ","20");
map.put(" Li Si ","25"); map.put(" Wang Wu ","19"); for(String s:map.keySet()){
System.out.println(s + " What's your age :" + map.get(s) + " year !"); } /** * What's the age of Li Si :25 year ! *
What's the age of Zhang San :20 year ! * What is Wang Wu's age :19 year ! */ }
5,return

       
return Keywords serve two purposes : One is to specify what value a method returns , The second is to cause the current method to exit . If you are returning void None of the methods return sentence , Then at the end of the method, there is an implicit return.
@Test public void example9() { Boolean b = this.back1(10, 5, 20);
System.out.println(b); Boolean b1 = this.back1(25, 5, 20);
System.out.println(b1); } // Judge whether it is between two numbers private Boolean back1(Integer num,
Integer begin, Integer end) { if (num >= begin && num <= end) { return
Boolean.TRUE; } return Boolean.FALSE; }
6,break and continue

        break Used to force an exit loop , The remaining statements in the loop are not executed , and continue Is to abort the current loop , Continue to the next cycle .
@Test public void example10(){ System.out.println(useBreak(10));
System.out.println(useContinue(10)); } private int useBreak(int num){ int
result = 0; for(int i=1;i<num;i++){ if(i%5 == 0){ break; } result += i; }
return result;//1+2+3+4=10 } private int useContinue(int num){ int result = 0;
for(int i=1;i<num;i++){ if(i%5 == 0){ continue; } result += i; } return
result;//1+2+3+4+6+7+8+9=40 }
7,java Label and goto

       
goto It's a jump at the source level , This has led to a bad reputation , If a program always jumps from one place to another , This makes it difficult to identify the control flow of the program . in fact , The real problem is not the use goto, It's about goto The abuse of , In a few cases ,goto It is the best way to control the process ,goto Can be used to jump out of deep nested loops . although goto still java A reserved word in , But it's not used in language .

       
java Using tags ,break and continue Keyword jump operation , Tagged break It breaks the loop and jumps out of the loop that the tag refers to , Tagged continue It will reach the location of the label , And re-enter the loop immediately after that tag .
@Test public void example11(){ // Three groups , Each group 5 Students int[][] arr =
{{65,80,92,85,55},{58,65,76,52,66},{67,63,52,85,76}}; int result = 0;
out:for(int i=0;i<arr.length;i++){ for(int j=0;j<arr[i].length;j++){
if(i==0){// End the first cycle of the outer cycle , Go to the next cycle continue out; }
if(i==2){// When the outer cycle enters the third time , End the outer cycle directly break out; } result =
result>arr[i][j]?result:arr[i][j]; } } System.out.println(result);//76 }
8,switch

         each case It's a tag, When matching to tag The others are ignored tag The code body is then executed sequentially , Until the encounter break perhaps }
@Test public void example12(){ int i=9; switch (i) { default:
System.out.println("default"); case 0: System.out.println("zero"); case 1:
System.out.println("one"); case 2: System.out.println("two"); } /* * default *
zero * one * two analysis : each case It's a tag, When matching to tag The others are ignored tag The code body is then executed sequentially , Until the encounter break perhaps } */ }
 

Technology
©2019-2020 Toolsou All rights reserved,
Final review of database : Summary of comprehensive application questions use Python Make simple games Laplance operator ( Second derivative ) Convert hard disk to GPT Partition format Python Implementation of Hanoi Tower code about String How to create objects vue3 Learning journey 1—— establish vue3 project java String from back to front _Java String String summary use Python Write a story about plants versus zombies 【 Greedy Algorithm 】 Huffman coding problem