Virtual Open Schooling Online courses available at Virtual Open Schooling (VOS), for more details kindly click here!
NIOS Silver Jubilee Year
NIOS

Looping Constructs

From NIOS
Jump to: navigation, search
Previous Page Home Page Next Page
ICT Applications

Looping Constructs (Controlling Programming Flow)

The statements inside any program executes sequentially, lineby line. This flow of execution is called as sequential execution.This flow of execution can be controlled (for example: Execute aportion of a program only once, based on a condition. Execute aportion of the program repetitively, based on condition.) by usingprogramming constructs. This programming constructs typicallyare of two types:

Conditional

These constructs provide the facility to execute a portionof a program only once, based on a condition. Examples ofconditional constructs are: if, if...else and switch...case.

Iterative

These constructs provide the facility to execute a portionof the program repetitively, based on a condition. Examples of iterative constructs are: while, do...while and for loops.

Conditional Constructs (if, if...else & switch...case)

The if and if...else statementIf and if..else are the primary decision making constructs of anyprogramming language.

Syntax:

if(condition)
{
Statement1
Statement2
———————
———————
}
In this case, if a certain
condition evaluates to true,
than only the statement(s)
written inside the block gets
execute otherwise skips the block.

if(condition)
{
Statement(s)
}else
{
Statement(s)}
Here, if a certain condition
evaluatestotrue,thestatement(s) written inside the
block gets execute otherwise,
the statement(s) written inside
else block gets execute.

N.B: more than one condition can also be used in if and if..elsestatement with the help of && (logical end)and ||(logical or).


A sample program in first column of below table to find out the average marks secured by the student in 3 subjects, display the result PASS, only if average marks > 40.

A sample program in second column of below table to find outthe average marks secured by the student in3 subjects, display the result PASS ifaverage marks > 40 else FAIL.

Java Script Conditional example

The output of the above examples are shown in below:

Javascript5.png
Javascript6.png


The switch Statement

The switch statement compares a value of the variable against other values. Multiple ifstatements can be replaced with switch.

switch(variablename)
{
case value1:
statement(s)
break
case value2:
statement(s)
break
default:
statement(s)
}


value1, value2 may be a number or string, if string it should be written inside doublequote. The switch statement on execution checks if the value of variable matches withany case values i.e value1, value2 it executes the statement(s) under it. In case, it can notfind a matching case statement, it executes the default statement(s)


Below example is showing use of switch..case statement.

Javascript7.png

Output of the above example is as below:

Javascript8.png

Iterative Constructs (while, do...while and for loops)

The while statement

The while construct, is used to execute statement(s) as long as certain condition is true.

while(condition(s))
{
statement(s)
}

The do...while statement

This statement is very similar to the while statement, except that it first execute thestatement then checking the condition whereas in while first condition checking thenstatement execution. So in case of do... while, the statement(s) execute at least once evenif the condition is false.

do
{
Statement(s)
}while(condition(s))

Javascript9.png


The output of the Example 8.6 shown in Figure 8.6 below:

Javascript10.png

The for statement

The for, statement is also an iterative construct, used to execute statement(s) based onsome condition.


for(variable initialization;checking condition;change value of vriable)
{
Statement(s)
}

The for statement first initialize the variable with some value then, checks the condition.If the condition becomes true, it changes the value of the variable and the statement(s)within the braces are executed. One thing always remembers while changing the value ofvariable, the changes to the variable in such a way that at one point of time on checkingthe condition it should get false otherwise it would enter into infinite loop and theprogram will hang.

Javascript11.png
Javascript12.png
Previous Page Home Page Next Page
ICT Applications