BizTalk Utilities CV ,   Jobs ,   Code library
 
Home Page


Add/Edit your code items
Search the code library
Browse for the code library


LAMP
What is the CreateEventSource method?
What is the CreateEventSource method?


 
 

<< JavaScriptMicrosoft ASP >>


By Roger Stuart
First Posted 03/17/2007
Times viewed 660

Control Structures


Summary 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:

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:

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:

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:

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:

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:

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:

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/


<< JavaScriptMicrosoft ASP >>





Leave a comment for this article
Your name
Your email (optional)
Your comment
Optional: Upload an attachment
Enter the code shown:

 
 

    Email TopXML