javascript Chapter 2 process control - branches

Posted by lisa71283 on Sun, 16 Jan 2022 17:50:31 +0100

Chapter II branch structure of process control

Understand the three structures of the program

Be familiar with the program execution process of sequential structure

Skillfully use if statement to realize branch condition judgment

Skillfully use switch statements to judge multiple branches

ECMA basic syntax:

Annotation, variable, operator, data type, process control, object

1, Process control

1.1. What is process control
The order of execution in a program. (top down, right to left (if the right is an expression (left to right)))
Controls the execution time of the code. Number of executions.
You need to eat when you're hungry.
1.2 and 3 program structures
1. Sequential structure: from top to bottom, from right to left (if the right is an expression (left to right))
2. Branch structure: controls when and whether code is executed
3. Loop structure: controls how many times the code is executed
2, Branch office – if
format

	if(Conditional expression①){
	Executed code block;
}
//**Execution process: * * the code block will be executed only when the conditional expression ① is established.

If your score is less than 60, Congratulations, fail!

<script type="text/javascript">
    // If your java score is less than 60, congratulations and fail!
    var Score = prompt("Please enter your java achievement");
    /*alert(typeof(Score));//string
    alert(typeof(60));//number*/
    if(javaScore < 60){

    alert("Congratulations, failed!");
    }
</script>

2.2if else judgment (if so, otherwise)

if(Conditional expression①){
	Code block①;
}else{
	Code block②;
}

First judge whether the conditional expression ① is true. If it is true, execute code block ①; otherwise, execute code block ②;

var Score = prompt("Please enter your java achievement");
			if(Score < 60){
				alert("Congratulations, failed!");
			}else{
				alert("Congratulations, Xiuer!");
			}

2.3 multiple if judgments

if(Conditional expression①){
	Code block①;
}else if(Conditional expression②){
	Code block②;
}else if(Conditional expression③){
	Code block③;
}else if(Conditional expression④){
	Code block④;
}else if(Conditional expression⑤){
	Code block⑤;
}else{
	Code block⑥;
}

First judge whether the conditional expression ① is true. If it is true, execute the code block ①,

Otherwise, judge whether the conditional expression ② is true. If it is true, execute the code block ②

Otherwise, judge whether the conditional expression ③ is true, and if so, execute the code block ③

Otherwise, judge whether the conditional expression ④ is true, and if so, execute the code block ④

. . . . .
Until the last else executes the last code block and the program ends.
Case 1:

At the beginning of school, the body temperature is detected. If you have a fever, the nucleic acid is detected to see if you are infected with pneumonia. If you are infected with pneumonia, isolate immediately. Otherwise, normal class.

var tiwen = prompt("Please enter your body temperature");
			if( tiwen >37){
				alert("Nucleic acid detection")
				var result=prompt("Is it infected");
				if(result=="yes"){
					alert("quarantine")				
				}else{
				alert("Normal class")
			}
			}else{
				alert("Normal class")
			}
var score =prompt("Please enter your test results");
		if(score >=90){
			alert("Xiuer!");
		}else if(score>=80 ){
			alert("A little Xiuer!")
		}else if(score>=70 ){
			alert("A little Xiuer!")
		}else if(score>=60 ){
			alert("A little Xiuer!")
		}else if(score<60 ){
			alert("A little Xiuer!")
		} 

3, Branch switch case

//Syntax format
switch(parameter){
	case Value 1: 
		Code block①;
		break;
	case Value 2:
		Code block②;
		break;
	case Value 3:
		Code block③;
		break;
	. . . . . 
	default:
		Last code block;
		break;
}

Execution process:
Compare whether the parameters after switch are the same with the case. If they are the same, execute the code block behind the case. Otherwise, compare them with the next case. break is the conclusion
If all case s are the same, execute the code block under default

**be careful**: The comparison value is a congruent comparison, that is, the comparison value also compares the data type.

The difference between switch and if:
if generally makes interval judgment.

switch generally judges equivalence.

practice

<script type="text/javascript">
			var a=prompt("Please enter a number to judge whether it is the number of daffodils");			 
			// var b=a1/100;			
			//var c=a1%100/10;
			// var d=a1%10;
			var a1=parseInt(a);
			var b=parseInt(a1/100);
			 var c=parseInt(a1%100/10);
			 var d=parseInt(a1%10);
			 if (a==b*b*b+c*c*c+d*d*d){
				 alert("true");
			 }else{
				 alert("false");
				 
			 }
		</script>
//There are chickens and rabbits in a cage, with 35 heads and 94 legs; Input two numbers, representing the number of chickens and rabbits respectively, and output whether it is the answer
<script type="text/javascript">
			var x = Number (prompt("Number of chickens"));
			var y = Number (prompt("Number of rabbits"));
			if(x+y==35&&2*x+4*y==94){
				alert(true);
			 }else 
			 {
			alert(false);
			 }
	
		</script>
//Enter a 3 / 4 / 5 / 6 digit number to judge whether this number is a palindrome number (looking forward = = looking backward).
<script type="text/javascript">
				
				var a=Number(prompt("Please enter a number that is no more than six digits"));
				var b,c,d,e,f,g;
				if(a>=100000){
					b=parseInt(a/100000);
					c=parseInt(a%100000/10000);
					d=parseInt(a%10000/1000);
					e=parseInt(a%1000/100);
					f=parseInt(a%100/10);
					g=parseInt(a%10);
					alert(b==g && c==f && d==e);
							
				}else if(a>=10000){
					d=parseInt(a/10000);
					e=parseInt(a%10000/1000);
					f=parseInt(a%100/10);
					g=parseInt(a%10);
					alert(d==g && e==f );
				}else if(a>=1000){
					d=parseInt(a/1000);
					e=parseInt(a%1000/100);
					f=parseInt(a%100/10);
					g=parseInt(a%10);
					alert(d==g && e==f );
				}else if(a>=100){
					e=parseInt(a/100);
					g=parseInt(a%10);
					alert(e==g);
				} 
			</script>
//Input the scores of five people continuously, and calculate the maximum score, minimum score and average score.
<script type="text/javascript">
			var a= Number(prompt("Score 1"));
			var b= Number(prompt("Score 2"));
			var c= Number(prompt("Score 3"));
			var d= Number(prompt("Score 4"));
			var f= Number(prompt("Grade 5"));
			var max;
			var min;
			var pj;
			max= a>b ? a:b;
			max= max>c ? max:c;
			max= max >d ? max:d;
			max=max > f ? max:f;
			
			min=a<b? a:b;
			min=min <d ? min:d;
			min=min <c ? min:c;
			min=min <d ? min:d;
			
			pj=(a+b+c+d+f)/5;
			alert(max);
			alert(min);
		 
			alert(pj);
			
		
			
		</script>

Topics: Javascript