Copyright © 2012 Pearson Education, Inc.
Chapter 3 P2:
Loops
Copyright © 2012 Pearson Education, Inc.
Outlines
3.2.1 Increment & Decrement Operators
3.2.2 Introduction to loops
3.2.3 Classification of loops
3.2.3.1 for loop
3.2.3.2 while loop
3.2.3.3 do-while loop
3.2.3.4 nested loop
3.2.4 Jumping Statements & their types
3.2.5 Continue statement & Break statement
Copyright © 2012 Pearson Education, Inc.
3.2.1
The Increment and Decrement
Operators
Copyright © 2012 Pearson Education, Inc.
The Increment and Decrement
Operators
• ++ is the increment operator.
It adds one to a variable.
val++; is the same as val = val + 1;
• ++ can be used before (prefix) or after (postfix) a
variable:
++val; val++;
Copyright © 2012 Pearson Education, Inc.
The Increment and Decrement
Operators
• -- is the decrement operator.
It subtracts one from a variable.
val--; is the same as val = val - 1;
• -- can be also used before (prefix) or after
(postfix) a variable:
--val; val--;
Copyright © 2012 Pearson Education, Inc.
Increment and Decrement
Operators in Program 5-1
Continued…
Copyright © 2012 Pearson Education, Inc.
Increment and Decrement
Operators in Program 5-1
Copyright © 2012 Pearson Education, Inc.
Prefix vs. Postfix
• ++ and -- operators can be used in
complex statements and expressions
• In prefix mode (++val, --val) the
operator increments or decrements, then
returns the value of the variable
• In postfix mode (val++, val--) the
operator returns the value of the variable,
then increments or decrements
Copyright © 2012 Pearson Education, Inc.
Prefix vs. Postfix - Examples
int num, val = 12;
cout << val++; // displays 12,
// val is now 13;
cout << ++val; // sets val to 14,
// then displays it
num = --val; // sets val to 13,
// stores 13 in num
num = val--; // stores 13 in num,
// sets val to 12
Copyright © 2012 Pearson Education, Inc.
Notes on Increment and Decrement
• Can be used in expressions:
result = num1++ + --num2;
• Must be applied to something that has a location
in memory. Cannot have:
result = (num1 + num2)++;
• Can be used in relational expressions:
if (++num > limit)
pre- and post-operations will cause different
comparisons
Copyright © 2012 Pearson Education, Inc.
3.2.2
Introduction to loops
Copyright © 2012 Pearson Education, Inc.
Introduction to Loops
• Loop: is a control structure that allows you to
repeat a block of code multiple times.
• This is useful when you want to perform
repetitive tasks, such as iterating over a range of
numbers or processing each item in a collection.
• The loop statements allow a set of instructions to
be performed repeatedly until a certain condition
is fulfilled
Copyright © 2012 Pearson Education, Inc.
3.2.3
Classification of loops
Copyright © 2012 Pearson Education, Inc.
Classification of Loops
There are several types of loops, each serving different use
cases:
1.for loop: Used when the number of iterations is known ahead
of time.
2.while loop: Used when the number of iterations is unknown,
and the loop continues as long as a condition remains true.
3.do-while loop: Similar to the while loop but guarantees that
the loop body executes at least once, as the condition is
checked after the loop body.
4.nested loop is a loop that exists within another loop. In other
words, you have one loop (inner loop) inside the body of
another loop (outer loop)
Copyright © 2012 Pearson Education, Inc.
3.2.3.1
The For Loop
Copyright © 2012 Pearson Education, Inc.
The for Loop
• Useful for counter-controlled loop
• General Format:
for(initialization; test; update)
statement; // or block in { }
• No semicolon after the update expression or
after the )
Copyright © 2012 Pearson Education, Inc.
for Loop - Mechanics
for(initialization; test; update)
statement; // or block in
{ }
1) Perform initialization
2) Evaluate test expression
– If true, execute statement
– If false, terminate loop execution
3) Execute update, then re-evaluate test
expression
Copyright © 2012 Pearson Education, Inc.
for Loop - Example
int count;
for (count = 1; count <= 5; count++)
cout << "Hello" << endl;
Copyright © 2012 Pearson Education, Inc.
A Closer Look
at the Previous Example
Copyright © 2012 Pearson Education, Inc.
Flowchart for the Previous Example
Copyright © 2012 Pearson Education, Inc.
A for Loop in Program 5-9
Continued…
Copyright © 2012 Pearson Education, Inc.
A for Loop in Program 5-9
Copyright © 2012 Pearson Education, Inc.
A Closer Look at Lines 15 through
16 in Program 5-9
Copyright © 2012 Pearson Education, Inc.
Flowchart for Lines 15 through 16
in Program 5-9
Copyright © 2012 Pearson Education, Inc.
When to Use the for Loop
• In any situation that clearly requires
– an initialization
– a false condition to stop the loop
– an update to occur at the end of each iteration
Copyright © 2012 Pearson Education, Inc.
The for Loop is a Pretest Loop
• The for loop tests its test expression
before each iteration, so it is a pretest
loop.
• The following loop will never iterate:
for (count = 11; count <= 10; count++)
cout << "Hello" << endl;
Copyright © 2012 Pearson Education, Inc.
3.2.3.2
The while Loop
Copyright © 2012 Pearson Education, Inc.
The while Loop
• General format of the while loop:
while (expression)
statement;
• statement; can also be a block of
statements enclosed in { }
Copyright © 2012 Pearson Education, Inc.
The while Loop – How It Works
while (expression)
statement;
• expression is evaluated
– if true, then statement is executed, and
expression is evaluated again
– if false, then the loop is finished and
program statements following statement
execute
Copyright © 2012 Pearson Education, Inc.
The Logic of a while Loop
Copyright © 2012 Pearson Education, Inc.
The while loop in Program 5-3
Copyright © 2012 Pearson Education, Inc.
How the while Loop in Program 5-
3 Lines 9 through 13 Works
Copyright © 2012 Pearson Education, Inc.
Flowchart of the while Loop in
Program 5-3
Copyright © 2012 Pearson Education, Inc.
The while Loop is a Pretest Loop
expression is evaluated before the
loop executes. The following loop will
never execute:
int number = 6;
while (number <= 5)
{
cout << "Hellon";
number++;
}
Copyright © 2012 Pearson Education, Inc.
Watch Out for Infinite Loops
• The loop must contain code to make
expression become false
• Otherwise, the loop will have no way of
stopping
• Such a loop is called an infinite loop,
because it will repeat an infinite number of
times
Copyright © 2012 Pearson Education, Inc.
Example of an Infinite Loop
int number = 1;
while (number <= 5)
{
cout << "Hellon";
}
Copyright © 2012 Pearson Education, Inc.
3.2.3.3
The do-while Loop
Copyright © 2012 Pearson Education, Inc.
The do-while Loop
• do-while: a posttest loop – execute the loop,
then test the expression
• General Format:
do
statement; // or block in { }
while (expression);
• Note that a semicolon is required after
(expression)
Copyright © 2012 Pearson Education, Inc.
The Logic of a do-while Loop
Copyright © 2012 Pearson Education, Inc.
An Example do-while Loop
int x = 1;
do
{
cout << x << endl;
} while(x < 0);
Although the test expression is false, this loop will
execute one time because do-while is a posttest
loop.
Copyright © 2012 Pearson Education, Inc.
A do-while Loop in Program 5-7
Continued…
Copyright © 2012 Pearson Education, Inc.
A do-while Loop in Program 5-7
Copyright © 2012 Pearson Education, Inc.
do-while Loop Notes
• Loop always executes at least once
• Execution continues as long as
expression is true, stops repetition
when expression becomes false
• Useful in menu-driven programs to bring
user back to menu to make another choice
(see Program 5-8 on pages 245-246)
Copyright © 2012 Pearson Education, Inc.
3.2.3.4
The nested Loop
Copyright © 2012 Pearson Education, Inc.
Nested Loop
 A nested loop is a loop within another loop.
 This construct is useful for iterating over multiple dimensions
of data or performing repetitive tasks that require multiple
levels of iteration.
 Syntax
for (initialization1; condition1; increment1) {
for (initialization2; condition2; increment2) {
// Code to be executed
}
}
Copyright © 2012 Pearson Education, Inc.
Example
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
cout << i * j << "t";
}
cout << endl;
}
return 0;
}
Copyright © 2012 Pearson Education, Inc.
Explanation
1.Outer Loop: Iterates over rows (1 to 10).
2.Inner Loop: Iterates over columns (1 to 10) for each row.
3.Multiplication: Inside the inner loop, the product of i and j
is calculated and printed.
4.Newline: After each row, a newline character is printed to
move to the next line.
Copyright © 2012 Pearson Education, Inc.
3.2.4
Jumping statements & Their Types
Copyright © 2012 Pearson Education, Inc.
Breaking Out of a Loop
 In C++, jump statements are used to transfer control to
different parts of a program.
 These statements allow you to exit loops, skip over parts
of code, or move to a specific point within the program.
C++ provides four main types of jump statements:
1. break statement
2. continue statement
3. goto statement
4. return statement
Copyright © 2012 Pearson Education, Inc.
3.2.5
Breaking and Continuing a Loop
Copyright © 2012 Pearson Education, Inc.
Breaking Out of a Loop
The break statement is used to exit a loop or switch statement
immediately.
When a break statement is encountered, control jumps to the next
statement following the loop or switch. Example
•int main() {
• for (int i = 1; i <= 5; i++) {
• if (i == 3) {
• break; // exits the loop when i is 3
• }
• cout << i << " ";
• }
• return 0;
•}
Copyright © 2012 Pearson Education, Inc.
The continue Statement
The continue statement skips the current iteration of a loop and
moves directly to the next iteration.
Unlike break, it does not terminate the loop but rather skips the
rest of the code in the current iteration. Example
•int main() {
• for (int i = 1; i <= 5; i++) {
• if (i == 3) {
• continue; // skips the rest of the loop when i is 3
• }
• cout << i << " ";
• }
• return 0;
•}
Copyright © 2012 Pearson Education, Inc.
1 0
END

More Related Content

PDF
Loops and Files
PPT
Cso gaddis java_chapter4
PDF
Chapter_05HGUYUYGUYGUTFTIVVGUTFGIHGHYIYUIGY
PPT
Eo gaddis java_chapter_05_5e
PPT
Eo gaddis java_chapter_05_5e
PPT
M C6java6
PPTX
slidesgo-understanding-the-for-loop-in-c-principles-and-applications-20241116...
PPT
Different loops in C
Loops and Files
Cso gaddis java_chapter4
Chapter_05HGUYUYGUYGUTFTIVVGUTFGIHGHYIYUIGY
Eo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5e
M C6java6
slidesgo-understanding-the-for-loop-in-c-principles-and-applications-20241116...
Different loops in C

Similar to C++ CH3-P2 using c++ in all other parts.ppt (20)

PPT
Chapter 5 Looping
PPTX
complier design unit 5 for helping students
PDF
Introduction to c first week slides
PPTX
Presentation1
PPT
Control structures repetition
PPT
Lecture 2
PPTX
Lesson 1 of c programming algorithms and flowcharts.pptx
PDF
Chapter 12 Computer Science ( ICS 12).pdf
PPTX
D1basicstoloopscppforbeginnersgodoit.pptx
PDF
Week04
PPTX
Cs1123 4 variables_constants
PPTX
control statements
PPTX
Loops c++
PPT
Decision making and loop in C#
PDF
Core Java Programming Language (JSE) : Chapter IV - Expressions and Flow Cont...
PPTX
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
PPTX
Chap3java5th
PDF
Java chapter 5
PPT
ch5.ppt
PPTX
etlplooping-170320213203.pptx
Chapter 5 Looping
complier design unit 5 for helping students
Introduction to c first week slides
Presentation1
Control structures repetition
Lecture 2
Lesson 1 of c programming algorithms and flowcharts.pptx
Chapter 12 Computer Science ( ICS 12).pdf
D1basicstoloopscppforbeginnersgodoit.pptx
Week04
Cs1123 4 variables_constants
control statements
Loops c++
Decision making and loop in C#
Core Java Programming Language (JSE) : Chapter IV - Expressions and Flow Cont...
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Chap3java5th
Java chapter 5
ch5.ppt
etlplooping-170320213203.pptx
Ad

More from MutacalimMohamed (20)

PPTX
HEALTH SERVICE DELIVERY education w.pptx
PPTX
Presentation1 in real word education .pptx
PPTX
New Microsoft PowerPoint Presentation (2).pptx
PPTX
history taking in real world countires .pptx
PPTX
manternal ppt in health science by .pptx
PPTX
Places & Directions English daily uses.pptx
DOCX
Medical Nursing Parasitic Diseases in real.docx
PPTX
New Microsoft PowerPoint Presentation (2).pptx
PPTX
Activities that holds animals in sw.pptx
PPTX
Activities that holds animals in china .pptx
PPT
C++ CH4 in funtion using c++ in all ab.ppt
PPTX
ETHICS by using computing hello in .pptx
PPTX
Presentation2 in human that cases by.pptx
PPTX
Nursing Proposal Defence Presentation.pptx
PPT
ANTIBIOTIC RESISTANCE in whole peolple.ppt
PPT
9- Single Area OSPF on in networking.ppt
PPT
5A- ethernet protocol in engineering.ppt
PPTX
complteyl proposal in GU university.ppt.pptx
PPTX
COMMUNICABLE DISEASE in gu university.pptx
PPTX
project proposal writing in easy way.pptx
HEALTH SERVICE DELIVERY education w.pptx
Presentation1 in real word education .pptx
New Microsoft PowerPoint Presentation (2).pptx
history taking in real world countires .pptx
manternal ppt in health science by .pptx
Places & Directions English daily uses.pptx
Medical Nursing Parasitic Diseases in real.docx
New Microsoft PowerPoint Presentation (2).pptx
Activities that holds animals in sw.pptx
Activities that holds animals in china .pptx
C++ CH4 in funtion using c++ in all ab.ppt
ETHICS by using computing hello in .pptx
Presentation2 in human that cases by.pptx
Nursing Proposal Defence Presentation.pptx
ANTIBIOTIC RESISTANCE in whole peolple.ppt
9- Single Area OSPF on in networking.ppt
5A- ethernet protocol in engineering.ppt
complteyl proposal in GU university.ppt.pptx
COMMUNICABLE DISEASE in gu university.pptx
project proposal writing in easy way.pptx
Ad

Recently uploaded (20)

PDF
BSc-Zoology-02Sem-DrVijay-Comparative anatomy of vertebrates.pdf
PPTX
4. Diagnosis and treatment planning in RPD.pptx
PDF
Physical pharmaceutics two in b pharmacy
PPTX
Power Point PR B.Inggris 12 Ed. 2019.pptx
PDF
anganwadi services for the b.sc nursing and GNM
PDF
African Communication Research: A review
PDF
CHALLENGES FACED BY TEACHERS WHEN TEACHING LEARNERS WITH DEVELOPMENTAL DISABI...
PDF
GIÁO ÁN TIẾNG ANH 7 GLOBAL SUCCESS (CẢ NĂM) THEO CÔNG VĂN 5512 (2 CỘT) NĂM HỌ...
PPTX
growth and developement.pptxweeeeerrgttyyy
PPTX
IT infrastructure and emerging technologies
PPTX
ACFE CERTIFICATION TRAINING ON LAW.pptx
PDF
Review of Related Literature & Studies.pdf
PPTX
Approach to a child with acute kidney injury
PDF
POM_Unit1_Notes.pdf Introduction to Management #mba #bba #bcom #bballb #class...
PDF
Diabetes Mellitus , types , clinical picture, investigation and managment
PDF
Compact First Student's Book Cambridge Official
PPTX
Key-Features-of-the-SHS-Program-v4-Slides (3) PPT2.pptx
PPTX
Neurology of Systemic disease all systems
PDF
FYJC - Chemistry textbook - standard 11.
PDF
Laparoscopic Imaging Systems at World Laparoscopy Hospital
BSc-Zoology-02Sem-DrVijay-Comparative anatomy of vertebrates.pdf
4. Diagnosis and treatment planning in RPD.pptx
Physical pharmaceutics two in b pharmacy
Power Point PR B.Inggris 12 Ed. 2019.pptx
anganwadi services for the b.sc nursing and GNM
African Communication Research: A review
CHALLENGES FACED BY TEACHERS WHEN TEACHING LEARNERS WITH DEVELOPMENTAL DISABI...
GIÁO ÁN TIẾNG ANH 7 GLOBAL SUCCESS (CẢ NĂM) THEO CÔNG VĂN 5512 (2 CỘT) NĂM HỌ...
growth and developement.pptxweeeeerrgttyyy
IT infrastructure and emerging technologies
ACFE CERTIFICATION TRAINING ON LAW.pptx
Review of Related Literature & Studies.pdf
Approach to a child with acute kidney injury
POM_Unit1_Notes.pdf Introduction to Management #mba #bba #bcom #bballb #class...
Diabetes Mellitus , types , clinical picture, investigation and managment
Compact First Student's Book Cambridge Official
Key-Features-of-the-SHS-Program-v4-Slides (3) PPT2.pptx
Neurology of Systemic disease all systems
FYJC - Chemistry textbook - standard 11.
Laparoscopic Imaging Systems at World Laparoscopy Hospital

C++ CH3-P2 using c++ in all other parts.ppt

  • 1. Copyright © 2012 Pearson Education, Inc. Chapter 3 P2: Loops
  • 2. Copyright © 2012 Pearson Education, Inc. Outlines 3.2.1 Increment & Decrement Operators 3.2.2 Introduction to loops 3.2.3 Classification of loops 3.2.3.1 for loop 3.2.3.2 while loop 3.2.3.3 do-while loop 3.2.3.4 nested loop 3.2.4 Jumping Statements & their types 3.2.5 Continue statement & Break statement
  • 3. Copyright © 2012 Pearson Education, Inc. 3.2.1 The Increment and Decrement Operators
  • 4. Copyright © 2012 Pearson Education, Inc. The Increment and Decrement Operators • ++ is the increment operator. It adds one to a variable. val++; is the same as val = val + 1; • ++ can be used before (prefix) or after (postfix) a variable: ++val; val++;
  • 5. Copyright © 2012 Pearson Education, Inc. The Increment and Decrement Operators • -- is the decrement operator. It subtracts one from a variable. val--; is the same as val = val - 1; • -- can be also used before (prefix) or after (postfix) a variable: --val; val--;
  • 6. Copyright © 2012 Pearson Education, Inc. Increment and Decrement Operators in Program 5-1 Continued…
  • 7. Copyright © 2012 Pearson Education, Inc. Increment and Decrement Operators in Program 5-1
  • 8. Copyright © 2012 Pearson Education, Inc. Prefix vs. Postfix • ++ and -- operators can be used in complex statements and expressions • In prefix mode (++val, --val) the operator increments or decrements, then returns the value of the variable • In postfix mode (val++, val--) the operator returns the value of the variable, then increments or decrements
  • 9. Copyright © 2012 Pearson Education, Inc. Prefix vs. Postfix - Examples int num, val = 12; cout << val++; // displays 12, // val is now 13; cout << ++val; // sets val to 14, // then displays it num = --val; // sets val to 13, // stores 13 in num num = val--; // stores 13 in num, // sets val to 12
  • 10. Copyright © 2012 Pearson Education, Inc. Notes on Increment and Decrement • Can be used in expressions: result = num1++ + --num2; • Must be applied to something that has a location in memory. Cannot have: result = (num1 + num2)++; • Can be used in relational expressions: if (++num > limit) pre- and post-operations will cause different comparisons
  • 11. Copyright © 2012 Pearson Education, Inc. 3.2.2 Introduction to loops
  • 12. Copyright © 2012 Pearson Education, Inc. Introduction to Loops • Loop: is a control structure that allows you to repeat a block of code multiple times. • This is useful when you want to perform repetitive tasks, such as iterating over a range of numbers or processing each item in a collection. • The loop statements allow a set of instructions to be performed repeatedly until a certain condition is fulfilled
  • 13. Copyright © 2012 Pearson Education, Inc. 3.2.3 Classification of loops
  • 14. Copyright © 2012 Pearson Education, Inc. Classification of Loops There are several types of loops, each serving different use cases: 1.for loop: Used when the number of iterations is known ahead of time. 2.while loop: Used when the number of iterations is unknown, and the loop continues as long as a condition remains true. 3.do-while loop: Similar to the while loop but guarantees that the loop body executes at least once, as the condition is checked after the loop body. 4.nested loop is a loop that exists within another loop. In other words, you have one loop (inner loop) inside the body of another loop (outer loop)
  • 15. Copyright © 2012 Pearson Education, Inc. 3.2.3.1 The For Loop
  • 16. Copyright © 2012 Pearson Education, Inc. The for Loop • Useful for counter-controlled loop • General Format: for(initialization; test; update) statement; // or block in { } • No semicolon after the update expression or after the )
  • 17. Copyright © 2012 Pearson Education, Inc. for Loop - Mechanics for(initialization; test; update) statement; // or block in { } 1) Perform initialization 2) Evaluate test expression – If true, execute statement – If false, terminate loop execution 3) Execute update, then re-evaluate test expression
  • 18. Copyright © 2012 Pearson Education, Inc. for Loop - Example int count; for (count = 1; count <= 5; count++) cout << "Hello" << endl;
  • 19. Copyright © 2012 Pearson Education, Inc. A Closer Look at the Previous Example
  • 20. Copyright © 2012 Pearson Education, Inc. Flowchart for the Previous Example
  • 21. Copyright © 2012 Pearson Education, Inc. A for Loop in Program 5-9 Continued…
  • 22. Copyright © 2012 Pearson Education, Inc. A for Loop in Program 5-9
  • 23. Copyright © 2012 Pearson Education, Inc. A Closer Look at Lines 15 through 16 in Program 5-9
  • 24. Copyright © 2012 Pearson Education, Inc. Flowchart for Lines 15 through 16 in Program 5-9
  • 25. Copyright © 2012 Pearson Education, Inc. When to Use the for Loop • In any situation that clearly requires – an initialization – a false condition to stop the loop – an update to occur at the end of each iteration
  • 26. Copyright © 2012 Pearson Education, Inc. The for Loop is a Pretest Loop • The for loop tests its test expression before each iteration, so it is a pretest loop. • The following loop will never iterate: for (count = 11; count <= 10; count++) cout << "Hello" << endl;
  • 27. Copyright © 2012 Pearson Education, Inc. 3.2.3.2 The while Loop
  • 28. Copyright © 2012 Pearson Education, Inc. The while Loop • General format of the while loop: while (expression) statement; • statement; can also be a block of statements enclosed in { }
  • 29. Copyright © 2012 Pearson Education, Inc. The while Loop – How It Works while (expression) statement; • expression is evaluated – if true, then statement is executed, and expression is evaluated again – if false, then the loop is finished and program statements following statement execute
  • 30. Copyright © 2012 Pearson Education, Inc. The Logic of a while Loop
  • 31. Copyright © 2012 Pearson Education, Inc. The while loop in Program 5-3
  • 32. Copyright © 2012 Pearson Education, Inc. How the while Loop in Program 5- 3 Lines 9 through 13 Works
  • 33. Copyright © 2012 Pearson Education, Inc. Flowchart of the while Loop in Program 5-3
  • 34. Copyright © 2012 Pearson Education, Inc. The while Loop is a Pretest Loop expression is evaluated before the loop executes. The following loop will never execute: int number = 6; while (number <= 5) { cout << "Hellon"; number++; }
  • 35. Copyright © 2012 Pearson Education, Inc. Watch Out for Infinite Loops • The loop must contain code to make expression become false • Otherwise, the loop will have no way of stopping • Such a loop is called an infinite loop, because it will repeat an infinite number of times
  • 36. Copyright © 2012 Pearson Education, Inc. Example of an Infinite Loop int number = 1; while (number <= 5) { cout << "Hellon"; }
  • 37. Copyright © 2012 Pearson Education, Inc. 3.2.3.3 The do-while Loop
  • 38. Copyright © 2012 Pearson Education, Inc. The do-while Loop • do-while: a posttest loop – execute the loop, then test the expression • General Format: do statement; // or block in { } while (expression); • Note that a semicolon is required after (expression)
  • 39. Copyright © 2012 Pearson Education, Inc. The Logic of a do-while Loop
  • 40. Copyright © 2012 Pearson Education, Inc. An Example do-while Loop int x = 1; do { cout << x << endl; } while(x < 0); Although the test expression is false, this loop will execute one time because do-while is a posttest loop.
  • 41. Copyright © 2012 Pearson Education, Inc. A do-while Loop in Program 5-7 Continued…
  • 42. Copyright © 2012 Pearson Education, Inc. A do-while Loop in Program 5-7
  • 43. Copyright © 2012 Pearson Education, Inc. do-while Loop Notes • Loop always executes at least once • Execution continues as long as expression is true, stops repetition when expression becomes false • Useful in menu-driven programs to bring user back to menu to make another choice (see Program 5-8 on pages 245-246)
  • 44. Copyright © 2012 Pearson Education, Inc. 3.2.3.4 The nested Loop
  • 45. Copyright © 2012 Pearson Education, Inc. Nested Loop  A nested loop is a loop within another loop.  This construct is useful for iterating over multiple dimensions of data or performing repetitive tasks that require multiple levels of iteration.  Syntax for (initialization1; condition1; increment1) { for (initialization2; condition2; increment2) { // Code to be executed } }
  • 46. Copyright © 2012 Pearson Education, Inc. Example #include <iostream> using namespace std; int main() { for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) { cout << i * j << "t"; } cout << endl; } return 0; }
  • 47. Copyright © 2012 Pearson Education, Inc. Explanation 1.Outer Loop: Iterates over rows (1 to 10). 2.Inner Loop: Iterates over columns (1 to 10) for each row. 3.Multiplication: Inside the inner loop, the product of i and j is calculated and printed. 4.Newline: After each row, a newline character is printed to move to the next line.
  • 48. Copyright © 2012 Pearson Education, Inc. 3.2.4 Jumping statements & Their Types
  • 49. Copyright © 2012 Pearson Education, Inc. Breaking Out of a Loop  In C++, jump statements are used to transfer control to different parts of a program.  These statements allow you to exit loops, skip over parts of code, or move to a specific point within the program. C++ provides four main types of jump statements: 1. break statement 2. continue statement 3. goto statement 4. return statement
  • 50. Copyright © 2012 Pearson Education, Inc. 3.2.5 Breaking and Continuing a Loop
  • 51. Copyright © 2012 Pearson Education, Inc. Breaking Out of a Loop The break statement is used to exit a loop or switch statement immediately. When a break statement is encountered, control jumps to the next statement following the loop or switch. Example •int main() { • for (int i = 1; i <= 5; i++) { • if (i == 3) { • break; // exits the loop when i is 3 • } • cout << i << " "; • } • return 0; •}
  • 52. Copyright © 2012 Pearson Education, Inc. The continue Statement The continue statement skips the current iteration of a loop and moves directly to the next iteration. Unlike break, it does not terminate the loop but rather skips the rest of the code in the current iteration. Example •int main() { • for (int i = 1; i <= 5; i++) { • if (i == 3) { • continue; // skips the rest of the loop when i is 3 • } • cout << i << " "; • } • return 0; •}
  • 53. Copyright © 2012 Pearson Education, Inc. 1 0 END