How to master Java selection and loop structure
This article mainly explains "how to master Java selection and loop structure". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to master Java selection and loop structure.
Select structure 1. Simple ifint money=10; if (money > 100) {System.out.println ("money less than 100");}
If in Java is actually the same as other programming languages. Write the data you need to judge in parentheses after if, and execute it if you match it.
two。 Multiple if
Multiple if structures, there are many choices, but choose one here with else if to use
If (number==1) {System.out.println ("you chose toothpaste, 10 ¥"); shop = 10;} else if (number==2) {System.out.println ("you chose towels, 15 ¥"); shop = 15 } else if (number==3) {System.out.println ("you chose a washbasin, 20 ¥"); shop = 20;} else {System.out.println ("wrong choice"); a = false;}
When multiple judgments are needed, just after the closing parentheses of the last if, write else if to make the next judgment, and the program will always compare, such as after the execution of all the if that has not yet been met, execute the else statement, for example, the code above allows you to understand deeply, that is, a judgment of the number value.
3. Nested if
As the name implies, the nested if is to write the if in the if. When the program is executed, skip the parent if judgment, and then enter the nested if. If multiple if are nested inside, the execution process is the same.
If (a > 0) {if (a0, match, enter the second if, if a