Conditional statement IF
Syntax:if (condition) statements1 else Statement2
Chestnuts:
<Scripttype= "Text/javascript"> varI=Prompt ("Please enter your score."); if(i>= the &&I<= -) {alert ("Good grades"); }Else if(i>= - &&I< the) {alert ("Score Qualified"); }Else if(i< - &&I>=0) {alert ("failure to pass the grade"); }</Script>
Conditional Statement Switch
Grammar:
switch (expression) {case value: //statement break ; Case value: //statement break ; Default: //statement }
Chestnuts:
<Scripttype= "Text/javascript"> varI= -; Switch(i) { Case -: Alert (i+ "equals"); Break; Case -: Alert ( -); Break; default: Alert (" Other"); }</Script>
While loop
Syntax: while (exp) {//statements;}
Description: while (variable <= end value) {code to be executed}
Chestnuts:
<Scripttype= "Text/javascript"> varI= 1; while(i< 3) {alert (i); I++; }</Script>
Do...while Cycle
Syntax: do {//statements;} while (condition);
Description: Do {code to be executed} while (variable <= end value)
Chestnuts:
<Scripttype= "Text/javascript"> varI= 1; Do{alert (i); I++; } while(i< 3);</Script>
For loop
Syntax: for (initial; expression; post-loop-expression) {//statement;}
Description: for (variable = start value; variable <= end value; variable = variable + step value) {Code to execute}
Chestnuts:
<Scripttype= "Text/javascript"> for(varI= 1; I< 3; I++) {alert (i); }</Script>
Break statements and Continue statements
Description: The break statement can exit the loop immediately; The continue statement simply exits the current loop;
Chestnuts:
<Scripttype= "Text/javascript"> varN= 0; for(varI= 1; I< Ten; I++){ if(i== 4){ Continue; } N++; } alert (n); varm= 0; for(varJ= 1; J< Ten; J++){ if(J> 5){ Break; } m++; } alert (m);</Script>
JS (conditional statement, loop statement)