1
- Dr. Adriana Badulescu Kallas
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
Chapter 5: Control Structures II (Repetition) C++ Programming 1 - - PowerPoint PPT Presentation
Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Chapter 5: Control Structures II (Repetition) C++ Programming 1 Dr. Adriana Badulescu Kallas Introduction to Computer Science &
1
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
2
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
Learn about repetition (looping) control structures Explore how to construct and use count-controlled, sentinel-controlled, flag-controlled, and EOF-controlled repetition structures Examine break and continue statements Discover how to form and use nested control structures Learn how to avoid bugs by avoiding patches Learn how to debug loops
3
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
4
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
INPUT N1 INPUT N2 COMPUTE Sum=N1+N2; OUPUT Sum START STOP
5
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
INPUT N1 INPUT N2 COMPUTE Sum=N1+N2+N3; OUPUT Sum START STOP INPUT N3
6
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming N2
N1 N3
7
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming REPEAT 3 TIMES N2
N1 N3
8
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
REPEAT 1000 TIMES
REPEAT 3 TIMES
9
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
10
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
false true
Expression Statement while ( Expression ) Statement;
11
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
12
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
13
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
14
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
15
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
16
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
//initialize the counter counter = 0; //test the counter while (counter < N) { //loop statements Statements; //update the counter counter++; }
17
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
false true
counter < N Statement counter = 0 counter=counter +1
18
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
REPEAT 5 TIMES
REPEAT 5 TIMES
19
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
20
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
//initialize the loop control variable cin >> variable; //test the loop control variable while (variable != SENTINEL) { //loop statements Statements; //update the loop control variable cin >> variable; }
21
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
false true
variable!= SENTINEL variable!= SENTINEL Statement
INPUT variable INPUT variable
22
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
23
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
24
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
//initialize the loop control variable found = false; //test the loop control variable while (!found) { //loop statements Statements; //update the loop control variable if (expression) found = true; }
25
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
true false true
!found Statement found=false found=true expression
false
26
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
27
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
28
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
//initialize the loop control variable InputStream >> variable; //test the loop control variable while (InputStream) { //loop statements Statements; //update the loop control variable InputStream >> variable; }
29
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
false true
InputStream Statement INPUT variable INPUT variable
30
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
31
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
First Name Last Name TestScore Letter Grade
LetterGrade= A if 90<TestScore B if 80<TestScore<90 C if 70<TestScore<80 D if 60<TestScore<70 F if TestScore<60
∑ "
For each student For the class
32
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
START INPUT FirstName, LastName, AverageTestScore
YES NO
InputFile STOP NumberStudents=0 INPUT FirstName, LastName, AverageTestScore Calculate LetterGrade Sum = Sum + TestScore NumberStudents = NumberStudents+1 ClassAverage = Sum/NumberStudents
OUTPUT Name + LetterGrade
Sum=0 OUTPUT ClassAverage
33
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
34
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
noOfGuesses=0; while ((noOfGuesses < 5) && (!isGuessed)) { cout << "Enter an integer greater than or equal to 0 and less than 100: "; cin >> guess; cout << endl; noOfGuesses++;
}
35
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
36
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
+
37
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
38
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
Get the position, n, of the Fibonacci number in the sequence
By adding the previous two elements of the Fibonacci
39
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
40
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
41
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
42
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
+
43
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
44
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
45
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
for( InitialStatement; LoopCondition; UpdateStatement ) Statement;
46
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
false true LoopCondition
Statement InitialStatement UpdateStatement
for( InitialStatement; LoopCondition; UpdateStatement ) Statement;
47
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
48
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
49
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
50
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
51
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
52
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
for (i=0;i<5;i++); cout << “*" << endl;
for (;;) cout << “*" << endl;
53
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
54
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
Sample output:
Sample output:
55
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
56
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
57
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
false true
Expression Statement do Statement; while ( Expression );
58
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
59
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
60
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
61
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
62
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
63
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
64
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
for (j = 1; j <= N; j++) cout << "*";
for (i = 1; i <= 5 ; i++) { cout << i; cout << endl; }
65
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
* ** *** **** *****
for (i = 1; i <= 5 ; i++) { for (j = 1; j <= i; j++) cout << "*"; cout << endl; }
On line i – print i stars
66
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
***** **** *** ** *
for (i = 5; i >= 1; i--) { for (j = 1; j <= i; j++) cout << "*"; cout << endl; }
On line 5-i-1 – print i stars / decreasing order
67
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
* ** *** **** *****
for (i = 1; i <= 5 ; i++) { for (j = 1; j <= 5-i; j++) cout << “ "; for (j = 1; j <= i; j++) cout << "*"; cout << endl; }
On line i – print 5-i spaces and i stars
68
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
* ** *** **** ***** **** *** ** *
for (i = 1; i <= 5 ; i++) { for (j = 1; j <= i; j++) cout << "*"; cout << endl; } for (i = 4; i >= 1; i--) { for (j = 1; j <= i; j++) cout << "*"; cout << endl; }
69
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
70
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
71
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming
72
Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming