0% found this document useful (0 votes)
70 views

Cont. Loop Types

Looping statements in object-oriented programming execute a sequence of statements repeatedly until a condition becomes false. There are three main types of loops: 1. The while loop, which evaluates a condition before processing the loop body. 2. The do-while loop, which is similar but evaluates the condition after processing the body. 3. The for loop, which allows looping using three expressions for initialization, condition, and increment. Care must be taken to ensure loops terminate to avoid infinite loops.

Uploaded by

Kainat Kabeer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Cont. Loop Types

Looping statements in object-oriented programming execute a sequence of statements repeatedly until a condition becomes false. There are three main types of loops: 1. The while loop, which evaluates a condition before processing the loop body. 2. The do-while loop, which is similar but evaluates the condition after processing the body. 3. The for loop, which allows looping using three expressions for initialization, condition, and increment. Care must be taken to ensure loops terminate to avoid infinite loops.

Uploaded by

Kainat Kabeer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Revising loop

 Looping Statements in oop which execute the sequence of


statements many times until the stated condition becomes false.
 A loop consists of two parts, a body of a loop and a control
statement.
 The control statement is a combination of some conditions that
direct the body of the loop to execute until the specified condition
becomes false.

The purpose of the loop is to repeat the same code a number of


times.
'three’ types of loop:

 1. The while loop

 2. The do-while loop

 3. The for loop


While Loop

 In while loop, a condition is evaluated before processing a body


of the loop. If a condition is true then and only then the body of
a loop is executed.
 A while loop is the most straightforward looping structure.
While loop syntax in OO programming language is as follows:
while (condition)
{
statements;
}
we use while loops in circumstances when beforehand we do not know the precise
number of times the body of the loop needs to run. The execution of the loop is
terminated based on the test condition.
Example of while loop
Display Numbers from 1 to 5
#include <iostream> ++i;
using namespace std; }
int main() { return 0;
int i = 1; }
while (i <= 5)
{
cout << i << " ";
Example of while loop
Do-While loop

 A do-while loop is similar to the while loop except that the


condition is always executed after the body of a loop. It is also
called an exit-controlled loop.

 Syntax of Do-While Loop in C:


do {
statements
}
while (expression);
Note: In a do-while loop, the condition is always executed after the body of a loop. It is also
called an exit-controlled loop.
do-while loop example
OUTPUT:
Example do-while:
OUTPUT:
Code Explanation:

1. Including the iostream header file in our code. It will allow us to read from and write to
the console.
2. Including the std namespace so as to use its classes and functions without calling it.
3. Calling the main() function inside which the logic of the program should be added.
4. The { marks start of body of the main() function.
5. Declaring two integer variables, num, and sum. The variable sum has been initialized
to 0.
6. Creating a do-while loop. The { marks start of loop body.
7. Printing the text “Enter a number:” on the console.
8. Reading user input from the console and storing the value in variable num. The cin
(console input) is a function that reads user input.
Code Explanation:

9. Adding the value of num to value of sum and storing result in variable sum.
10.The } marks the end of the loop body. The test expression has been added to the
end of the loop body. It tests whether the value entered by the user to make sure it’s
not 0. The != is the not equal to the operator. If the user enters a 0, the loop should
terminate.
11.Printing value of variable sum on the console alongside other text.
12.The main() function should return a value if the program runs fine.
13.End of the body of the main() function.
In an entry control loop, a
condition is checked before
executing the body of a loop. It is
also called as a pre-checking loop.

In an exit-controlled loop, a


condition is checked after executing
the body of a loop. It is also called
as a post-checking loop.
The ‘Infinite’ Loop

• A loop becomes infinite loop if a condition never becomes


false.
• The for loop is traditionally used for this purpose. Since none
of the three expressions that form the ‘for’ loop are required,
you can make an endless loop by leaving the conditional
expression empty.
#include <iostream> return 0;
using namespace std; }
int main () {
for( ; ; )
{
printf("This loop will run forever.\n");
}
Break Statement in C
 The break statement is used mainly in in the switch statement. It is also useful for immediately stopping a
loop.
 We consider the following program which introduces a break to exit a while loop:

#include <stdio.h>
int main() {
int num = 5;
while (num > 0) {
if (num == 3)
break;
printf("%d\n", num);
Output:
num--;
5
}} 4
Which loop to Select?
Selection of a loop is always a tough task for a programmer, to select a loop do
the following steps:

 Analyze the problem and check whether it requires a pre-test or a post-test


loop.

 If pre-test is required, use a while or for loop.

 If post-test is required, use a do-while loop.

You might also like