cs 141 computer programming 1
DESCRIPTION
Control Statements. CS 141 Computer Programming 1. Teacher Assistant AlAnoud Bahomaid. Outline. Control structure in c++ Repetition Structure Counter control loop While repetition structure For repetition structure Optional expressions in the for statement header Common logical error - PowerPoint PPT PresentationTRANSCRIPT
1
CS 141Computer
Programming 1
Teacher AssistantAlAnoud Bahomaid
Control Statements
2Outline Control structure in c++ Repetition Structure Counter control loop While repetition structure For repetition structure Optional expressions in the for statement header Common logical error The counter variable Scope For repetition example Convert for and while Do… while repetition structure Nesting loop Nested control structure Continue Break
C++ Control
Structure
Sequence structure
Selection Structure
If, if… else, switch
Repetition structure
While, do…while, for
Control structures in C++
4
repetition structure
It’s a control structure that repeats a group of steps in a program
Action repeated while some condition remains true
Definition
while do.. whilefor
5
repetition structure ask yourself some of the following questions
to determine whether loops will be required in your code:
Were there any steps I repeated as I solved the problem? If so, which ones?
If the answer to question 1 is yes, did I know in advance how many times to repeat the steps?
If the answer to question 2 is no, how did I know how long to keep repeating the steps?
When use repetition structure
6
counter-controlled loop
Essentials of counter-controlled repetition requires:
The name of a control variable (loop counter) The initial value of the control variable. The loop-continuation condition that test for the
final value of the control variable. The increment (or decrement) by which the control
variable is modified each time through the loop
a loop whose required number of iterations can be determined before loop execution begins
Definition
7
Sentinel-controlled loop
•A loop whose number of iterations is UNKNOWN. (indefinite repetition).
•Use a special value called sentinel value (signal value, dummy value or a flag value).
• Indicates the end of the loop.
Definition
True
Activity Diagram
1-While Repetition Structure (1-10)
Condition
False
Action (a)
9
1-While Repetition Structure (2-10)
If the controlling expression( condition) is true, the loop body is then executed before the controlling expression is evaluated once more.
If the controlling expression (condition) is false, i.e. expression evaluates to false, the program goes on to execute the statement following the while loop.
If the body of the counter-controlled repetition contains more than one statement, you should surround its body by braces { }.
10
1-While Repetition Structure (2-10)
While( loop repetition condition) statement;
OR
While( loop repetition condition){
statements; . . }
Syntax
No semicolon after while statement
Write a program that print numbers from 1 to 10 using (while) loop
1-While Repetition Structure (4-10)
Question
12 1-While Repetition Structure (5-10)
13 1-While Repetition Structure (6-10)
14
1-While Repetition Structure (7-10)
In previous Example:Control variable name : counterInitial value : 1The loop continuation condition:
counter<=10The loop counter is incremented : +
+counter ;
Write a program to find the average of the students grade for ten subjects in the class.
1-While Repetition Structure (8-10)
Question
16
1-While Repetition Structure (9-10)
17 1-While Repetition Structure (10-10)
18
2 -for Repetition Structure (1-8)
for ( initialization ; LoopContinuationTest ; increment ) statement
OR
for ( initialization ; LoopContinuationTest ; increment ) { statementS
.
. }
Syntax
No semicolon after for statement
19
(2 -for Repetition Structure (2-8
As we said in while, If you need to repeat more than one statement in a program loop, you must place the statements in a block marked by braces { }.
Write a program that print numbers from 1 to 10 using (for) loop
2- for Repetition Structure (3-8)
Question
212-for Repetition Structure (4-8)
22
2-for Repetition Structure (5-8)
23
2-for Repetition Structure (6-8)
24
Initialization and increment: For multiple variables, use comma-separated
lists
for (int i = 0, j = 0; j + i <= 10; j++,i++ ) cout << j + i << endl;
2-for Repetition Structure (7-8)
25
2-for Repetition Structure (8-8) Vary control variable from 1 to 5 in increments of 1
for ( int i = 1; i <= 5; i++ ) Vary control variable from 5 to 1 in decrements of 1
for ( int i = 5; i >= 1; i-- ) Vary control variable from 7 to 77 in steps of 7
for ( int i = 7; i <= 77; i += 7 ) Vary control variable from 20 to 2 in steps of -2
for ( int i = 20; i >= 2; i -= 2 ) Vary control variable over the sequence: 2, 5, 8, 11, 14, 17, 20 for ( int i = 2; i <= 20; i += 3 )
Vary control variable over the sequence: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0 for ( int i = 99; i >= 0; i -= 11 )
26
Optional expressions in the for statement header
All the three expressions in the for statement header are optional . The two semicolon are required.
Omitting the loopContinuationCondition: C++ assumes that the condition is true.
for ( int i=0 ; ; i++)
Omitting the initialization expression: The counter variable must be initialized earlier in the program.
Int i=0; for( ; i< 10 ; i++)
Omitting increment expression: The increment is calculated by statement in the body.
for ( int i=0 ; i<10 ; ) i;++
27
Optional expressions in the for statement headerExample
s:
1. Loop2. Loop3. Loop4. loop..10. loop
0123456789
Infinite loop
- initialize control variable before the loop
- increment control variable in the loop body
28
Common logical Error Be sure to verify that a loop’s repetition
condition will become false (0); otherwise, an infinite loop may result.
Examplefor (int counter=1 ; counter <= 25 ; counter--)
cout << counter << endl;
This condition will never be false
int counter = 1;while (counter <= 25){ cout << counter << endl; counter -- ;}
This condition will never be false
29The counter variable Scope If the initialization expression declares
the control variable , the control variable can be used only in the body of the for statements.
This is what we called variable scope.
Write a program that calculate the sum of even integers from 2 through 100 .
then determine the scope of the control variable .
for Repetition Example (1-5)
Question
31
for Repetition Example (2-5)
32
for Repetition Example (3-5)
33
for Repetition Example (4-5)
sum sum += number
number
2 0=+2 26 2=+4 4
12 6=+6 620 12=+8 830 20=+10 10..…..…..…
..…
..…
..…
..…
..…
..…
2550 2450=+100 100
34
for Repetition Example (5-5)
The scope of the control variable (number) is:
Line 14 and Line 15
35
Convert for and while for loops can usually be rewritten as while loops
for(initialization; loopContinuationCondition; increment) {
Statement }
Is Equivlent to:
initialization;while (loopContinuationCondition){
statement increment;
}
36
3-do… while structure Repetition (1-9) The do…while repetition statement is similar to
the while statement.
In the while: The loop continuation condition test occurs at the
beginning of the loop before the body of the loop executes.
In the do … while: The loop continuation condition test occurs after
the loop body executes. The loop body always executes at least once. Recommended to use braces in the do.. While to
avoid confusing with while statements.
True
Action (a)
Activity Diagram
3-do… while structure Repetition (2-9)
Condition
False
38
3-do… while structure Repetition (3-9)
do{ statement
}while ( condition );
Syntax
Don’t forget semicolon after condition
Write a program that print numbers from 1 to 10 using (do.. while) loop
3-do… while structure Repetition (4-9)
Question
40
3-do… while Repetition Structure (5-9)
41
3-do… while Repetition Structure (6-9)
42
3-do… while structure Repetition (7-9)
Another solution
Notice the pre increment in loop-continuation condition.
Write a program that request the user to type positive numbers (one each time),
and calculate the average when the user enter a number less than one.
3-do… while structure Repetition (8-9)Questio
n
Enter a number (less than 1 = quit): 3Enter a number (less than 1 = quit): 1Enter a number (less than 1 = quit): 4Enter a number (less than 1 = quit): 0The average is: 2Press any key to continue . . .
Enter a number (less than 1 = quit): 0No averagePress any key to continue . . .
3-do… while structure Repetition (9-9)Solutio
n
45
Nesting Loops (1-7)
Loops can be nested, that is, the loop body can also contain a loop.
46
Nesting Loops (2-7)
Write a program that print this using Loop
Question
47
Nesting Loops (3-7)
48
Nesting Loops (4-7)
49
Nesting Loops (5-7)
Write a program that print this using Loop
Question
50
Nesting Loops (6-7)
51
Nesting Loops (7-7)
52
Nested Control Structures (1-5)
Problem statement A college has a list of test results (1 = pass, 2 = fail) for
10 students. Write a program that analyzes the results. If more than 8 students pass, print "Raise Tuition."
Notice that Program processes 10 results
Fixed number, use counter-controlled loop Two counters can be used
One counts number that passed Another counts number that fail
Each test result is 1 or 2 If not 1, assume 2
Question
53
Nested Control Structures (2-5)
54
Nested Control Structures (3-5)
55
Nested Control Structures (4-5)
Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 2Enter result (1 = pass, 2 = fail): 2Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 2Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 2Passed 6Failed 4
output
56Nested Control Structures (5-5)
Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 2Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Enter result (1 = pass, 2 = fail): 1Passed 9Failed 1Raise tuition
output
57
Break (1-6)
The break statement exits from a switch or loop immediately.
You can use the break keyword to stop a loop for any reason.
58
Break (2-6)
59 Break (3-6)
Example (1):
60
Break (4-6)
Output (1):
61
Break (5-6)
Example(2) :
62
Break (6-6)
Output (2):
63
Continue (1-6)
The continue statement can be used in loops and has the opposite effect to break, that is, the next loop is begun immediately.
In while, do…while: The loop continuation condition evaluates
immediately after the continue statement executes.
In for statement: The increment expression executes, then the
loop-continuation test evaluates.
64Continue (2-6)
int counter = 0;while ( counter <= 10 ) { ++counter; continue; cout << counter << endl; }
int counter= 0; do { ++counter; continue; cout << counter << endl; } while(counter <= 10);
for (int counter=1 ; counter <=10 ; counter++){
continue; cout << counter << endl;}
This Line never Excute
This Line never Excute
This Line never Excute
65
Continue (3-6)
Example (1):
66
Continue (4-6)
Output (1)
67
Continue (5-6)
Example (2):
68
Continue (6-6)
Output (2)