Search “Program Yuan Xiaozhuang” on wechat, and you will get a full set of Python full stack tutorials, as well as the constantly updated e-books and interview materials prepared by Xiaozhuang

preface

JavaScript as a programming language also has branch structure and loop structure to control the flow of code execution.

Branching structure

Python uses the if branch, and JavaScript has two branches, if and switch.

If the structure

// Syntax structure
if(condition) {code executed when condition is true}else if(condition) {code executed when condition is true}...else{code for which none of the above conditions are valid}/ / small example
var age = 18;
if (age>18)
{
    console.log('Welcome')  // console.log(") can be printed in the browser's console
}
else if(age=18)
{
	console.log('Just wait.')}else
{
	console.log('No minors allowed')}Copy the code

The switch structure

// Syntax structure
switch(Condition) {caseCondition: The execution code executed when the condition is truebreak;
	caseCondition: The execution code executed when the condition is truebreak;
	caseCondition: The execution code executed when the condition is truebreak;
	default:
		console.log(None of the above conditions hold true.)}/ / small example
var num = 2;
switch(num){
  case 0:
  	console.log('drink');
  	break; # do not addbreakOnce you get a match, you keep goingcase 1:
  	console.log('singing');
  	break;
  case 2:
  	console.log(Wash feet ' ');
  	break;
  case 3:
  	console.log('massage');
  	break;
  case 4:
  	console.log(Nutrition Express);
  	break;
  case 5:
  	console.log('Go easy, boss. Welcome next time.');
  	break;
  default:
  	console.log('None of the conditions match the default flow.')}Copy the code

The ternary operation

Note: triadic operations do not write too complicated oh

// Syntax format
var/letRes = condition? The result when the condition is true: the result when the condition is not/ / small example
var res = 2>5 ? 2:100  / / 100
let res = 2>5 ?10: (8>5?1:2)  / / 1
Copy the code

Loop structure

The for loop

// Print a loop of 0-9
for(let i=0; i<10; i++)
{
	console.log(i);
};

// Iterate over the elements in the array
var a = [111.222.333];
for (let i=0; i<a.length; i++)
{
	console.log(a[i]);
};
Copy the code

The while loop

// Loop to print 0-99
var i = 0;
while (i < 100)
{    
	console.log(i);    
	i++;
};
Copy the code

conclusion

The article was first published on the wechat public account Program Yuan Xiaozhuang, and synchronized with nuggets and Zhihu.

The code word is not easy, reprint please explain the source, pass by the little friends of the lovely little finger point like and then go (╹▽╹)