# Chapter05

Views:

## Presentation Description

No description available.

## Presentation Transcript

### Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman :

Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 5 (Loop Statements) © CPCS 202 1431/1432 – Term 1

### CHAPTER 5 - Loops :

CHAPTER 5 - Loops #  column shows the topics index.  column shows the programs index. ATM Simulation 3 Company Payroll Validating Numbers Illustrate Nested Loop Bald Eagle Sightings

### Compound Assignment Operators :

Compound Assignment Operators Introduction Giving programmers alternative ways to write equations Syntax When an assignment statement in the form: variable = variable op expression; It can be written in the following format variable op= expression; If we try to increase/decrease the value of the variable by only one, we can use increment (++) or decrement (--) operators Examples x = x * 5; as same as x *= 5; x = x + 1; as same as x += 1; as same as x++; 1

### Repetition and Loop Statements :

Repetition and Loop Statements Introduction Loop repeats statements until a condition is met Avoid infinity loop Each loop has a loop control variable, and 3 components: Initialization of the loop control variable Testing of the loop repetition condition Changing (updating) of the loop control variable Nested Loop is a concept of loops inside loops Subtopics while Statement do-while Statement for Statement 2

### 1. While Statement :

1. While Statement Introduction The statement (loop body) is executed while the condition (Loop repetition condition) is True Avoid infinity loop Syntaxwhile (loop repetition condition) one statement orwhile (loop repetition condition){ statements // loop body} Loop Repetition Condition C T F A Flow Diagram for (while) B loop body 2a

### ATM Simulation 3 :

ATM Simulation 3 P1 Write a program that simulates an ATM, where they are three main options: Deposit Money Withdraw Money Print Balance Assume the balance in the account is Zero Use if to choose an option from the Main Menu Validate the input; if a user choose a wrong option, display an error message REPEAT the menu until the user choose Zero -6-

### ATM Simulation 3 :

ATM Simulation 3 P1 #include <stdio.h> int main(void) { int command, money, balance; /* 1. Initial the balance */ /* 2. Ask the user to choose one of the 4 options */ while (command != 0) // stop the loop if command = 0 { switch (command) { case 1: /* 2.1 Deposit Money */ case 2: /* 2.2 Withdraw Money */ case 3: /* 2.3 Print Balance */ default: } } return(0); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23.

### ATM Simulation 3 :

ATM Simulation 3 P1

### ATM Simulation 3 :

ATM Simulation 3 P1

### Company Payroll :

Company Payroll P2 Write a program that asks a user to choose the number of employees in a company The program calculates the payroll for each employee according to the number of working hours and the rate Also, the program calculates the total amount of money for all the employees -10-

### Company Payroll :

Company Payroll P2 This loop will repeat based on a counter

### Company Payroll :

Company Payroll P2

### 2. Do-While Statement :

2. Do-While Statement Introduction The statement (loop body) is executed once before checking the condition Syntax do one statementwhile (loop repetition condition); ordo{ statements // loop body} while (loop repetition condition); condition B T F C Flow Diagram for (do-while) A loop body 2b 2b

### 2. Do-While Statement :

2. Do-While Statement Example 14 char letter; do { printf("Enter a letter from A through E to exit: "); scanf(“%c", &letter); } while (letter < ‘A’ || letter > ‘E’); printf(“END...\n"); 1. 2. 3. 4. 5. 6. 7. 8. 6 4-5 T F 8 1 2b 2b

### Validating Numbers :

Validating Numbers P3 We need a program that asks a user to choose a value from the range 10 to 20 Validate the input and display an error message if a user chooses a number out of the range, a character, or an string -15-

### Validating Numbers :

Go to the function In this case, 3 ways to validate the input Validating Numbers P3 Check the main() first

### Validating Numbers :

Validating Numbers P3

### 3. For Statement :

3. For Statement Introduction Put the three main components in one line in the same order Syntax for (initialization expression; loop repetition condition; update expression){ statements // loop body} Flow Diagram for (for) condition C T F A B loop body initial update 2c 2c

### 3. For Statement :

3. For Statement Example 1 Example 2 19 /* display N asterisks */ for (count_star = 0; count_star < N; count_star += 1) printf(“*"); 5. 6. 7. 2c int factorial(int n) { int i, product; product = 1; /* computes the product n x (n-1) x (n-2) x ... x 2 x 1 */ for (i = n; i > l; i--) { product = product * i; } return (product); } 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.

### Illustrate Nested Loop :

Illustrate Nested Loop P4 Write a simple program that illustrates how is nested loop working. Choose the outer loop from 1 to 3 Choose the inner loop from 0 to the outer number -20-

### Illustrate Nested Loop :

Illustrate Nested Loop P4 /* * Illustrates a pair of nested counting loops */ #include <stdio.h> int main(void) { int i, j; printf (" I J\n"); printf ("-------- --- ---\n"); for (i = 1; i<4; ++i) /* heading of outer loop */ { printf("Outer %6d\n", i); for (j=0; j<i; ++j) /* heading of inner loop */ { printf(" Inner%9d\n", j); } // end of inner loop } // end of outer loop return(0); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23.

### Bald Eagle Sightings :

Bald Eagle Sightings P5 Write a program that calculates the number of bald eagle appears in each month for one year In each month, the user will enter the number of the bald eagles in groups, such as 5 6 2 3 13 2 When a sentinel is appeared in a month, this indicates that no bald eagle will appear in that month -22- bald eagle

### Bald Eagle Sightings :

Bald Eagle Sightings P5 -23- Month 1 Month 2 Month 3 Month 12 loop based on a COUNTER loop based on a USER INPUT 5 8 2 0 5 3 1 4 0 1 0

### Bald Eagle Sightings :

Bald Eagle Sightings P5

### Bald Eagle Sightings :

Bald Eagle Sightings P5 P5 Click on any character to move to the next scanf, such as Space or Enter

### Questions :

Questions Which of the following is an infinite loop? for (int i=20; i>=10; i--) for (int i=1; i<=10; i++) for (int i=10; i<=20; i--) for (int i=20; i>=50; i++) The statement _______, when executed in a while loop, skips the remaining statements in the body of the structure and begins the next iteration of the loop. continue break next do/more -26- Q

### Homework :

Homework Write a program to process 5 collections of daily high temperatures using loop. Your program should count and print: the number of hot days (85 or higher), the number of pleasant days (60-84), and the number of cold days (less than 60). It should also display the category of each temperature. At the end of the run, display the average temperature. -27- HANDWRITING IN THE HOMEWORK IS NOT ACCEPTABLE hw