.

Decision Making in Javascript Assignment Help

{ //block1- statements } else if(constraint2) { // block2- statements } else if (constraint3) { // block3- statements } . . else { //block-statements } `}

Example:

decision making in java script img5

7.5 Switch-case statement

It switches the flow of program execution to n-number of cases based on the value which exists in switch statement.

{`
switch( expression )
{
case value1:
     Block-1 statements;
     Break;
case value2:
     Block-2 statements;
     Break;
case valuen:
     Block-n statements;
     Break;
default:
     Block-1 statements;
     Break;
     }
`}

The following rules apply to a switch statement −

  • The value of the expression may be integer or a character.
  • Each case is identified using value1, 2, n. No two case values are same.
  • Case labels end with a colon ( : ). Each of these case have block of executable statements.
  • Whenever the switch is executed, it returns the value from the expression and the value is compared with all the cases which reside within the switch-case block.
  • The case block starts execution if the expression value is matched with case value.
  • The break statement terminates the program execution and switch over the control out of the switch block.
  • The optional blocks of statements are written in the section default case. When the expression value is not matched with any case value, the default block will be executed.

Example:

decision making in java script img6
.