Unit 4 - Object Oriented Programming / C++
Unit 4 - Object Oriented Programming / C++
QF002/4/1
UNIT4
OBJECTIVES
General Objective
Specific Objective
Recognize the repetition structure. Differentiate the repetition structure. Write and design a simple program using the repetition structure.
________________________________ ________________________________________
QF002/4/2
INPUT
4.0
Repetition Structure
The repetition structure allow a sequence of instructions to be executed repeatedly until a certain condition is reached. This structure comes in three forms:
a. While b. Do- while c. For
A while structure allows the program to repeat a set of statements as long as the starting condition remains true. True is used in the Boolean sense and so the condition for the loop to continue must evaluate to true or false. The program evaluates the condition if it is true then the statements inside the braces are executed. The condition is evaluated again. This will contin ue until the condition is false and the program jumps to the next line after the body of the structure. If only one statement is used in the while loop then the braces are not required.
________________________________ ________________________________________
QF002/4/3
Figure 4.1: A While Statement Structure The expression is first evaluated, If it is TRUE the statement block is executed If it is FALSE, the statement block is skipped
exp
Yes
Statement
No
Figure 4.2: Flowchart Of While Statement Structure A while structure allows the program to repeat as long as the condition remains true
Ahha.. Error?
________________________________ ________________________________________
QF002/4/4
________________________________ ________________________________________
QF002/4/5
Analysis: This program is a game. Enter two numbers, one small and one large. The smaller number will count up by ones and the larger number will count down by twos. The goal of the game is to guess when they will meet. In lines 12-15, the numbers are entered. Line 20 sets up a while loop which will continue only as long as three conditions are met small is not bigger than large; large isn't negative and small doesn't overrun the size of a small integer (MAXSMALL). In line 23, the value in small is calculated modulo 5,000. This does not change the value in small; however, it only returns the value 0 when small is an exact multiple of 5,000. Each time a dot (.) is printed to the screen to show progress. In line 26, small is incremented and on line 28, large is decremented by 2. When any of the three conditions in the while loop fails, the loop ends and execution of the program continues after the
while loop's closing brace in line 29.
An Output of the program Output: Enter a small number: 2 Enter a large number: 100000 small: 2......... Small: 33335 Large: 33334
________________________________ ________________________________________
QF002/4/6
4.2
A variation on the simple while loop is the do while structure. The do part contains the body and is executed as long as the while condition remains true. The difference between these two while structures are that with a simple while loop the condition is evaluated first and if it is immediately false, the body is never executed. In the do while loop, the body is executed before the condition is tested and so guaranteed to be executed at least once. Figure 4.5 below shows the general form of do-while structure
Do Statement block While (expression);
Statement
Yes
No
exp
________________________________ ________________________________________
QF002/4/7
A simple program
// Program to illustrate a do while loop #include <ios.stream.h> void main() { int selection; do { cout << \n Menu\n; cout << \n 0. Exit\n;
cout << \n 1. Append\n; cout << \n 2. Delete\n; cout << \n 3. Modify\n; cout << \n\n Enter Selection: ; cin >> selection; } while(selection >0 && selection < 4); // The do loop is repeated if the expression is true }
The main difference is that in the while statement, the conditional expression is evaluated at the top of the loop, while in the do-while statement, the conditional expression is evaluated at the bottom of the loop. Therefore, the statements controlled by the do-while statement are executed at least once.
________________________________ ________________________________________
QF002/4/8
In the While loop the condition is evaluated first and if it is immediately false, the body is never executed
In the Do- while loop, the body is executed before the condition is tested and executed at lest one
________________________________ ________________________________________
QF002/4/9
Activity 4a
Test your comprehension before continuing the next input. Check your answers on the next page. 4.1 4.2 4.3 What is repetition structure? Name the components of the repetition structur e. What is the main difference between the while and do-while statements? 4.4 4.5 Can the while statement end with a semicolon? Develop a simple program to print cubes by using while statement
________________________________ ________________________________________
QF002/4/10
Feedback 4a
Make sure you have tried to ans wer all the questions given.you can check your answers with the answers below. 4.1. The repetition structure permits a sequence of the instructions to be executed repeatedly until a certain condition is reached.
4.2.
4.3.
The main difference is that in the while statement, the conditional expression is evaluated at the top of the loop,
while in the do-while statement, the conditional expression is
evaluated at the bottom of the loop. Therefore the state ments controlled by the do-while statements are executed at least once.
4.4.
By definition, the while statement does not end with a semicolon. However, it is legal in C to put a semicolon right after the while statement
________________________________ ________________________________________
QF002/4/11
4.5.
#include<iostream.h> main() { int n; cout<< Enter positif integers. Terminate with 0.\n\t: cin>>n; while (n>0) { cout << n<< cubed is <<n*n*n<< \n\t: ; cin >> n; } } ;
________________________________ ________________________________________
QF002/4/12
4.3
For statement
The for structure has the general form as the while structure.
for(initialization ;expression; incrementation) statement block;
Figure 4.9 A For Statement Structure The initialization part is used to initialize any variable(s) that may need to be initialized. This part is performed just once at the start of the loop. The expression part determines whether the loop execution should continue. If the value of the expression is TRUE (non zero), the statements block will be executed otherwise the loop will be terminated. The incremented part typically increment s or decrements the loop index. This performed every time through the loop iteration. This part is used to increment any variable(s) that may need to be incremented. For loop expression is not terminated with semicolon Remember that!!!!!
Yes. Sir!!!
________________________________ ________________________________________
QF002/4/13
A simple program
#include <iostream.h> void main() { int i, sum, sum2; for (i=2,sum=0,sum2=0;i<=20; i = i+2) { sum = sum + i; sum2= sum2 + i * i; } cout << \n Sum of first 20 even natural numbers = << sum; cout << \n Sum of their squares } = << sum2;
________________________________ ________________________________________
QF002/4/14
4.4
Two commands that can be included in looping processes are continue and break. The continue statement, when included in the body of the loop causes the program to return immediately to the start of the loop and ignore any remaining part of the body. The break statement causes the program to immediately exit from the loop and resume at the next line of the program
4.5
The special statement "break" causes the loop to be abandoned and execution continues following the closing curly brace.
while ( i > 0 ) { ....; if ( j == .... ) { break; // abandon the loop } ....; } // end of the loop body cout << "continues here ...\n";
The figure above shows the program continues after the end of the loop. Within a nested loop, "break" causes the innermost loop to be abandoned.
________________________________ ________________________________________
QF002/4/15
4.6
The continue statement is to terminate loop but also goes back to the beginning of the loops block to begin the next iteration. An example of a simple program
#include <iostream.h> main() { int n; for (;;){ cout << Enter int: ; cin >> n; if (n%2 = = 0) continue; else if (n%3 = = 0) break; cout << \t Bottom of loop.\n; } cout << \t Outside of loop.\n; }
Emmm..
Sh. When the continue statement inside a loop is executed, the program control is branched back to the beginning of the loop so that another iteration can be started.
________________________________ ________________________________________
QF002/4/16
________________________________ ________________________________________
QF002/4/17
Activity 4b
Test your comprehension before continuing the next input Check your answers on the next page . 4.6. 4.7. 4.8. What is break statement used for? What is continue statement used for? Write a simple program using for statement to calculate the square root of even number. What can the continue statement do inside a loop?
4.9.
________________________________ ________________________________________
QF002/4/18
Feedback 4b
Make sure you have tried to answer all the questions given. You can check your answers with the answers below 4.6. The break statement is used to terminate the switch or a loop. construct
4.7. The continue statement is used to let you stay within a loop while skipping over some statements.
4.8. main() { float x; cout << Enter a positive number: ; cin >> x; for (int ; cout <<The integer square root of << x >> is << n 1 << endl; n = 1; n * n <= x, n++)
________________________________ ________________________________________
QF002/4/19
4.9. When the continue statement inside a loop is executed, the program control is branched back to the beginning of the loop so that another iteration can be started. Inside the loop, any statements following the continue statement will be skipped over each other if the continue statement is executed.
________________________________ ________________________________________
QF002/4/20
Key Facts
The while structure allows the program to repeat a set of statements as long as the starting condition remains true. In the do-while statemant, the conditional expression is evaluated at the bottom of the loop. The Do- while loop, the body is executed before the condition is tested and executed at lest one. The break statement causes the loop to be abandoned and execution continues following the closing curly brace. The continue statement is to terminate loop but also goes back to the beginning of the loops block to begin the next iteration.
________________________________ ________________________________________
QF002/4/21
Self-Assessment
You are approaching success, please answer the questions below. If you have any problems, please discuss it with your lecturer. Wish you good luck and all the best.
Question 4-1
e. What is wrong with this code? int counter = 0 while (counter < 10) { cout << "counter: " << counter; counter++; }
________________________________ ________________________________________
QF002/4/22
f. What is wrong with this code? int counter = 100; while (counter < 10){ cout << "counter now: " << counter; counter--;}
Question 4-2.
a. How do I initialize more than one variable in a for loop? b. Why is goto should be avoided?
d. What is the value of x when the for loop completes? for (int x = 0; x < 100; x++)
f. Write a for statement to count from 100 to 200 by 2s. for (int x = 100; x<=200; x+=2)
________________________________ ________________________________________
QF002/4/23
g. What wrong with this code? for (int counter = 0; counter < 10; counter++); cout << counter << "\n";
h. What wrong with this code? cout << "Enter a number between 0 and 5: "; cin >> theNumber; switch (theNumber) { case 0: doZero(); case 1: case 2: case 3: case 4: case 5: doOneToFive(); break; default: doDefault(); break; } // fall through // fall through // fall through // fall through
________________________________ ________________________________________
QF002/4/24
Feedback On Self-assessment
Make sure you have tried to answer all the questions given. You can check your answers with the answers below.
Answer 4-1 a. Yes. Any loop can be nested within any other loops.
b. Yes. Following are examples for both a for loop and a while
loop: for(;;) { // This for loop never ends! } while(1) { // This while loop never ends!
________________________________ ________________________________________
QF002/4/25
d.
e.
int counter = 0 while (counter < 10) { cout << "counter: " << counter; counter++; } The counter is never incremented and the while loop will never terminate.
f.
int counter = 100; while (counter < 10){ cout << "counter now: " << counter; counter--;} The counter is initialized to 100, but the test condition is that if it is less than 10, the test will fail and the body will never be executed. If line 1 were changed to int counter = 5;, the loop would not terminate until it had counted down past the smallest possible int. Because int is signed by default, this would not be what has been intended.
________________________________ ________________________________________
QF002/4/26
Answer 4-2.
makes for source code that is difficult to understand and therefore difficult to maintain.
c. Yes, if the condition is FALSE after the initialization, the body of
the for loop will never execute. Here's an example: for (int x = 100; x < 100; x++) d. 100
e.
for (int i = 0; i< 10; i++) { for ( int j = 0; j< 10; j++) cout << "0"; cout << "\n"; }
f.
________________________________ ________________________________________
QF002/4/27
g.
for (int counter = 0; counter < 10; counter++); cout << counter << "\n"; There is a semicolon after the loop, and the loop d oes nothing. The programmer may have intended this, but if counter was supposed to print each value, it won't.
________________________________ ________________________________________