0% found this document useful (0 votes)
17 views31 pages

LOOPS

Covers the basic concepts of Loops in C++
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views31 pages

LOOPS

Covers the basic concepts of Loops in C++
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Friday, Sept

ember 6, 20
24

LOOPS
Flow of Control 2
Friday, September 6, 20
24

What are LOOPs?


 Loops are used to repeat a block of code.
Being able to have your program repeatedly
execute a block of code is one of the most
basic but useful tasks in programming
 many programs or websites that produce
extremely complex output are really only
executing a single task many times
Friday, September 6, 20
24

3 Types of Loops:
 While Loop
syntax: while (boolean expression/condition)
{ loop body }
 Do While Loop
syntax: do { loop body
} while (boolean
expression/condition);
 For Loop
syntax: for (initialize; condition; stepwise)
{ loop body }
Friday, September 6, 20
24

WHILE
while ()
{
}
Friday, September 6, 20
24

WHILE
cin >> countDown;
while ()
{
}
Friday, September 6, 20
24

WHILE
cin >> countDown;
while (countDown > 0)
{
}
Friday, September 6, 20
24

WHILE
cin >> countDown;
while (countDown > 0)
{
cout << “Hello”;
}
Friday, September 6, 20
24

WHILE
cin >> countDown;
while (countDown > 0)
{
cout << “Hello”;
--countDown;
}
Friday, September 6, 20
24

DO-WHILE
do {
} while ();
Friday, September 6, 20
24

DO-WHILE
cin >> countDown;
do {
} while ();
Friday, September 6, 20
24

DO-WHILE
cin >> countDown;
do {
} while (countDown > 0);
Friday, September 6, 20
24

DO-WHILE
cin >> countDown;
do {
cout << “Hello”;
} while (countDown > 0);
Friday, September 6, 20
24

DO-WHILE
cin >> countDown;
do {
cout << “Hello”;
--countDown;
} while (countDown > 0);
Friday, September 6, 20
24

FOR
for ( ; ; )
{
}
Friday, September 6, 20
24

FOR
for ( <initialize> ; <condition> ; <stepwise> )
{
<loop_body>
}
Friday, September 6, 20
24

FOR
cin >> countDown;
for ( ; ; )
{
}
Friday, September 6, 20
24

FOR
cin >> countDown;
for ( ; countDown > 0 ; countDown-- )
{
}
Friday, September 6, 20
24

FOR
cin >> countDown;
for ( ; countDown > 0 ; countDown-- )
{
cout << “Hello”;
}
Friday, September 6, 20
24

FOR (another example)


cout << “How many quizzes will be included? ”;
Friday, September 6, 20
24

FOR (another example)


cout << “How many quizzes will be included? ”;
cin >> quizMax;
Friday, September 6, 20
24

FOR (another example)


cout << “How many quizzes will be included? ”;
cin >> quizMax;
for ( ; ; )
{
}
Friday, September 6, 20
24

FOR (another example)


cout << “How many quizzes will be included? ”;
cin >> quizMax;
for ( int q=0; ; )
{
}
Friday, September 6, 20
24

FOR (another example)


cout << “How many quizzes will be included? ”;
cin >> quizMax;
for ( int q=0; q < quizMax; )
{
}
Friday, September 6, 20
24

FOR (another example)


cout << “How many quizzes will be included? ”;
cin >> quizMax;
for ( int q=0; q < quizMax; q++)
{
}
Friday, September 6, 20
24

FOR (another example)


cout << “How many quizzes will be included? ”;
cin >> quizMax;
for ( int q=0; q < quizMax; q++)
{
cout << “Enter quiz result: ”;
}
Friday, September 6, 20
24

FOR (another example)


cout << “How many quizzes will be included? ”;
cin >> quizMax;
for ( int q=0; q < quizMax; q++)
{
cout << “Enter quiz result: ”;
cin >> quiz;
}
Friday, September 6, 20
24

FOR (another example)


cout << “How many quizzes will be included? ”;
cin >> quizMax;
for ( int q=0; q < quizMax; q++)
{
cout << “Enter quiz result: ”;
cin >> quiz;
sum = sum + quiz;
}
Friday, September 6, 20
24

FOR (another example)


cout << “How many quizzes will be included? ”;
cin >> quizMax;
for ( int q=0; q < quizMax; q++)
{
cout << “Enter quiz result: ”;
cin >> quiz;
sum = sum + quiz;
}
average = sum / quizMax;
Friday, September 6, 20
24

FOR (another example)


cout << “How many quizzes will be included? ”;
cin >> quizMax;
for ( int q=0; q < quizMax; q++)
{
cout << “Enter quiz result: ”;
cin >> quiz;
sum = sum + quiz;
}
average = sum / quizMax;
cout << “Average is: ” << quizMax << endl;
Friday, September 6, 20
24

Determine the Output


int i, x=1;
for (i=1; i<10; i++)
{
do {
x = (x * -1) + i;
} while (x < 0);
cout << "x = " << x << endl;
}
Friday, September 6, 20
24

Let’s Try
Write a program that will read
in an integer N, then will
display the sum of the series 1
+ 2 + 3 + … + N.

You might also like