Download presentation
Presentation is loading. Please wait.
Published byAllan Watkins Modified over 6 years ago
1
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Department of Computer Science & Engineering Air University Intro to Programming Week # 6 Repetition Structure Lecture # 10 By: Saqib Rasheed
2
. Nested loops
3
Nested Loops A loop can be nested inside of another loop.
C++ allows at least 256 levels of nesting When working with nested loops, the outer loop changes only after the inner loop is completely finished
4
The syntax for a nested for loop statement in C++
for ( init; condition; increment ) { statement(s); } statement(s); // you can put more statements.
5
The syntax for a nested while loop statement in C++
while(condition) { statement(s); } statement(s); // you can put more statements.
6
The syntax for a nested do...while loop statement in C++
do { statement(s); // you can put more statements. statement(s); } while( condition );
7
For loop nesting
8
Nested loops (loop in loop)
b ************* cin >> a >> b; for (int i = 0; i < a; i++) { for (int j=0; j<b; j++) cout << “*”; } cout << endl; a
9
Nested loops (2) b * ** *** a **** int a,b; cin >> a >> b;
for (int i = 0; i < a; i++) { for (int j=0; j<b; j++) if (j > i) break; cout << “*”; } cout << endl;
10
Nested loops (3) b * ** *** a **** int a,b; cin >> a >> b;
for (int i = 0; i < a; i++) { for (int j=0; j<b && j < i; j++) { cout << “*”; } cout << endl; if (j > i) break; j <= i;
11
Nested loops (4) b ************* ************ *********** a **********
int a,b; cin >> a >> b; for (int i = 0; i < a; i++) { for (int j=0; j<b; j++) if (j < i) cout << “ ”; else cout << “*”; } cout << endl;
12
Write a program in C++ that prints a tables Starting from 1 12.
Air University
13
#include <iomanip> // defines setw()
#include <iostream> // defines cout using namespace std; int main() { for (int x=1; x <= 12; x++) for (int y=1; y <= 12; y++) cout << setw(4) << x*y; cout << endl; } return 0; } . Air University
14
Assignment Due next Class Copied Assignments will be marked zero
Late Assignment not accepted Hard Copies only Write your name roll #, Section
15
Nested loops (5) * *** ***** ******* ********* ***********
16
Develop a code in C++ that generate the following series
Develop a code in C++ that generate the following series .Use nested while loop! . Series No No No. 3 1 2 2 3 3 3 1 2 3 4 5 6 1 1 2 1 2 3 Air University
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.