Please enable JavaScript.
Coggle requires JavaScript to display documents.
LOOPING STATEMENTS - Coggle Diagram
LOOPING STATEMENTS
Define Looping Statement
• Looping is a sequence of instructions that
is continually repeated until a certain
condition is reached.
• A loop is a way of repeating a statement a
number of times until some way of ending
the loop occurs.
List types of Looping
Statement
There are 3 types of looping statements:
- FOR
- WHILE
- DO-WHILE
Define FOR loop statement
• A for loop is a programming
language statement which allows code to
be repeatedly executed.
• A for loop is a kind of control statement for
specifying iteration.
Define WHILE loop statement
• A WHILE loop is a control
flow statement that allows code to be
executed repeatedly based on a
given Boolean condition.
• The WHILE loop can be thought of as a
repeating if statement.
Define DO-WHILE loop
statement
• A DO-WHILE loop is a control
flow statement that executes a block of
code at least once, and then repeatedly
executes the block, or not, depending on a
given Boolean condition at the end of the
block.
Why need for BREAK &
CONTINUE statements?
• C provides two commands to control how
we loop:
BREAK exit from loop or switch.
CONTINUE skip 1 iteration of loop.
• The BREAK statement allows you to exit a
loop from any point within its body,
bypassing its normal termination
expression.
• When the break statement is encountered
inside a loop, the loop is immediately
terminated, and program control resumes
at the next statement following the loop.
• The break statement can be used with all
three of C's loops. You can have as many
statements within a loop as you desire.
• It is generally best to use the break for
special purposes, not as your normal loop
exit.
• BREAK is also used in conjunction with
functions and case statements which have
been covered in previous session.
• The CONTINUE statement is somewhat
the opposite of the BREAK statement.
• It forces the next iteration of the loop to
take place, skipping any code in between
itself and the test condition of the loop.
• In WHILE and DO-WHILE loops,
a CONTINUE statement will cause control
to go directly to the test condition and then
continue the looping process.
• In the case of the FOR loop, the increment
part of the loop continues.
• One good use of CONTINUE is to restart a
statement sequence when an error occurs.
Why need for GOTO
statement?
• Using a label, a Goto Statement can also
create a loop by jumping backwards to a
label though this is generally discouraged
as a bad programming practice.
• For some complex code it allows a jump to
a common exit point that simplifies the
code.
Structure of FOR loop
statement
• FOR statements are often used to process
lists such a range of numbers.
• The general form of the FOR statement is
for(initialization statement; test expression; update statement)
{
code/s to be executed;
}
• For statement declaration as shown above can be
divided into 3 parts:
- Initialization statement In this we initialize the loop control
value to a starting value. Eg. X=0;
- Test expression In this evaluate the condition for the iteration
of the loop and loop repeat only when this is true. Eg. X<10;
- Update statement In this determine how the value of loop
control value changes with each iteration of the loop. It can be
increment, decrement or any mathematical expression. Eg. X++
• Statement in above form can be a single statement,
block statement or at blank statement.
• Also note that each part is separated by a semi colon.
• The initialization statement is executed
only once at the beginning of the for loop.
• Then the test expression is checked by the
program.
• If the test expression is false, for loop is
terminated. But if test expression is true
then the code/s inside body of for loop is
executed and then update expression is
updated.
• This process repeats until test expression
is false.
• But if test expression is true then the
code/s inside body of for loop is executed
and then update expression is updated.
• This process repeats until test expression
is false.
Structure of WHILE loop
statement
• The while statement is used to carry out
looping operations, in which a group of
statement is executed repeatedly, until
some condition has been satisfied.
• The general form of the while statement is
while ( conditional expression )
{
Statement 1;<increment loop counter>
}
• The WHILE construct consists of a block
of code and a condition/expression.
• The condition/expression is evaluated, and
if the condition/expression is true, the code
within the block is executed.
• This repeats until the condition/expression
becomes false.
Structure of DO-WHILE loop
statement
• DO-WHILE loops are useful for things that
want to loop at least once.
• The structure is
Do
{
Code to be execute
} while ( condition );
• Notice that the condition is tested at the
end of the block instead of the beginning,
so the block will be executed at least once.
• If the condition is true, we jump back to the
beginning of the block and execute it
again.
• A DO-WHILE loop is almost the same as a
while loop except that the loop body is
guaranteed to execute at least once.
• A WHILE loop says "Loop while the
condition is true, and execute this block of
code", whereas a DO-WHILE loop says
"Execute this block of code, and then
continue to loop while the condition is
true".
Differentiate the loop statements
• In loop the condition is tested first
and then the statements are executed if
the condition turns out to be true.
• In DO-WHILE the statements are executed
for the first time and then the conditions
are tested, if the condition turns out to be
true then the statements are executed
again.
• A DO-WHILE loop runs at least once even
though the the condition given is false
• In a WHILE loop the condition is first tested
and if it returns true then it goes in the loop
- entry control loop
• In a DO-WHILE loop the condition is
tested at the last - exit control loop
• FOR - Statement for allowing us to
repeat certain parts of the program until a
specified number of repetitions – allow to
determine the required number of
repetitions.
Explain the working of BREAK
• In C programming, break is used in
terminating the loop immediately after it is
encountered.
• The break statement can be used in
terminating all three loops FOR, WHILE
and DO-WHILE loops
• The BREAK statement in C programming
language has the following two usages:
- When the BREAK statement is encountered
inside a loop, the loop is immediately
terminated and program control resumes at
the next statement following the loop.
- It can be used to terminate a case in
the switch statement
• If you are using nested loops (i.e., one
loop inside another loop), the break
statement will stop the execution of the
innermost loop and start executing the
next line of code after the block.
• Syntax of break statement
break;
Explain the working of GOTO
• In C programming, GOTO statement is
used for altering the normal sequence of
program execution by transferring control
to some other part of the program.