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

Lecture 6

The document discusses repetition structures or loops in C++. It describes counter-controlled loops like for loops that repeat a number of times based on a counter variable. It also describes conditional loops like while and do-while loops that repeat until a condition is met. It provides examples of using for, while, do-while loops and explains break and continue statements that can be used within loops. Finally, it discusses nested loops where one loop is placed within another loop.

Uploaded by

Hassan Ali
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)
29 views

Lecture 6

The document discusses repetition structures or loops in C++. It describes counter-controlled loops like for loops that repeat a number of times based on a counter variable. It also describes conditional loops like while and do-while loops that repeat until a condition is met. It provides examples of using for, while, do-while loops and explains break and continue statements that can be used within loops. Finally, it discusses nested loops where one loop is placed within another loop.

Uploaded by

Hassan Ali
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/ 48

Repetition Structures

L E C T U R E 6

M A E M O O N A K A Y A N I
Repetition Structure

• Repetition Structure or Loops: Allows you to repeat a section of your


program a certain number of times

• Repeats until the condition remains true

• Terminates when the condition becomes false


Loops in C++
Loops
• Counter-controlled Loops

• Depends on the value of a variable known as counter variable. The counter


is changed (increased/decreased) in each iteration.

Example: for loop

• Conditional loop

• A conditional loop keeps repeating until a specific condition is met

Example: while and do loops


for Loop
for Loop - Example
(for loop) --Class Exercise-1
• Get a number form user and calculate its factorial
(for loop) --Class Exercise-2
• Write a program that ask the user to enter a number. The program should print the table of that
number (up to 10 values). Example…

• Enter a number: 7
(for loop) --Class Exercise-3
-Write a program that asks the user to enter two numbers (multiple of 10) : speed1, and
speed2 representing speeds in KPH (KilometersperHour). Then the program should convert
and show table of speeds in MPH(MilesperHour) for all the speed values between speed1
and speed2.

• MPH = KPH * 0.6214

• Speed1 and speed2 variables should be multiple of 10. Each table entry (in KPH) should
be updated by 5 in each iteration.
for loop –Multiple Expressions
(1) for loop –Multiple Expressions
(1) for loop -Variable Visibility
int main()

{
for(int j=0; j<10; j++)

k = j*j;

cout<<“\n Valueof k: “<<k;

// j = 23; cannot do this!

return 0;

}
(1) for loop –optional expressions
for loop

int i= 10;

for (cout<<“Starting…”; i; cout<<i<<endl)

--i;

Output?
While Loop
While Loop

• for loop does something a fixed number of times.

• If you don’t know how many times you want to do something


before you start the loop?

• In this case a different kind of loop may be used: the while loop
While loop syntax
Example: tracing a while loop
Example: tracing a while loop
Example: tracing a while loop
Example: tracing a while loop
Example: tracing a while loop
Example: tracing a while loop
Example: tracing a while loop
Example: tracing a while loop
Example: tracing a while loop
While loop
Example
While loop - Example
While loop - Example
do Loop
do Loop

•In while loop if condition is false it is never entered or executed

•Sometime, requirements are that the loop should be executed at least


once….

•For that, we use do loop, that guarantees at least on execution of the loop
body
do while loop - Syntax
do while loop - Syntax
int main( )
{

int counter, howmuch;

cin>>howmuch; counter = 0;

do {

counter++;

cout<<counter<<endl;

} while ( counter < howmuch);

return 0;

}
do Loop – Example 2
int main( )

{ intnum1, num2; char ch;

do {

cout<<“\n Enter a number:”;

cin>>num1;

cout<<“\n Enter another number:”;

cin>>num2;

cout<<“\nTheirsum is: “<<num1+num2;

cout<<“\n Do another time (y/n):”;

cin.get(ch);

} while(ch==‘y’);
break Statement

• break statement

–Immediate exit from while, for, do/while, (also used in switch)

–break immediately ends the loop that contains it.

• Common uses:

–Escape early from a loop

–Skip remainder part of the loop and exit


break Statement – Examples
(using break in loops) – Class Exercise 1

•Write a program which reads an integer n from the user, and


prints square value(n*n) for that number. Whenever ZERO is
entered by the user program should terminate by printing
“InvalidValue” message.
continue Statement

• continue statement

–Only ends the current iteration

–Skips remainder of loop body (in current iteration)

–Proceeds with next iteration of loop

•“continue” can only be inside loops (for, while, or do-while). IT CANNOT


BE USED IN “switch”
continue Statement - Example
(Nested Loops)Nested
Repetition Structures
(Nested Loop)

•In a nested repetition structure, one loop (inner loop) is placed


entirely within another loop (outer loop)

•In nested loops any loop (for, while, or do loop) can be placed
inside another loop which can be a for, while, or a do loop
(Nested Loops)- Example
(Nested Loops) - Example
(Nested Loops) –Exercise-1
-Write a program to print triangle of stars.
*

**

***

****

*****

******

*******

********

*********
(Nested Loops) –Exercise-2
-Write a program to print triangle of stars.

*********

********

*******

******

*****

****

***

**

*
(Nested Loops) –Exercise-3
-Write a program to print Rectangle based on two triangles (One using + and other using *
symbol).
+********

++*******

+++******

++++*****

+++++****

++++++***

+++++++**

++++++++*

+++++++++
(Nested Loops) –Exercise-4
-Write a program for a company to calculate total sales for 3 regions. Each region’s sales is
entered by user which is then summed in total regional sales. The program keep accepting
regional sales until it is not 0. The program prints the final regional sum for three regions
and exits.

Example Output:

Total Sales for Region 1 : 87645

Total Sales for Region 2 : 2312

Total Sales for Region 3 : 8874


Questions

You might also like