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

Conditional Statement & Loops

Control structures like conditional statements (if/else) and loops (while, do-while, for) are essential parts of programming languages. Conditional statements allow executing code based on boolean conditions, and loops allow repeating code execution. Common conditional statements include if, if/else, if/else if, and nested if statements. Common loops include while, do-while, and for loops. Switch statements allow executing different code blocks based on different case values. Break and continue statements allow early termination or skipping iterations in loops.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
513 views

Conditional Statement & Loops

Control structures like conditional statements (if/else) and loops (while, do-while, for) are essential parts of programming languages. Conditional statements allow executing code based on boolean conditions, and loops allow repeating code execution. Common conditional statements include if, if/else, if/else if, and nested if statements. Common loops include while, do-while, and for loops. Switch statements allow executing different code blocks based on different case values. Break and continue statements allow early termination or skipping iterations in loops.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Conditional Statement & Loops

Control structure is the essential part of programming language. Commonly used control structures are:

Selection statement If Ifelse Ifelse if Nested if While Do-while For Switch case Break Continue

Selection statement The syntax of if is


If(condition) Statement if the condition is true

The syntax of ifelse is


If(condition) Statement if the condition is true else Statement if the condition is false

The syntax of ifelse if is


If(condition) Statement if the condition is true else If(condition) Statement if the another condition is false else If(condition) Statement if the another condition is false else Statement

Nested if
If(condition) Statement if the condition is true If(condition) Statement if the condition is true

else Statement if the condition is false

If else statement
<html> <head> <title> if else Demo </title> </head> <body> <script type="text/javascript"> var a,b,c; a=10;b=20;c=30;

if(a>b)
{

if(a>c)
document.write("<h3> a is largest number </h3>"); else

document.write("<h3> c is largest number </h3>"); } else { if(b>c) document.write("<h3> b is largest number </h3>"); else document.write("<h3> c is largest number </h3>"); } </script> </body> </html>

<html> <head> <title> if else if Demo </title> </head> <body> <script type="text/javascript"> var marks; marks=80; if(marks<40) document.write("You are Failed"); else if(marks>=40 && marks<50) document.write("You are Passed");

If else if statement
else if(marks=50 && marks<60) document.write("You have got second class"); else if(marks>=60 && marks<66) document.write("You have got first class"); else document.write("You are Distinction Holder"); </script> </body> </html>

Display Even or Odd


<html> <head> <title> Odd or Even Demo </title> <script type="text/javascript"> function fun(str) { var num=Number(str); if(num%2==0) alert("Even number!!!"); else alert("Odd number!!!"); } </script> </head> <body> <script type="text/javascript"> var input_str=prompt("Enter some number",""); fun(input_str); </script> </body> </html>

Display the Message Using Time Condition <html>


<head> <title> Display Message </title> <script type="text/javascript"> function GreetMsg() { var today=new Date(); var h=today.getHours(); var m=today.getMinutes(); var s=today.getSeconds(); if(h<10)

alert("Good Morning!!!(Time"+h+":"+m +":"+s+")"); else alert("Have A Good Day!!!(Time"+h+":"+m+":"+ s+")"); } </script> </head> <body onload="GreetMsg()"> </body> </html>

While statement
Used to implement the iterative logic of the program. Syntax: Some initial condition; While(terminating condition) { Some statements; Stepping condition; }

<html> <head> <title> While Demo </title> </head> <body> <table border=1 align="center"> <script type="text/javascript"> i=1; while(i<=10)

while(i<=10) { document.write("<tre><td>" +i+"</td><td>"+(i*i)+"</t d></tr>"); i++; } </script> </head> <body onload="GreetMsg()"> </body> </html>

Do While statement
Syntax:
Do { }while(condition)

<html> <head> <title> Do-While Demo </title> </head> <body> <script type="text/javascript"> counter=1; do

{ document.write("This statement number:"+counter); document.write("<br>"); counter++; }while(counter<=5); </script> </body> </html>

For loop statement


Syntax:
For(initial condition; terminating condition; stepping condition)

<html> <head> <title> for-loop Demo </title> </head> <body> <table border=1 align="center"> <th>Number</th><th>Square< /th> <script type="text/javascript">

for(i=1;i<=10;i++) { document.write("<tr><td>"+i+" </td><td>"+(i*i)+"</td></tr> "); } </script> </table> </body> </html>

Switch statement
Syntax: Switch(ch) { Case 0: statement1; Break; Case 0: statement2; Break; Case 0: statement3; Break; }

<html> <head> <title> switch case Demo </title> </head> <body> <script type="text/javascript"> d=new Date(); ch=d.getMonth(); switch(ch) { Case 1: document.write("January"); Break; Case 2: document.write("February"); Break; Case 3: document.write("March"); Break; Case 4: document.write("April"); Break; Case 5:

document.write("May"); Break; Case 6: document.write("june"); Break; Case 7: document.write("july"); Break; Case 8: document.write("august"); Break; Case 9: document.write("september"); Break; Case 10: document.write("october"); Break; Case 11: document.write("november"); Break; Case 12: document.write("december"); Break; } </script> </body> </html>

Break statement
<html> <head> <title> Break Demo </title> </head> <body> <script type="text/javascript"> for(i=10;i>=0;i--) { if(i==5) break; } document.write("My Lucky Number is"+i); </script> </body> </html>

Break statement
<html> <head> <title> Continue Demo </title> </head> <body> <script type="text/javascript"> for(i=10;i>=0;i--) { if(i==5) { x=i; continue; } document.write(i); document.write("<br>"); } document.write("The Number "+x+" is missing in above list"); </script> </body> </html>

You might also like