Error » Certification & Programming center Error !! » Programming tutorials » Control Structures

Programming tutorials All Knowledge Info and links to posted here

Post New Thread Reply
  Control Structures
LinkBack Thread Tools Display Modes
Old 03-Jan-2007, 10:25 PM   #1 (permalink)
Administrator
 
Anilrgowda's Avatar

Posts: 18,715
Join Date: Jan 2006
Rep Power: 10 Anilrgowda is on a distinguished road

IM:
Default Control Structures

Most of the programming languages use control structures to control the flow of a program. The control structures include decision-making and loops. Decision-making is done by applying different conditions in the program. If the conditions are true, the statements following the condition are executed. The values in a condition are compared by using the comparison operators. The loops are used to run a set of statements several times until a condition is met. If the condition is true, the loop is executed. If the condition becomes false, the loop is terminated and the control passes to the next statement that follows the loop block.

The control structures that are used in JavaScript are as follows:

The if statement: The if statement is a simple decision-making statement that is used to apply conditions in the program. If a condition is true, the statement in the if block is executed; otherwise, the statement following the if block is executed.

The syntax of the if statement is as follows:

if(condition)
{
//statements
}

The following example will demonstrate the use of the if statement:

<SCRIPT language=javascript>
function ifdemo()
{
var a=prompt("Enter a number")
var b= prompt("Enter a number")
if(a>b)
{
document.write(a)
}
}
</SCRIPT>

The above code will execute only if the value of the variable a is greater than the value of the variable b, and the value of the variable a will be printed.

The if-else statement: The if-else statement is used to apply a condition in the program. If the condition is true, the statement in the if block is executed. If the condition is false, the statement in the else block is executed.

The syntax for using the if-else statement is as follows:

if(condition)
{
//statements
}
else
{
//statements
}

The following example will demonstrate the use of the if else statement:

<SCRIPT language=javascript>
function ifelsedemo()
{
var a=prompt("Enter a number")
var b= prompt("Enter a number")
if(a>b)
{
document.write(a)
}
else
{
document.write(b)
}
}
</SCRIPT>

In the above code, if the value of the variable a is greater than the value of the variable b, the value of the variable a will be printed. If the value of the variable b is greater than the value of the variable a, the value of the variable b will be printed.

The if-else if statement: The if-else if statement is used to check several conditions. If any of the condition is true, the statement of that block is executed; otherwise, the value in the else block is executed.

The syntax of using the if-else if statement is as follows:

if(condition)
{
//statements
}
else if(condition)
{
//statements
}
else if(condition)
{
//statements
}
else
{
//statements
}

The following example will demonstrate the use of the if-else if statement:

<SCRIPT language=javascript>
function ifelseifdemo()
{
var a=parseFloat(prompt("Enter the marks in Math"))
var b=parseFloat(prompt("Enter the marks in Science"))
var c=parseFloat(prompt("Enter the marks in Geography"))
var d=parseFloat(prompt("Enter the marks in History"))
var e=parseFloat(prompt("Enter the marks in Commerce"))
var f=(a+b+c+d+e)*100/500
if(f<40)
{
alert("Fail")
}
else if(f>=40 && f<50)
{
alert("Fair")
}
else if(f>=50 && f<60)
{
alert("Good")
}
else if(f>=60 && f<75)
{
alert("Very Good")
}
else if(f>75)
{
alert("Excellent")
}
}
</SCRIPT>

In the above code, five conditions are given. If anyone of the condition is true, the statement following the block will be executed.

The conditional statement: The conditional statement can be used in the same way as the if else statement. The conditional statement is a single line statement that can be used in place of the if-else statement.

The syntax of using the conditional statement is as follows:

variablename=condition ? value1 : value2

If the condition is true, the statement following the question mark(?) will be executed. If the condition is false, the statement after the colon( is executed.

The following code will demonstrate the use of the conditional expression:

<SCRIPT language=javascript>
function condexp()
{
var a=prompt("Enter a number")
var b=prompt("Enter a number")
var c=a>b ? a : b
document.write(c)
}
</SCRIPT>

Loops: The loops are the statements that are used to run a set of statements several times until a condition is met. These statements can also be used to extract the elements of an array.

The loops that are used in JavaScript are as follows:

The for loop: The for loop is created by using the for keyword.

The syntax of using the for loop is as follows:

for(initialization; condition; increment/decrement)
{
//statements
}

In the above syntax, the initialization refers to the value that starts the loop. The condition is the conditional statement that is used to control the loop. If the condition is true, the loop continues. If the condition is false, the loop is terminated and the control passes to the next statement that follows the loop block. The increment or decrement is used to run the loop in forward or backward direction.

The following code will demonstrate the use of the for loop:

<SCRIPT language=javascript>
function fordemo()
{
var a
for(a=1; a<=10; a++)
{
document.write(a)
}
}
</SCRIPT>

The above code will print a series from 1 to 10.

The for in loop: The for in loop is the variation of the for loop. It is used to extract the properties of the object that are being used in the current browser window.

The syntax of using the for in loop is as follows:

for(counter in object)
{
//statements
}

The following code will demonstrate the use of the for in loop

< script language="javascript">
function forindemo()
{
var obj=document
var objname="window"
var result=""
var i
for(i in obj)
{
result+=objname+"."+i+"="+obj[i]+ "
"
}
document.write(result)
}


The while loop: The while loop is a statement that is used to run a statement or a set of statements several times until a condition is met. The while loop checks the condition and runs the loop until the condition is true. If the condition becomes false, the loop is terminated and the control goes to the next statement that follows.

The syntax of the while loop is as follows:

while(condition)
{
//statements
}

The following example will demonstrate the use of the while loop:

<SCRIPT language=javascript>
function whiledemo()
{
var num=0
while(num<=10)
{
document.write(num)
}
}
</SCRIPT>

The above statement will print a series from 1 to 10.

The do-while loop: The do-while loop is a statement that is used to run a statement or a set of statements several times until a condition is met. The do-while loop starts before checking a condition. After its first execution, it checks the condition and runs the loop until the condition is true. If the condition becomes false, the loop is terminated and the control goes to the next statement that follows.

The syntax of the do-while loop is as follows:

do
{
//statements
} while(condition)

The following example will demonstrate the use of the do-while loop:

<SCRIPT language=javascript>
function dowhiledemo()
{
var num=0
do
{
document.write(num)
}while(num<=10)
}
</SCRIPT>

The above statement will print the series from 1 to 10.

The break statement: The break statement is used to terminate a loop according to the given condition inside the loop.

The syntax of using the break statement is as follows:

for(initialization; condition; increment/decrement)
{
//statements
if(condition)
{
break
}
}

The following code will demonstrate the use of the break statement:

<SCRIPT language=javascript>
function breakdemo()
{
var a
for(a=1; a<=10; a++)
{
document.write(a)
if(a==5)
{
break
}
}
}
</SCRIPT>

The above code will print a series from 1 to 5.

The continue statement: The continue statement breaks the loop and starts the loop again with the next value.

The syntax of using the continue statement is as follows:

for(initialization; condition; increment/decrement)
{
//statements
if(condition)
{
continue
}
}

The following example will demonstrate the use of the continue statement:

<SCRIPT language=javascript>
function cont()
{
var a
for(a=0;a<10;a++)
{
if(a==5)
{
continue
}
document.write(a)
}
}
</SCRIPT>

In the above code, the series from 1 to 4 will be printed, the loop will break at 5, and again the series from 6 to 10 will be printed.

The with statement: The with statement helps the users to write shorter code related to an object. The properties and methods of that object can be invoked without writing the lengthy object reference. This can help to reduce the file size and also to check and debug the code.

The syntax of using the with statement is as follows:

with(object)
{
//statements
}

The following example will demonstrate the use of the with statement:

<SCRIPT language=javascript>
function mywith()
{
with(document.form1)
{
elements[0].value="text1"
elements[1].value="text2"
elements[2].value="text3"
elements[3].value="text4"
}
}
</SCRIPT>

The switch statement: The switch statement can be used for writing code that make decisions. Decision-making process is used to execute a program according to one or more conditions.

The syntax of using the switch statement is as follows:

switch(expression)
{
case label:
statements
[break]
case label:
statements
[break]
[default
statements
break]
}

In the above syntax, the break statement and the default statement are optional. The break statement stops the execution of the switch statement after the execution of a case statement. If the break statement is removed, all the case statements following the correct case statement will be executed.

The following code will demonstrate the use of the switch statement:

<SCRIPT language=javascript>
function myswitch()
{
var a=prompt("Enter the letters a,b,c")
switch(a)
{
case "a":
alert("a is typed")
break
case "b":
alert("b is typed ")
break
case "c":
alert("c is typed ")
break
default:
alert("It is a wrong character.")
break
}
}
</SCRIPT>

About the Author:

uCertify was formed in 1996 with an aim to offer high quality educational training software and services in the field of information technology to its customers. uCertify provides exam preparation solutions for the certification exams of Microsoft, CIW, CompTIA, Oracle, Sun and other leading IT vendors. To know more about uCertify, please visit http://www.ucertify.com/
Anilrgowda is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
   


   
Post New Thread Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -8. The time now is 09:58 AM.

Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0

DMCA Policy

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228