I.C.S Part 2 Computer Sciences

Chapter#1          Chapter#10          Chapter#11          Chapter#12

Chapter#12
LOOP CONSTRUCTORS


Loop Control Variable:-
A variable which is used to control the number of iteration of the loop is called loop control variable.
Loops:-
It is a control structure which is used to repeat set of statements up to a fixed numbers of times or until given condition is satisfied.
There are Two-Types of Loops.
i. Counter Loop
ii. Conditional Loop
Counter Loop:-
It is a control structure which is used to repeat set of statements up to a fixed number of times.
   for loop: -
 It is a counter loop.
 It repeats set of statements up to a fixed number of times.
 User knows in advance that how many times the loop will be executed.
 It consists of Two Parts.
i. Loop Header
ii. Loop Body
Loop Header:-
It is the first part of the for loop.
It consists of Three Expressions.
 i: Initialization
ii: Condition
iii: inc/dec
Initialization:-
• It is the first expression of loop header.
• In this expression, loop control variable is initialized.
• It is terminated by statement terminator.
• It is an optional part.
This part runs only for one time in the first iteration.
Condition:-
• After initialization, control is transferred to the conditional part.
• In this part, given condition is evaluated.
• If the condition is true, body of the loop is executed.
• If the condition is false, body of the loop is ignored and control is transferred to the statement which comes after the body of the loop.
• It is necessary for the condition of the loop to get false, otherwise loop becomes infinite loop.
• It is terminated by statement terminator.
Inc/Dec:-
• It is the third expression of the loop header.
• After executing the body of the loop control is transferred to this expression.
• This part is responsible to make the condition of the loop false.
• After inc/dec, condition is again tested.
Syntax Of for Loop:-

for(initialization;condition;inc/Dec)
{
--------------- //Body
---------------
}

Conditional Loop:-
• It is a control structure which is used to repeat set of statements until given condition is satisfied.
• There are two types of conditional loop
i. While Loop
ii. Do-While Loop

while Loop:-
• It is a conditional loop.
• It repeats set of statement until given condition is satisfied.
• It is possible in while loop, not to execute body of loop even for one time.
• It is also called top tester because it checks the condition at top and then execute the statements.
• If the condition is true, body of the loop will be executed.
• If the condition is false, body of the loop will be ignored.
• Inc/Dec expression is written inside the body of the loop.
Syntax Of while Loop:-
Initialization;
While(condition)
{
--------------- // Body
Inc/Dec;
}

do-While Loop:-
• It is a conditional loop.
• It repeats set of statement until given condition is satisfied.
• It is called bottom tester.
• It executes the body first and then checks the condition at the last.
• In this loop, body is executed at least one time.
• Two keywords are used in it
i. do
ii. while
• do represents the start of loop.
• while represent the end of the loop.
• while statement contains the condition and it is terminated by statement terminator.
• If the condition is true, control is transferred to the start of the loop.
• If the condition is false, control is transferred to the next statement after the loop.
Syntax Of do-while Loop:-
Initialization;
Do
{
-----------------
-----------------//Body
Inc/Dec;
}
while(condition);
Sentinel Control Loop:-
• A loop which uses sentinel value to end the loop is called sentinel controlled loop.
• A value which ends the loop is called sentinel value.
e.g:- [Y/N]
• It is normally a conditional loop.
Iteration:-
 Get a value
 Compare it with sentinel value.
 If the value is sentinel, it terminates the loop.
 If the value is not sentinel, then get the next value again.
Nested Loop:-
• Loop within the body of another loop is called nested loop.
• Nesting can be performs up to any level.
• Nesting increases the complexity of the program.
• Any loop can be placed within the body of another loop.
• For every iteration of the outer loop, inner loop runs completely.
• Outer loop controls the number of rows.
• Inner loop controls the number of columns.
Syntax Of Nested Loop:-
Loop(  )
{
           loop(   )
     {
-------------------
------------------
----------------- // Body Nested Loop
         }
}


Array:-
• It is the collection of variables, stored in a contagious portion of the memory having same name and same data type.
• Each portion of the memory is called element of the array.
• Each element is accessed by a unique number called subscript or index.
• Index always starts with 0.
• Range of index is = 0length-1.
Continue Statement:-
• This statement is normally used within the body of the loop.
• When this statement is executed, all the remaining statements are ignored and control is transferred to the start of loop for next iteration.
Syntax of continue statement:-
Following syntax is general syntax of continue statement.
for(a=1;a<=10;a++)
{
--------------
--------------
continue;
-----------------
--------------
-----------------
}
goto Statement:-
• This statement is used for un-conditional transfer of control from one place to another place in the program.
• Normally this statement is not preferred in the program.
Syntax Of goto Statement:-
Following syntax is a general syntax of goto statement.
-------------------
------------------
--------------
goto lable;
-------------
------------
lable:
-----------------
-----------------
---------------------
THE END
________________________________________
________________________________________________________________________________

0 comments:

Post a Comment