0% found this document useful (0 votes)
4 views

Control State

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Control State

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Control Structures in JavaScript

JavaScript has a similar set of control structures like the other languages such as c, c++ and Java.
conditional statements are supported by if and else described below: Conditional Statements • In
JavaScript we have the following conditional statements as defected

• if (statement)specify a block of code to be executed, if the specified condition is true

• else (statement) specify a block of code to be executed, if the same condition isfalse

• else if (statement) specify a new condition to test, if the first condition is false

• switch (statement) specify many alternative blocks of code to be executed.

Example

Prg1:
<!DOCTYPE> <html> <head></head> <body>

<p>Display Good Day! if the hour is less than 18:00: </p>

<script type="text/javascript">

var age=prompt(“Enter Your age”);

if (age >18)

document.write("Elligible for Voting ");

else

document.write("Elligible for Voting ");

</script> </body> </html>

Prg2:
<html> <head></head> <body>

<p>Display Good Day! if the hour is less than 18:00: otherwise Display Good

Eveening</p>

<script type="text/javascript">

var d = new Date();


var hr = d.getHours();

if (hr < 18) {

document.write("Good Day! ");

else

document.write("Good Evening! ");

</script> </body>

Prg3:
<!DOCTYPE> <html><body>

<p> Display Day of Week</p>

<script> var d = new Date();

var n = prompt(“Enter 1 for Sunday, 2 for Monday ………….7 for Saturday”);

if (n== 1)

day = "Sunday";

else if (n== 2)

day = "Monday";

else if (n==3)

day = "Tuesday";

else if(n==4)

day = "Wednesday";

else if(n==5)

day = "Thursday";

else if(n==6)

day = "Friday";

else if(n==7)

day = "Saturday";
}

document.write("Today is "+ day);

</script> </body> </html>

SWITCH
The switch statement is used to select one of many blocks of the code to be executed in a
program described below. the switch expression is evaluated once and the value of the expression is
compared with the values of each case. if there is a match found, then the associated block of code is
executed otherwise default code block is executed.

Syntax: switch statement

switch (expression)

case n:

code block

break;

case n:

code block

break;

default:

default code block

Example: Use the weekday number to calculate weekday name with the help of getday() method.the
getday() method returns the weekday as a number between 0 and 6. (Sunday=0,Monday=1, Tuesday=2
..)

<!DOCTYPE> <html><body>

<p> Display Day of Week</p>

<script>

var da= prompt(“Enter 1 for Sunday, 2 for Monday ………….7 for Saturday”);

da= parseInt(n);

switch(n){

case 0:
day = "Sunday";

break;

case 1:

day = "Monday";

break;

case 2:

day = "Tuesday";

break;

case 3:

day = "Wednesday";

break;

case 4:

day = "Thursday";

break;

case 5:

day = "Friday";

break;

case 6:

day = "Saturday";}

document.write("Today is "+ day);

</script> </body> </html>

Section-6: Looping Structure


Loops Loops can be used to execute the same code over and over again, particularly useful when dealing
with arrays as shown in figure 3.3. JavaScript supports different kinds of loops described below:

• for - loops through a block of code a number of times

• for/in - loops through the properties of an object

• while - loops through a block of code while a specified condition is true

• do/while - also loops through a block of code while a specified condition is true
*For Loop The for loop is best suited when you already know the number of times the statements
should be executed. the loop executes until the condition becomes false.

Syntax: for(initialization; condition; increment)

Statements

Example:

<!DOCTYPE html>

<html> <body>

<p> For loop Example</p>

<script>

for (i = 0; i < 5; i++) {

document.write("Hello Life! You are so Good" +"<br>"); }

</script> </body> </html>

Output: Hello Life! You are so Good

Hello Life! You are so Good

Hello Life! You are so Good

Hello Life! You are so Good

Hello Life! You are so Good

* For..in Loop
The for/in loop: the JavaScript for/in statement loops through the properties of an object
explain below.

You might also like