Introduction to Java·Java control flow·if judgment usage·while loop usage·for loop usage

Introduction to Java·Java control flow·if judgment usage·while loop usage·for loop usage

Knowledge map

Insert picture description here

if judgment usage

if-else syntax:

 	 if(判断条件)
	  {
    
    
	  	代码块;
	  }else{
    
    
	  代码块2;
	  }
	 

Execution:
*1. First judge whether the condition is true, true or false
*2. If it is true, execute the code block in if, if it is false, skip the
if-else-if syntax:

		if()
		 {
    
    
		 	代码块1;
		 }
		 else if()
		 {
    
    
		 	代码块2;
		 }
		 else if()
		 {
    
    
		 	.....
		 }
		 else
		 {
    
    
		 代码块n;
		 }

Quiz

Write a program: input three integers from the keyboard and store them in num1, num2, and num3 respectively,
sort them (using if-else-if), and output them from small to large.

Scanner sc = new Scanner(System.in);
		System.out.println("请输入num1");
		int num1 = sc.nextInt();
		System.out.println("请输入num2");
		int num2 = sc.nextInt();
		System.out.println("请输入num3");
		int num3 = sc.nextInt();
		int temp;
		if(num1 > num2)//num1 > num2
		{
    
    
			if(num3 > num1)//num3 num1 num2
			{
    
    
				
			}
			else if(num3 > num2)//num1 num3 num2
			{
    
    
				
			}
			else//num1 num2 num3
			{
    
    
				
			}
		}
		else//num1 < num2
		{
    
    
			if(num3 > num2)//num3 num2 num1
			{
    
    
				
			}
			else if(num3 > num1)//num2 num3 num1
			{
    
    
				
			}
			else//num2 num1 num3	
			{
    
    
				
			}
		}

The difference between if and switch

1.switch适合用在条件是固定值(常量),不能进行逻辑判断,根据用户输入的值直接去找相应的case,效率高
2.case里边需要加入break;
3. if常用在逻辑判断,每一步判断都会执行

if && switch

Write a program: read a student's score from the keyboard, store it in the variable score, and output its
*corresponding grade according to the value of score:
* score >= 90 grade A
* 70 <= score <90 grade B
* 60 <= score <70 grade C
* score <60 grade D

Scanner sc = new Scanner(System.in);
		System.out.println("请输入你的成绩");
		int score = sc.nextInt();
		/*if-else 方式*/
		if(score >= 90)
		{
    
    
			System.out.println("A");
		}
		else if(score >= 70)
		{
    
    
			System.out.println("B");
		}
		else if(score >= 60)
		{
    
    
			System.out.println("C");
		}
		else
		{
    
    
			System.out.println("D");
		}
		/*switch方式*/
		
		if(score < 0 || score > 100)
		{
    
    
			System.out.println("成绩应该在0-100之间");
			System.exit(0);
		}
		switch(score / 10)
		{
    
    
		case 10:
		case 9:
			System.out.println("A");
			break;
		case 8:
		case 7:
			System.out.println("B");
			break;
		case 6:
			System.out.println("C");
		default:
			System.out.println("D");
			
		}
		

while loop usage

While loop syntax:

 ①初始化
		 while(②循环语句)
		 {
    
    
		 		③循环体;
		 		   累加器;
		  }

do...while loop

grammar:

		 ①初始化变量
		 do{
    
    
		 ②循环语句
		 ③累加器
		  }while(④循环条件)

The difference between while and do...while:
1. While is to judge first and then execute
2. do...while is to execute first and then judge, the loop body is executed at least once

for loop usage

The for loop provides us with a place to put three expressions
1. Initialization statement
2. Conditional expression
3. Accumulator

	 for(①初始化语句;②条件表达式;③累加器)
		 {
    
    
		 	③循环体;
		 }

The difference between while and for loop

1. While is often used when the number of loops is uncertain.
2. For is used when the number of times is fixed.
3. The for loop initialization is defined in parentheses and can only be used internally.
4. While initialization is defined outside the loop and outside the loop Can also be used

Guess you like

Origin blog.csdn.net/zjlwdqca/article/details/103807984