Chapter 05

  • Uploaded by: mohammad
  • 0
  • 0
  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Chapter 05 as PDF for free.

More details

  • Words: 1,069
  • Pages: 23
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

CP 202 Chapter 5

CHAPTER 5

Compound Assignment Operators 

When an assignment statement in the form: variable = variable op expression;

For example, x = x + 1;

an alternative way of writing the statement is: variable op= expression;

For example, x += 1; 

Increment (++) / Decrement (--) x = x++;



x += 1;



x = x + 1;

While Statement Introduction   



Loop repetition condition. The statement (loop body) is executed while the condition is True. Avoid infinity loop.

condition F

Syntax while (loop repetition condition) statement or while (loop repetition condition) { statements // loop body }

A

T loop body



B C

Flow Diagram for (while)

While Statement Example 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

int command, money, balance = 0; printf("Enter command number:\n“); printf(" 0 - quit\n"); printf(" 1 - deposit money\n"); printf(" 2 - withdraw money\n"); printf(" 3 - print balance\n"); scanf("%d", &command); while (command != 0) // stop the loop if command = 0 { switch (command) { case 1: // deposit code printf("Enter deposit amount: "); scanf("%d", &money); balance = balance + money; break; case 2: // withdraw code printf("Enter withdraw amount: "); scanf("%d", &money); balance = balance - money; break; case 3: // print code printf("Current balance = %d\n", balance); break; default: printf("Error\n"); } printf(“Enter command number: “); scanf("%d", &command); } printf("See you later!\n");

While Statement Example 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

int command, money, balance = 0; printf("Enter command number:\n“); printf(" 0 - quit\n"); printf(" 1 - deposit money\n"); printf(" 2 - withdraw money\n"); printf(" 3 - print balance\n"); scanf("%d", &command);

0. loop control variable 1. initialization

while (command != 0) // stop the loop if command = 0 { switch (command) { case 1: // deposit code printf("Enter deposit amount: "); scanf("%d", &money); balance = balance + money; break; case 2: // withdraw code printf("Enter withdraw amount: "); scanf("%d", &money); balance = balance - money; break; case 3: // print code printf("Current balance = %d\n", balance); break; default: printf("Error\n"); } printf(“Enter command number: “); scanf("%d", &command); } printf("See you later!\n");

2. loop repetition condition

3. Updating

Draw the flow diagram for this program?

While Statement Example 2 

Check the book:    

Compute company payroll (example 5.1 in the book) See the program at Figure 5.4 See the output at Figure 5.4 Trace the memory. See Table 5.2

While Statement Example 2

While Statement Example 2

0. loop control variable 1. initialization 2. loop repetition condition

3. Updating

Draw the flow diagram for this program?

While Statement Example 2 (OUTPUT) statement count_emp < number_emp scanf(“%lf”,&hours); scanf(“%lf”,&rate);

hours

rate

pay

total_pay

count_emp

?

?

?

0.0

0 True get hours get rate

50.0 5.25

Pay = hours * rate;

262.5

total_pay = total_pay + pay;

find pay 262.5

count_emp = count_emp +1; count_emp < number_emp scanf(“%lf”,&hours); scanf(“%lf”,&rate); Pay = hours * rate;

count_emp = count_emp +1;

increment count_emp True get hours get rate find pay

6.0 5.0 30.0 292.5

count_emp = count_emp +1;

total_pay = total_pay + pay;

add to total_pay 1

total_pay = total_pay + pay;

count_emp < number_emp scanf(“%lf”,&hours); scanf(“%lf”,&rate); Pay = hours * rate;

Effect

add to total_pay 2

increment count_emp True get hours get rate find pay

15.0 7.0 105.0 397.5

add to total_pay 3

increment count_emp

Do-While Statement Introduction  



Loop repetition condition. The statement (loop body) is executed once before checking the condition.

loop body



A B

Syntax do

condition

statement while (loop repetition condition);

F

or

C

do { statements // loop body } while (loop repetition condition);

Flow Diagram for (do-while)

T

Do-While Statement Example 1 

char letter;



do {



printf("Enter a letter from A through E to exit the loop: ");



scanf(“%c", &letter);



} while (letter < ‘A’ || letter > ‘E’);

Draw the flow diagram for this program?

Do-While Statement Example 2 

Check the book:  

Validating input using do-while statement (example 5.10) See the program at Figure 5.14

#include<stdio.h> int get_int(int n_min,int n_max) { int in_val, /* input - number entered by user status; /* status value returned by scanf char skip_ch; /* character to skip int error; /* error flag for bad input */

Do-While Statement Example 2

*/ */ */

/* Get data from user until in_val is in the range. */ do { error=0; // No errors detected yet. /* Get a number from the user. */ printf("Enter an integer in the range from %d ",n_min); printf("to %d inclusive> ",n_max); status=scanf("%d",&in_val); printf("status = %d ",status); /* Validate the number */ if(status!=1) /* in_val didn't get a number { error=1; scanf("%c",&skip_ch); printf("\nskip_ch = %c\n",skip_ch); printf("invalid character >>%c>>. ",skip_ch); printf("Skipping rest of line. \n"); } else if(in_valn_max) { error=1; printf("Number %d is not in range.\n",in_val); } /* Skip rest of data line. do scanf("%c",&skip_ch); while(skip_ch!='\n');

*/

*/

} while(error); return(in_val); } int main(void) { int i=get_int(10,20); printf("i=%d\n",i); return(0); }

Draw the flow diagram for this program?

For Statement Introduction 



Three components:  Initialization of the loop control variable.  Test of the loop repetition condition.  Change (update) of the loop control variable.

Syntax for (initialization expression; loop repetition condition; update expression) { statements // loop body }

initial update condition

T

F

A

loop body



B C

Flow Diagram for (for)

For Statement Example 1   

/* display N asterisks */ for (count_star = 0; count_star < N; count_star += 1) printf(“*");

Draw the flow diagram for this program?

For Statement Example 2

Draw the flow diagram for this program?

Nested Loop 

Loops inside Loops

Nested Loop Example 1

Nested Loop Example 1

Draw the flow diagram for this program?

Nested Loop Example 2

Draw the flow diagram for this program?

Case Study: Collecting Area for Solar-Heated House 

Review Section 5.9 from the book.

Learn in the Lab 

How to Debug and Test Programs? 

Review Section 5.10 from the book.

Related Documents

Chapter 05
June 2020 8
Chapter 05
November 2019 51
Chapter 05
November 2019 19
Chapter 05
November 2019 19
Chapter 05
November 2019 15
Chapter 05
November 2019 17

More Documents from ""

Chapter3-appendix2
November 2019 39
Arak
June 2020 24
Cpcs202_lab 1
November 2019 46
Chapter5-appendix1
November 2019 39
June 2020 22