Switch Case Loops and Functions in JavaScript

We have learned about decision making in JavaScript using if-else statements in our previous article on if-else statement in JavaScript. We have seen in our previous article that we can use the if-else statements to perform actions based on some particular condition. That is if a condition is true then perform some task or else if the condition is false then execute some other task.
The switch case statement in JavaScript is also used for decision making purposes. In some cases, using the switch case statement is seen to be more convenient over if-else statements. Consider a situation when we want to test a variable for hundred different values and based on the test we want to execute some task. Using if-else statement for this purpose will be less efficient over switch case statements and also it will make the code look messy.

Syntax:

switch(expression)

{

case value1;

statement1;

break

case value1;

statement2

break

.

case valueN

statementN

break

default:

statementDefault;

Switch example

var day = 3

switch(day)

{

case 1:

document.write(“<h1>  Sunday</h1>  “);

break;

case 2:

document.write(“<h1>  Monday</h1>  “);

break;

case 3:

document.write(“<h1>  Tuesday</h1>  “);

break;

case 4:

document.write(“<h1>  Wednesday</h1>  “);

break;

case 5:

document.write(“<h1>  Thursday</h6>  “);

break;

case 6:

document.write(“<h1>  Friday</h1>  “);

break;

case 7:

document.write(“<h1>  Saturday</h1>  “);

break;

default:

document.write(” wrong input”);

}

output of above example

default

In simple way we can default specify the code will run if there is not match in case

JavaScript Loops

Loops are useful when you does execute the same lines of code repeated, for a specific number of times or as long as a specific condition is true.

1. JavaScript loop

for-loops through a block of code until the counter reaches a specified number.

Syntax

for(statement)

{

lines of code will be execute

}

  • The statement1 executes first even before executing the looping code. So, this statement has normally use to assign values to variables that will be use inside the loop.
  • The statement2 is the condition to execute the loop.
  • The statement3 is execute every time after the loops code is execute.

Example – 1

for ( i=1; i<=4; i++)

{

document.write( i+”<br>  “);

}

Example – 2

for ( i=1; i<=10; i++)

{

documen.write(i);

}

JavaScript while loop

A while loop is the control flow statement that allows code to be execute repeatebased on a given Boolean statement. The while loop can be thought of as long as a repeating if statement.

Syntax

while(statement)

{

loop statement…(i);

}

Lets us see the example

var i = 15;

while(i <= 20)

{

document.write(i);

i++

}

the output of About example

do, while loop

The do, while loop is same like that while loop. The only difference is that in do…while loop, the block of code gets execute one time even before checking the condition.

Syntax

do {

// statement of code to be executed

}

while statement

Example 3

var i = 15;

do {

document.write ( i+”<br>  “);

i++

} while (i<= 20);

Output of above example

JavaScript Functions

in the javascript, function may be define any a name and with allow parentheses ()
in a paranthesis () within paramiter are use

syntax of function

this syntax is declaring of function

function functionName {

// execution of code

}

<script >  function short() {

document.write (“hello-everyone”)

}

</script >  

<html >  

<input type=”button” value=”click” onclick=”short()” > 

 </html >  

output

Leave a Reply

Your email address will not be published. Required fields are marked *