js basis (conditions Loops)

Conditional statements

  if syntax statement block as follows:

    Only two cases // if (condition) { block of statements to be executed ; the else {} block of statements to be executed ;}

 

    // if (condition) {many cases to a block of statements executed ;} else if (condition) { for a block of statements is executed ; the else {} block of statements to be executed ;}

  switch statement syntax is as follows :

    switch (expression) {case value 1: block of statements executed; break; Case 2 values: a block of statements executed; break; values ​​Case 3: a block of statements is executed; break;

          default: statement block executed;}

    Exercise:

<Script> 
//. 1 prompts the user desired product. 
	var Goods = prompt ( "the user to enter commodity") 
	// 2 treatment of commodities. 
	Switch (Goods) { 
		Case "Hamburg": 
			document.write ( "Your Hamburg to commodity, please pay 15 yuan "); 
			Alert (" You want a hamburger product, please pay 15 yuan "); 
			BREAK; 
		Case" Coke ": 
			document.write (" You want a Coke product, please pay $ 10 "); 
			Alert (" You want to commodities Coke, please pay $ 10 "); 
			BREAK; 
		Case" chicken rice flower ": 
			document.write (" You want commodity chicken rice flower, please pay 25 yuan ") ; 
			Alert ( "You want commodity chicken rice flower, please pay 25 yuan"); 
			BREAK; 
		default: 
			document.write ( "Please enter the correct goods"); 
			Alert ( "Please enter the correct goods"); 
			BREAK; 
	} 
</ script>

  

loop statement 

  for (Expression 1; 2 Expression; Expression 3) { loop statement ;}   Tip : In the case where the number of cycles determined generally used for loop. Compared while and do while syntax is more concise.

  break terminates the loop directly, continue the end of this cycle directly into the next cycle.

  Exercise 1 table

  Code

 

<script type="text/javascript">
	document.write("<table border='1'>")
	for (var i = 0; i < 4; i++) {
		document.write("<tr>");	
		for (var j = 0; j < 3; j++) {
			document.write("<td>"+j+"</td>");
		}
		document.write("</tr>");	
	}
</script>

 

  effect

English II 99 multiplication formulas

Code:

<script type="text/javascript">
	for (var i = 1; i < 10; i++)
	{
		for (var j= 1; j <= i; j++)
		{
			var c=i*j;
			document.write(j+"*"+i+"="+c+"  ");
		}
		document.write("<br>");
	}
</script>

effect:

Exercise three

Code:

<Script type = "text / JavaScript"> 
	for (var. 1 = I; I> 0; I ++) { 
		var AA = prompt ( "Please enter the password"); 
		var BB = "000000"; 
		IF (BB AA === ) { 
			BREAK; 
		} the else IF (I <=. 3) { 
			Continue; 
		} the else { 
			Alert ( "password error Please check card"); 
		} 
	} 
	var CC = prompt ( "Please enter a withdrawal amount"); 
	for (var I = . 1; I> 0; I ++) { 
		IF (CC> 100 && CC = <100% 1000 && CC == 0) { 
			document.write (CC); 
			BREAK; 
		} the else { 
			var CC = prompt ( "Please enter withdrawal ";) amount 
		} 
	} 
	Alert (" Please take the card to complete the transaction "); 
</ Script> 
<- after improvements -!> 
<type = Script "text / JavaScript"> script type="text/javascript">
	for (var i = 1; i > 0; i++) {
		var AA = prompt ( "Please enter the password"); 
		var BB = "000000"; 
		IF (=== AA BB) { 
			var CC = prompt ( "Please enter a withdrawal amount"); 
			for (var. 1 = I; I> 0; I ++) { 
				IF (100% == 0 && CC CC <1000 && CC> 0) { 
					document.write (CC); 
					BREAK; 
				} the else { 
					var CC = prompt ( "Please re-enter your withdrawal amount"); 
				} 
			} 
			Alert ( "Please take the card to complete the transaction"); 
		} the else IF (I> 2) { 
			Alert ( "Please check card password error"); 
		} 
	} 
</ Script>

  

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/zqy6666/p/11797559.html