JavaScript / Conditional Statements

Conditional Statements are of 4 types(If,Else, Else if and switch).They were

Conditional Statement Syntax Example Output
If if (condition) { Code } if (3>2) { document.write("3 is big"); } 3 is big
Else
if (condition) { code }
else { code }
if (2>3) { document.write("2 is big"); } else { document.write("3 is big"); } 3 is big
Else if
if (condition1)
{ code }
else if (condition2)
{ code }
else {code }

if (2>3)
{ document.write("2 is big"); }
else if (3>2) { document.write("3 is big"); }
else {document.write("Both are same"); }
3 is big
switch switch(2)
{
case 1: code block; break;
case 1: code block; break;
default: code block; break;
}

switch(expression)
{
case 1: document.write("Selected Case2"); }; break;
case 2: document.write("Selected Case2"); }; break;
default:document.write("Selected Case Default"); }; break;
}
Selected Case2


Break Statement
It is used to come out of a loop completely and continues executing the code after the loop (if any).

Continue Statement
It breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.



Home     Back