Four JavaScript control statements
(a) If-else
The If-else statement is the most basic control statement in JavaScript, which allows you to change the order in which statements are executed.
Expressions must use relational expressions or logical expressions to make judgments, and the result of an expression is usually used as a Boolean value to judge.
The number of 0 or nonzero in the result of an expression is converted to false and true respectively.
The statement within the curly braces {} allows multiple lines to be included if only one statement allows the curly braces {} to be used.
(ii) switch basic format
switch (expression)
{
Case value 1: statement 1; Break ;
Case value 2: statement 2;break;
Case Value 3: statement 3;break;
Default: Statement 4;
}
Here the code can practice, note that if the switch is written directly x instead of parseint (x), it will only execute the default content!!
<! DOCTYPE html> withA JS page</title><script type= "Text/javascript" >functionguess () {varX=prompt ("Guess What::", 1); Switch(parseint (x)) { Case1: Break; Case2: Break; Case3: document.write ("Winner!"); Break; default: Alert ("Don ' t give up!"); }}guess ();//Call the Funtion,be careful on the capital and small</script>page Content</body>(c) For loop
*for Loop Basic Format
for (initialize; condition; increment)
{
Statement 1; ...
}
Instance:
for (Var i=1;i<=7;i++)
{
document.write ("
document.write ("<br>");
}//Loop output H1 to H7 font size
(d) While
*while Loop Basic Format
while (condition)
{statement 1; ...}
Instance:
var I=1;
while (i<=7)
{
document.write ("
document.write ("<br>"); i++;
}//Loop output H1 to H7 font size
JavaScript Syntax Basics-Control statements