Looping Constructs
Previous Page | Home Page | Next Page |
---|
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:
Contents |
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) |
if(condition) |
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.
The output of the above examples are shown in below:

The switch Statement
The switch statement compares a value of the variable against other values. Multiple ifstatements can be replaced with switch.
switch(variablename) |
---|
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.
Output of the above example is as below:
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))
The output of the Example 8.6 shown in Figure 8.6 below:
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.
Previous Page | Home Page | Next Page |
---|