Wednesday, July 19, 2023

Condition Execution

 

Condition Execution (Or) Control Statement (Or) Programming Construct


If

Syntax

if (Condition)

{ Statement(s) }

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

 

    <h2 style="background-color: green;color:rgb(255, 217, 0);font-weight: bolder;font-family: Georgia, 'Times New Roman', Times, serif;">Condition Execution</h2>

    <h5 style="color: rgb(231, 15, 166);">Output</h5>

    <script type="text/javascript" src="basic.js"></script>

</head>

<body>

   

</body>

</html>



var age=20 ;

var lineb="<br>";

document.write("<u><b>Vote Eligibility Test</b></u>")

document.write(lineb)

if( age >= 18 ){

 document.write("<b>Eligible for vote</b>");

}

Output










If else

Syntax

if (Condition)

{ Statement(s) }

else

{ Statement(s) }

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

 

    <h2 style="background-color: green;color:rgb(255, 217, 0);font-weight: bolder;font-family: Georgia, 'Times New Roman', Times, serif;">Condition Execution</h2>

    <h5 style="color: rgb(231, 15, 166);">Output</h5>

    <script type="text/javascript" src="basic.js"></script>

</head>

<body>

   

</body>

</html>


var age=15 ;

var lineb="<br>";

document.write("<u><b>Vote Eligibility Test</b></u>")

document.write(lineb)

if( age >= 18 ){

 document.write("<b>Eligible for vote</b>");

}else{

 document.write("<b>Not eligible for vote</b>");

}

Output










if...else if..

Syntax

if (Condition)

{ Statement(s) }

else if (Condition)

{ Statement(s) }

else if (Condition)

{ Statement(s) }

else

{ Statement(s) }

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

 

    <h2 style="background-color: green;color:rgb(255, 217, 0);font-weight: bolder;font-family: Georgia, 'Times New Roman', Times, serif;">Condition Execution</h2>

    <h5 style="color: rgb(231, 15, 166);">Output</h5>

    <script type="text/javascript" src="basic.js"></script>

</head>

<body>

   

</body>

</html>

 

var mark = 95;

if( mark >=80  ){

 document.write("<b>A GRADE</b>");

}else if( mark >= 50 ){

 document.write("<b>B GRADE</b>");

}else if( mark == 30 ){

 document.write("<b>C GRADE</b>");

}else{

 document.write("<b>FAIL</b>");

}

 

Output









SWITCH-CASE

switch (Condition)

 {

case value1:

statement(s)

break;

case value2:

statement(s)

 break;

case value(n):

statement(s)

break;

default:

statement(s)

}

var grade='A';

document.write("Entering switch block<br />");

switch (grade)

{

 case 'A':

 document.write("Good job<br />");

 break;

 case 'B':

 document.write("Pretty good<br />");

 break;

 case 'C':

 document.write("Passed<br />");

 break;

 case 'D':

 document.write("Not so good<br />");

 break;

 case 'F':

 document.write("Failed<br />");

 break;

 default:

 document.write("Unknown grade<br />")

}

document.write("Exiting switch block");

 

Output








WHILE LOOP

syntax

while(condition)

{

Statement(s) to be executed if the condition is true

 }

var num = 5;

document.write("Starting Loop ");

while (num < 10){

 document.write("Current Count : " + num + "<br />");

 num++;

}

document.write("Exciting Loop!");

 

Output








do...whileLoop

do{

 Statement(s) to be executed;

} while (condition);

var num = 4;

document.write("Starting Loop ");

do{

 document.write("Current Count : " + num + "<br />");

 num++;

}while (num < 10);

document.write("Exciting Loop!");

 

Output











FOR LOOP

Syntax

for (initialization; condition checking; inc/dec)

{

 Statement;

}

var num = 1;

document.write("Starting Loop ");

for(num;num<=20;num++)

{

    document.write(num+"<br>");

}

document.write("Exciting Loop!");

 

Output










For...In Statement

Syntax

 for (variable in object)

{

Statement;

}

var x;

var cars = new Array();

cars[0] = "BMW";

cars[1] = "Volvo";

cars[2] = "Audi";

for(x in cars)

 {

 document.write(cars[x] + "<br />");

 }


Output







For each loop

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <script>

        function callfor()

        {

            var myarray=['DCA','HDCA','DMO','ADJP','ADPP','TPRIME'];

            myarray.forEach(show);

        }

        function show(x)

        {

            console.log(x);

        }

 

       

    </script>

</head>

<body>

    <button onclick="callfor()">Check</button>

</body>

</html>

Output



Break

var num = 1;

document.write("Starting Loop ");



for(num;num<=10;num++)

{

    if(num==5)

    {

    break;

    }

    document.write(num+"<br>");

}

document.write("Exciting Loop!");

 

Output









Continue

var num = 1;

document.write("Starting Loop ");

for(num;num<=10;num++)

{

    if(num==5)

    {

    continue;

    }

    document.write(num+"<br>");

}

document.write("Exciting Loop!");

 

Output










Labeled Loop

document.write("Entering the loop!<br /> ");

outerloop: // This is the label name

for (var i = 0; i < 3; i++)

{

 document.write("Outerloop: " + i + "<br />");

 for (var j = 0; j < 5; j++)

 {

 if (j == 3){

 continue outerloop;

}

 document.write("Innerloop: " + j + "<br />");

 }

}

document.write("Exiting the loop!<br /> ");

Output




0 comments:

Post a Comment