0% found this document useful (0 votes)
23 views9 pages

Gr.8 - Switch Statement - Notes

The switch statement allows performing different actions based on different conditions. It can be used as an alternative to multiple if/else statements. The switch expression is evaluated once and the value is compared to the case values. If a match is found, the associated code block is executed. If no match is found, the default code block runs. Break statements are used to terminate execution of the switch block. Without breaks, code for subsequent cases will also execute until a break is reached.

Uploaded by

namyab2009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views9 pages

Gr.8 - Switch Statement - Notes

The switch statement allows performing different actions based on different conditions. It can be used as an alternative to multiple if/else statements. The switch expression is evaluated once and the value is compared to the case values. If a match is found, the associated code block is executed. If no match is found, the default code block runs. Break statements are used to terminate execution of the switch block. Without breaks, code for subsequent cases will also execute until a break is reached.

Uploaded by

namyab2009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SWITCH STATEMENT

Switch Case Statement


The switch statement is used to perform different actions based on different
conditions. It used as an alternate to multiple if .. else statements.
➢ Switch case uses strict comparisons (===). The values must be the same type to
match. A strict comparison can only be true if the operands are of the same type.
➢ The switch expression is evaluated once.
➢ The value of the expression is compared with the values of each case.
➢ If there is a match, the associated block of code is executed.
➢ If multiple cases matches a case value, the first case is selected.
➢ If no matching cases are found, the program continues to the default label.
➢ If no default label is found, the program continues to the statement(s) after the
switch.
The basic switch case syntax:
switch(expression)
{
case value:
block of code to be executed;
break;
case value:
block of code to be executed;
break;
default:
block of code to be executed;
}
The default Keyword

➢ The default keyword specifies the code to run if there


is no case match.

➢ The default case does not have to be the last case in a


switch block.

➢ If default is not the last case in the switch block,


remember to end the default case with a break.
The break Keyword
➢ The break keyword breaks out of the switch block. This will stop the
execution of the code and/or case testing inside the block.

➢ If the case statement does not have a break, the code for the next
cases are also executed until a break is found. This is known as fall-
through.

➢ A break can save a lot of execution time because it "ignores" the


execution of the rest of the code in the switch block.

➢ It is not necessary to break the last case in a switch block. The block
breaks (ends) there anyway.
Program using Numeric value
<script>
var i=2; // Assigned a value to the variable text
switch (i) // Passing the variable to switch condition
{ case 1:
document.write(“Value of i = 1");
break;
case 2:
document.write(“Value of i = 2");
break;
case 3:
document.write(“Value of i = 3");
break;
default:
document.write(“Enter numbers from1-3");
break;
} </script>
Program using String value
<script>
var text="Hello"; // Assigned a value to the variable text
switch (text) // Passing the variable to switch condition
{ case "Hello 1":
document.write("Hello 1");
break;
case "Hello":
document.write("Correct Text Hello ");
break;
default:
document.write("This is default selection");
break;
} </script>
Program using fall through
Example of different cases using the same code and default not being the last case :-

var d = +prompt(“Enter the day of the week in number”)


switch (d)
{
case 1:
case 2:
case 3:
default:
alert("Looking forward to the Weekend“);
break;
case 4:
case 5:
alert("Soon it is Weekend“);
break;
case 6:
case 7:
alert("It is Weekend“);
}

You might also like