1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)

Slides:



Advertisements
Similar presentations
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Advertisements

1 CSE1301 Computer Programming: Lecture 15 Flowcharts and Debugging.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
FIT Objectives By the end of this lecture, students should: understand iteration statements understand the differences of for and while understand.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts, Testing and Debugging.
Chapter 5: Control Structures II (Repetition)
Chapter 5 (Loop Statements)
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Real World Applications: Statistical Measures Problem (page 95-98) Read a series of floating-point numbers from the standard input, and print a statistical.
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x0 B. y= 0 x0 C. y= 1 x
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 7: Repeating a Known Number of Times
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Iteration statement while do-while
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Control Structures - Repetition
Control Structures Lecture 7.
Looping.
- Additional C Statements
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Chapter 2.1 Repetition.
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
More Loops Topics Counter-Controlled (Definite) Repetition
ECE 103 Engineering Programming Chapter 18 Iteration
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
ICS103: Programming in C 5: Repetition and Loop Statements
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)

2 Topics while statement for statement break statement continue statement Nested loops

3 The while statement Implements the repetition in an algorithm Repeatedly executes a block of statements Tests a condition (Boolean expression) at the start of each iteration Terminates when condition becomes false (zero)

4 Example: addnum.c Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count

5 Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Initialize Check condition Update Iteration Control Example: addnum.c (cont)

6 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { return 0; } Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Example: addnum.c (cont)

7 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; return 0; } Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count only the variables sum and count are initialized to 0 Example: addnum.c (cont)

8 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); return 0; }. Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Example: addnum.c (cont)

9 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) { } return 0; } Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Example: addnum.c (cont)

10 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } return 0; } Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Example: addnum.c (cont)

11 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } return 0; } Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Same as: sum = sum + nextNum; Others: -=, *=, /=, etc. (D&D Fig. 3.11) Example: addnum.c (cont)

12 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } return 0; } Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Same as: count = count + 1; Decrement: count --; (D&D Fig. 3.12) Example: addnum.c (cont)

13 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Example: addnum.c (cont)

14 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf("%d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Read in numbers, add them, and print their sum and average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Example: addnum.c (cont)

15 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf ("% d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Example: addnum.c (cont) totalNum bers countnextNumsum ???? 0 0.0

16 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf ("% d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Example: addnum.c (cont) totalNum bers countnextNumsum ????

17 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf ("% d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Example: addnum.c (cont) totalNum bers countnextNumsum ????

18 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf ("% d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Example: addnum.c (cont) totalNum bers countnextNumsum ????

19 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count = 0, totalNumbers; scanf ("% d", &totalNumbers); while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Example: addnum.c (cont) totalNum bers countnextNumsum ????

20 Common Mistakes in while -- “ one liners” while (num < minimum) scanf(“%d”, &num); printf(“Number must be greater than %d.\n”, minimum); printf(“Please try again.\n”); while (num < minimum) { scanf(“%d”, &num); } printf(“Number must be greater than %d.\n”, minimum); printf(“Please try again.\n”);

21 while (num < minimum) { scanf(“%d”, &num); printf(“Number must be greater than %d.\n”, minimum); printf(“Please try again.\n”); } while (num < minimum) scanf(“%d”, &num); printf(“Number must be greater than %d.\n”, minimum); printf(“Please try again.\n”); Common Mistakes in while -- “ one liners” (cont)

22 while (num < minimum); { scanf(“%d”, &num); printf(“Number must be greater than %d.\n”, minimum); printf(“Please try again.\n”); } Common Mistakes in while -- extra semi-colon; Marks the end of the while-block -- usual cause of infinite loops

23 Read in numbers, add them, and print their sum and average set sum to 0 input nextNum check if end of input while (not end of input) { add nextNum to sum input nextNum check if end of input } etc...etc...etc... Checking for End-of-Input / End-of-File in while

24 Read in numbers, add them, and print their sum and average set sum to 0 input nextNum check if end of input while (not end of input) { add nextNum to sum input nextNum check if end of input } etc...etc...etc... float nextNum; float sum = 0.0; scanf(“%f”, &nextNum); while ( ?????? ) { sum += nextNum; scanf(“%f”, &nextNum); } etc...etc...etc... Checking for End-of-Input / End-of-File in while (cont)

25 Read in numbers, add them, and print their sum and average set sum to 0 input nextNum check if end of input while (not end of input) { add nextNum to sum input nextNum check if end of input } etc...etc...etc... float nextNum; float sum = 0.0; scanf(“%f”, &nextNum); ??????? while ( ?????? ) { sum += nextNum; scanf(“%f”, &nextNum); ??????? } etc...etc...etc... Checking for End-of-Input / End-of-File in while (cont)

26 Read in numbers, add them, and print their sum and average set sum to 0 input nextNum check if end of input while (not end of input) { add nextNum to sum input nextNum check if end of input } etc...etc...etc... float nextNum; float sum = 0.0; scanf(“%f”, &nextNum); ??????? while ( ?????? ) { sum += nextNum; scanf(“%f”, &nextNum); ??????? } etc...etc...etc... Checking for End-of-Input / End-of-File in while (cont) Recall: When the input ends, the scanf() function returns a special char value: EOF

27 Read in numbers, add them, and print their sum and average set sum to 0 input nextNum check if end of input while (not end of input) { add nextNum to sum input nextNum check if end of input } etc...etc...etc... float nextNum; float sum = 0.0; while ( scanf(“%f”, &nextNum) != EOF ) { sum += nextNum; } etc...etc...etc... Checking for End-of-Input / End-of-File in while (cont)

28 Read in numbers, add them, and print their sum and average set sum to 0 input nextNum check if end of input while (not end of input) { add nextNum to sum input nextNum check if end of input } etc...etc...etc... float nextNum; float sum = 0.0; while ( scanf(“%f”, &nextNum) != EOF ) { sum += nextNum; } etc...etc...etc... Checking for End-of-Input / End-of-File in while (cont)

29 do … while Implements a post-tested loop do { printf(“Main menu:\n”); printf(“1. Account balance.\n”); printf(“2. Withdraw cash.\n”); printf(“3. Transfer funds.\n\n”); printf(“9. Exit\n\n”); printf(“Please enter your choice: “); scanf(“%d”, &choice); /* Insert selection instructions here. */ } while ( choice != 9 );

30 do { printf(“Main menu:\n”); printf(“1. Account balance.\n”); printf(“2. Withdraw cash.\n”); printf(“3. Transfer funds.\n\n”); printf(“9. Exit\n\n”); printf(“Please enter your choice: “); scanf(“%d”, &choice); /* Insert selection instructions here. */ } while ( choice != 9 ); do … while (cont)

31 do { printf(“Main menu:\n”); printf(“1. Account balance.\n”); printf(“2. Withdraw cash.\n”); printf(“3. Transfer funds.\n\n”); printf(“9. Exit\n\n”); printf(“Please enter your choice: “); scanf(“%d”, &choice); /* Insert selection instructions here. */ } while ( choice != 9 ); do … while (cont)

32 The for statement Form of pre-tested loop which allows for initialization and iteration control Syntax: for ( initialization ; condition ; update ) { instruction block } Careful! A semi-colon here marks the end of the instruction block!

33 Example: addfor.c Read in numbers, add them, and print the sum and the average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count

34 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) { scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Read in numbers, add them, and print the sum and the average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Example: addfor.c (cont)

35 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) { scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Read in numbers, add them, and print the sum and the average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Initialize Example: addfor.c (cont)

36 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) { scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Read in numbers, add them, and print the sum and the average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Check condition Example: addfor.c (cont)

37 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) { scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Read in numbers, add them, and print the sum and the average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Example: addfor.c (cont) Update (aka Increment Step) IMPORTANT!! The Update is performed AFTER the body of the loop

38 #include /**********************************\ Read in numbers and add them up Print out the sum and the average \**********************************/ int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) { scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n",sum/count); return 0; } Read in numbers, add them, and print the sum and the average set sum to 0 set count to 0 input totalNumbers while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output "Sum was" sum output "Mean was" sum/count Example: addfor.c (cont)

39 #include int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) { scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; } while and for #include int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); count = 0; while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; }

40 #include int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) { scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; } while and for (cont) #include int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); count = 0; while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; } Check condition Initialize

41 #include int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) { scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; } #include int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); count = 0; while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; } Check condition while and for (cont)

42 #include int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); for ( count=0; count < totalNumbers; count++ ) { scanf("%f", &nextNum); sum += nextNum; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; } #include int main() { float nextNum, sum = 0.0; int count, totalNumbers; scanf("%d", &totalNumbers); count = 0; while (count < totalNumbers) { scanf("%f", &nextNum); sum += nextNum; count++; } printf("Sum was %f\n",sum); printf("Mean was %f\n", sum/count); return 0; } Update while and for (cont)

43 The break statement Implements the "exit loop" primitive Causes flow of control to leave a loop block ( while or for ) immediately

44 Example: recip.c Print out the reciprocals of numbers entered. Quit when 0 is entered loop { input nextNum if (nextNum is 0) { exit loop } else { output 1/nextNum }

45 #include /*******************************\ Print out the reciprocals of numbers entered. Quit when 0 is entered \*******************************/ int main() { float nextNum; return 0; } Print out the reciprocals of numbers entered. Quit when 0 is entered loop { input nextNum if (nextNum is 0) { exit loop } else { output 1/nextNum } Example: recip.c (cont)

46 #include /*******************************\ Print out the reciprocals of numbers entered. Quit when 0 is entered \*******************************/ int main() { float nextNum; while (1) { } return 0; } Print out the reciprocals of numbers entered. Quit when 0 is entered loop { input nextNum if (nextNum is 0) { exit loop } else { output 1/nextNum } “while (True)” infinite loop Example: recip.c (cont)

47 #include /*******************************\ Print out the reciprocals of numbers entered. Quit when 0 is entered \*******************************/ int main() { float nextNum; while (1) { scanf("%f", &nextNum); } return 0; } Print out the reciprocals of numbers entered. Quit when 0 is entered loop { input nextNum if (nextNum is 0) { exit loop } else { output 1/nextNum } Example: recip.c (cont)

48 #include /*******************************\ Print out the reciprocals of numbers entered. Quit when 0 is entered \*******************************/ int main() { float nextNum; while (1) { scanf("%f", &nextNum); if (nextNum==0.0) { break; } else { printf("%f\n", 1/nextNum); } return 0; } Print out the reciprocals of numbers entered. Quit when 0 is entered loop { input nextNum if (nextNum is 0) { exit loop } else { output 1/nextNum } Example: recip.c (cont)

49 #include /*******************************\ Print out the reciprocals of numbers entered. Quit when 0 is entered \*******************************/ int main() { float nextNum; while (1) { scanf("%f", &nextNum); if (nextNum==0.0) { break; } else { printf("%f\n", 1/nextNum); } return 0; } Print out the reciprocals of numbers entered. Quit when 0 is entered loop { input nextNum if (nextNum is 0) { exit loop } else { output 1/nextNum } Example: recip.c (cont)

50 #include /*******************************\ Print out the reciprocals of numbers entered. Quit when 0 is entered \*******************************/ int main() { float nextNum; while (1) { scanf("%f", &nextNum); if (nextNum==0.0) { break; } else { printf("%f\n", 1/nextNum); } return 0; } Print out the reciprocals of numbers entered. Quit when 0 is entered loop { input nextNum if (nextNum is 0) { exit loop } else { output 1/nextNum } Example: recip.c (cont)

51 The continue statement Causes flow of control to start immediately the next iteration of a loop block ( while or for )

52 Example: addpos.c Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop { input number if (number is negative) { begin next iteration } else if ( number is zero) { exit loop } add number to sum } output sum

53 include /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; printf("sum = %f\n", sum); return 0; } Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop { input number if (number is negative) { begin next iteration } else if ( number is zero) { exit loop } add number to sum } output sum Example: addpos.c (cont)

54 include /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; while (scanf("%f", &num) > 0) { sum += num; } printf("sum = %f\n", sum); return 0; } Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop { input number if (number is negative) { begin next iteration } else if ( number is zero) { exit loop } add number to sum } output sum Example: addpos.c (cont) scanf returns EOF if an end of file or error occurs; otherwise it returns the number of items converted and assigned

55 include /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; while (scanf("%f", &num) > 0) { if (num < 0) continue; else if (num == 0) break; sum += num; } printf("sum = %f\n", sum); return 0; } Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop { input number if (number is negative) { begin next iteration } else if ( number is zero) { exit loop } add number to sum } output sum Example: addpos.c (cont)

56 include /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; while (scanf("%f", &num) > 0) { if (num < 0) continue; else if (num == 0) break; sum += num; } printf("sum = %f\n", sum); return 0; } Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop { input number if (number is negative) { begin next iteration } else if ( number is zero) { exit loop } add number to sum } output sum Example: addpos.c (cont)

57 include /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; while (scanf("%f", &num) > 0) { if (num < 0) continue else if (num == 0) break; sum += num; } printf("sum = %f\n", sum); return 0; } Example: addpos.c (cont) These comparisons are ok despite num being of type float

58 include /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; while (scanf("%f", &num) > 0) { if (num < 0) continue; else if (num == 0) break; sum += num; } printf("sum = %f\n", sum); return 0; } Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop { input number if (number is negative) { begin next iteration } else if ( number is zero) { exit loop } add number to sum } output sum Example: addpos.c (cont)

59 for and continue In the case of a for statement, control passes to the “update” expression. for (count=0; scanf("%f", &num) > 0; count++) { if (num < 0) continue; else if (num == 0) break; sum += num; } Example: addpos2.c

60 scanf and while -- Example 1 float num; while (scanf(“%f”, &num) > 0) {...etc...etc...etc... } Input: 45.2 Result: 1

61 float num; while (scanf(“%f”, &num) > 0) {...etc...etc...etc... } Input: -5 Result: 1 scanf and while -- Example 1 (cont)

62 float num; while (scanf(“%f”, &num) > 0) {...etc...etc...etc... } Input: 0 Result: 1 scanf and while -- Example 1 (cont)

63 float num; while (scanf(“%f”, &num) > 0) {...etc...etc...etc... } Input: c Result: 0 scanf and while -- Example 1 (cont)

64 float num; while (scanf(“%f”, &num) > 0) {...etc...etc...etc... } Input: Dog Result: 0 scanf and while -- Example 1 (cont)

65 float num; while (scanf(“%f”, &num) > 0) {...etc...etc...etc... } scanf and while -- Example 1 (cont) Input: ^Z or ^D (depending on the operating system) Result: EOF (usually has value -1, but it can be any negative number)

66 int val; float x, y, z; val = scanf(“%f %f %f”, &x, &y, &z); printf(“%d\n”, val); Input: Output: 3 scanf -- Example 2

67 int val; float x, y, z; val = scanf(“%f %f %f”, &x, &y, &z); printf(“%d\n”, val); scanf -- Example 2 (cont) Input: c Output: 2

68 Input: 42.5 c 23 Output: 1 int val; float x, y, z; val = scanf(“%f %f %f”, &x, &y, &z); printf(“%d\n”, val); scanf -- Example 2 (cont)

69 Input: man 2 wolf Output: 0 int val; float x, y, z; val = scanf(“%f %f %f”, &x, &y, &z); printf(“%d\n”, val); scanf -- Example 2 (cont)

70 int num; char ch; float x; printf(“Please enter an int, a char, and a float: “); if ( scanf(“%d %c %f”, &num, &ch, &x) != 3 ) { printf(“Invalid input. No cookie for you.\n”); } else { printf(“Thank you. Your cookie is in the box.\n”); } scanf -- Example 3

71 int num; char ch; float x; printf(“Please enter an int, a char, and a float: “); if ( scanf(“%d %c %f”, &num, &ch, &x) != 3 ) { printf(“Invalid input. No cookie for you.\n”); } else { printf(“Thank you. Your cookie is in the box.\n”); } scanf -- Example 3 (cont)

72 Nested Loops Loops can be placed inside other loops The break and continue statements apply to the innermost enclosing while or for statement

73 Example: rect.c Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row }

74 Example: rect.c (cont) #include /* Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); return 0; } Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row }

75 #include /* Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); for (i=0; i < n; i++) { } return 0; } Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row } Example: rect.c (cont)

76 #include /* Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); for (i=0; i < n; i++) { for (j=0; j < m; j++) { } } return 0; } Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row } Example: rect.c (cont)

77 #include /* Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); for (i=0; i < n; i++) { for (j=0; j < m; j++) { printf("*"); } } return 0; } Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row } Example: rect.c (cont)

78 #include /* Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); for (i=0; i < n; i++) { for (j=0; j < m; j++) { printf("*"); } printf("\n"); } return 0; } Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row } Example: rect.c (cont)

79 #include /* Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); for (i=0; i < n; i++) { for (j=0; j < m; j++) { printf("*"); } printf("\n"); } return 0; } Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row } Example: rect.c (cont)

80 Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row } #include /* Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); for (i=0; i < n; i++) { for (j=0; j < m; j++) { printf("*"); } printf("\n"); } return 0; } program algorithm Example: rect.c (cont)

81 #include /* Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); i = 0; while (i < n) { for (j=0; j < m; j++) { printf("*"); } printf("\n"); i++; } return 0; } Variation: rect2.c Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row }

82 #include /* Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); for (i=0; i < n; i++) { j = 0; while (1) { printf("*"); j++; if (j == m) break; } printf("\n"); } return 0; } Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row } Variation: rect3.c

83 #include /* Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); for (i=0; i < n; i++) { j = 0; while (1) { printf("*"); j++; if (j == m) break; } printf("\n"); } return 0; } The innermost enclosing loop for this break is the while-loop Variation: rect3.c (cont)

84 Reading Deitel &Deitel –Chapter 3, Section 3.7 –Chapter 4, Sections 4.1 to 4.6 Sections 4.8 to 4.11