Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control.

Similar presentations


Presentation on theme: "C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control."— Presentation transcript:

1 C++ Programming: CS102 LOOP

2 Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control his fate? — William Shakespeare The used key is always bright. — Benjamin Franklin Intelligence… is the faculty of making artificial objects, especially tools to make tools. — Henri Bergson Every advantage in the past is judged in the light of the final issue. — Demosthenes

3 while Looping (Repetition) Structure The general form of the while statement is: while is a reserved word Statement can be simple or compound Expression acts as a decision maker and is usually a logical expression Statement is called the body of the loop The parentheses are part of the syntax

4 while Looping (Repetition) Structure (continued) Infinite loop: continues to execute endlessly −Avoided by including statements in loop body that assure exit condition is eventually false

5 while Looping (Repetition) Structure (continued)

6 Designing while Loops

7 Find output n = 5; j = 1; while ( j < 4 ) { if ( n >= 10 ) n = n – 2; else n = n * j; j++; } cout<<“ The n is ”<< n;

8 8 Example Write a program to display all the numbers divisible by 5 in the range 0 to 5000.

9 Example Assume you put 1000 pounds in a projects that returns a profit of about 5% per year. How long will it take for your money to double ? Assume you put 5000 pounds in a projects that returns a profit of about 10% per year. How much money will you have in 5 years ?

10 Example write a program that reads in the grades of 50 students in a course (out of 100 points each ) and then count the number of A students ( grade > 85 ) and the number of B students (grade > 75 ).

11 do … while Looping (Repetition) Structure General form of a do... while : The statement executes first, and then the expression is evaluated To avoid an infinite loop, body must contain a statement that makes the expression false The statement can be simple or compound Loop always iterates at least once

12 do … while Looping (Repetition) Structure (continued)

13

14 Find output prod = 1; do { prod = prod * m ; m = m + 1 ; } while ( m < 6 ) ; cout<<“ prod is \n “<< prod ;

15

16 Divisibility Test by 3 and 9

17 Example Write a program that does a survey on a certain question. The question has four possible answers. Run the survey on 20 people and then display the number of people who chose each answer. Example: What is your favorite subject? A. Mathematics B. Economics C. Programming D. Physics

18 Choosing the Right Looping Structure All three loops have their place in C++ −If you know or can determine in advance the number of repetitions needed, the for loop is the correct choice −If you do not know and cannot determine in advance the number of repetitions needed, and it could be zero, use a while loop −If you do not know and cannot determine in advance the number of repetitions needed, and it is at least one, use a do...while loop

19 break and continue Statements break and continue alter the flow of control break statement is used for two purposes: −To exit early from a loop Can eliminate the use of certain (flag) variables −To skip the remainder of the switch structure After the break statement executes, the program continues with the first statement after the structure

20 break & continue Statements (continued) continue is used in while, for, and do … while structures When executed in a loop −It skips remaining statements and proceeds with the next iteration of the loop

21 21 Write a program to ask the user for a positive integer, and then display its factorial. Given that factorial(n) is 1 X 2 X 3 …… x n Example

22 x to the y power // raise x to the y power #include using namespace std; int main() { int x, y, i, power; i = 1; power = 1; cout << "Enter base as an integer: "; cin >> x; cout << "Enter exponent as an integer: "; cin >> y; while ( i <= y ) { power *= x; ++i; } cout << power << endl; return 0; }

23 Programming Example The data show the account number followed by the account balance The operation has two entries: −Transaction code −Transaction amount Transaction codes − W : withdrawal & D = means deposit − I = interest paid by the bank Program updates balance after each transaction During the month, if at any time the balance goes below $1000.00, a $25.00 service fee is charged

24 Programming Example: Checking Account Balance (continued) Program prints the following information: −Account number −Balance at the beginning of the month −Balance at the end of the month −Interest paid by the bank −Total amount of deposit −Number of deposits −Total amount of withdrawal −Number of withdrawals −Service charge if any

25 Programming Example: Input and Output Input: file consisting of data in the previous format Output is of the following form: Account Number: 467343 Beginning Balance: $23750.40 Ending Balance: $24611.49 Interest Paid: $366.24 Amount Deposited: $2230.50 Number of Deposits: 3 Amount Withdrawn: $1735.65 Number of Withdrawals: 6


Download ppt "C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control."

Similar presentations


Ads by Google