switch...case, if...else, for and while loops, do...while statements, double for loops

Keywords: JDK Eclipse Java

Articles Catalogue

switch...case

Basic structure:

switch(key) {                  

case value :
	
	break;
	
default :
	
	break;
}
  • Practical examples
    int  weekday = 3;
	switch (weekday) {
		case 1:
			System.out.println("Monday");
			break;//Interruption termination 
			
		case 2:
			System.out.println("Tuesday");
			break;

		default:
			System.out.println("Other case If none of the conditions are met, the execution will be carried out. default Code in");
			break;
	}
  • Can the key of the basic structure be a string?
String nameString = "zhangsan";

		switch (nameString) {
			case "zhangsan":
				System.out.println("key For String");
				break;
String nameString = "zhangsan";
		switch (nameString) {
			case "zhangsan":
				System.out.println("key For String");
				break;
	
			default:
				break;
		}

From the simple example above, we know that switch.. Like if, case only executes a () break, which belongs to a branch statement.

Summary:

  1. switch... case is not used much in real development, but if you see it, you need to know what it is.
  2. Break terminates the statement, but gee, break can also be removed. Whether or not it should be removed depends on logic.
  3. All switch es... case can all be converted to if... Others, on the contrary, are not established.
  4. key must be int or String (JDK 1.7 or later) or enum (enumeration)
  5. Some people prefer switch. case, because it's neater than if.
  6. switch... The core of a case statement is to judge the difference between equality and inequality, but if is a little stronger than it.

if...else

Basic structure:

 Law 1. if(condition) {    Law 2.  if(condition) {  
		                                  
	}                         } else if(condition) {
	
	                          }
  • Practical examples
        int money = 40;
		
		if(money < 30) {
			
			System.out.println("Give your brother 10 yuan.");
			
		}else if(money >35) {
			
			System.out.println("Give your sister 20 yuan");	
		}

//    The output is "20 yuan for my sister"

Note:

Conditions are conditions. When conditions are a range, we need to use logical operators.
For example, 10 < money < 20
We need to write it (10 < money | money < 20) and if we want eclipse to make an error, we can write 10 < money < 20.. hahaha
Whether scope is used in code | | (or) or & (and) needs to be carefully considered by oneself, which is in fact similar to the meaning of Union and intersection.

Loop statements: for and while loops

In java, you always encounter code duplication. A common scenario in development is traversal of arrays (taking things from plastic bags).
for while do...while

for loop statement

  • Basic structure

    The order of execution is: 1 -> 2 -> 3 -> 4 -> 2 -> 3 -. - >2
    Initial condition 1 is executed only once.

  • Practical examples

//		Example: Accumulation and 1+2+...+10
		
		int sum = 0;
		
//		The function of i is to establish the number of cycles 
		for (int i = 0; i < 10; i++) {
			sum = sum + i + 1;
		}
		System.out.println("sum:" + sum);
//		Separating the relationship between loop i and summation factor, the following code:

		int sum1 = 0;
		int value = 1;
		for (int i = 0; i < 10; i++) {
			
//			Sum the time, pick the i clean, the role of i is only to indicate the number of cycles.
			sum1 = sum1 + value;
			value ++;
		}
		System.out.println("sum1:" + sum1);

Note: The function of i is to establish the number of cycles. Sometimes it is necessary for i to adopt its value, which can be adopted, but the initial function of i must be remembered.

while loop statement

        while (condition) {
		System.out.println(  );
  		}

Condition is a boolean value. For while, there must be a loop termination condition

	The following code is a dead loop, which is meaningless and can never be executed.
	
int a = 1;
	while (a < 10) {
		System.out.println("dead loop");
	}
//		for cycle accumulation and improvement while:
		int i = 0;
		while (i < 10) {

//			The incremental code for the next line of I is executed repeatedly because it's in while until i's
//			If the value is 10, the condition after which is false and the loop ends
			i ++;

		}

The difference between "while" and "for":
For can directly specify the number of cycles, while while not. so, if the number of cycles is established, priority should be given to using for or while.

do... the while statement

  • Basic structure

      do {
          //  Circulatory body
      } while (condition);
    

When condition is true, the code in {} after do is executed iteratively.

//		When executing a loop, do is executed once first.
		do {
			System.out.println("Ugly long live, handsome old fast grow");
		} while (false);
		
		
//		You can't write false after while, which results in an error: unreachable code.
//		Unreachable (executable) code
		
		while (false) {
			System.out.println("123");
		}

Double for cycle

Next article: https://blog.csdn.net/weixin_43607906/article/details/97155418

Posted by JohnnyBlaze on Wed, 24 Jul 2019 04:23:26 -0700