While Loop Lesson CS1313 Spring 2014 1 while Loop Outline 1.while Loop Outline 2.while Loop Example #1 3.while Loop Example #2 4.while Loop Example #3.

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
AP STUDY SESSION 2.
1
Feichter_DPG-SYKL03_Bild-01. Feichter_DPG-SYKL03_Bild-02.
© 2008 Pearson Addison Wesley. All rights reserved Chapter Seven Costs.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 3 CPUs.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
UNITED NATIONS Shipment Details Report – January 2006.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Properties of Real Numbers CommutativeAssociativeDistributive Identity + × Inverse + ×
Exit a Customer Chapter 8. Exit a Customer 8-2 Objectives Perform exit summary process consisting of the following steps: Review service records Close.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt BlendsDigraphsShort.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt RhymesMapsMathInsects.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Year 6 mental test 10 second questions
1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Break Time Remaining 10:00.
Turing Machines.
PP Test Review Sections 6-1 to 6-6
EU market situation for eggs and poultry Management Committee 20 October 2011.
Bright Futures Guidelines Priorities and Screening Tables
Bellwork Do the following problem on a ½ sheet of paper and turn in.
Green Eggs and Ham.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
VOORBLAD.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
© 2012 National Heart Foundation of Australia. Slide 2.
Adding Up In Chunks.
LO: Count up to 100 objects by grouping them and counting in 5s 10s and 2s. Mrs Criddle: Westfield Middle School.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
Note to the teacher: Was 28. A. to B. you C. said D. on Note to the teacher: Make this slide correct answer be C and sound to be “said”. to said you on.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
25 seconds left…...
Subtraction: Adding UP
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Analyzing Genes and Genomes
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Essential Cell Biology
Converting a Fraction to %
Clock will move after 1 minute
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
Immunobiology: The Immune System in Health & Disease Sixth Edition
Physics for Scientists & Engineers, 3rd Edition
Energy Generation in Mitochondria and Chlorplasts
Select a time to count down from the clock above
Murach’s OS/390 and z/OS JCLChapter 16, Slide 1 © 2002, Mike Murach & Associates, Inc.
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
Presentation transcript:

while Loop Lesson CS1313 Spring while Loop Outline 1.while Loop Outline 2.while Loop Example #1 3.while Loop Example #2 4.while Loop Example #3 5.Repetition and Looping 6.while Loop 7.while Loop Behavior 8.while Loop vs. if Block 9.while Loop Flowchart 10.while Loop Example #1 11.while Loop Example #2 12.while Loop Example #3 13.while Loop Example Flowchart 14.Execute Body How Many Times? 15.An Infinite Loop #1 16.An Infinite Loop #2 17.Aside: How to Kill a Program in Unix 18.Kinds of Statements Inside while Loop 19.Statements Inside while Loop 20.No Declarations Inside while Loop 21.Compound Statement a.k.a. Block #1 22.Compound Statement a.k.a. Block #2 23.Another while Loop Example #1 24.Another while Loop Example #2 25.Another while Loop Example #3 26.Another while Loop Example #4 27.Another while Loop Example #5 28.Yet Another while Loop Example #1 29.Yet Another while Loop Example #2 30.Yet Another while Loop Example #3 31.Yet Another while Loop Example #4 32.States & Traces #1 33.States & Traces #2 34.States & Traces #3 35.Tracing the Loop #1 36.Tracing the Loop #2 37.Tracing the Loop #3 38.Tracing the Loop #4 39.Tracing the Loop #5

while Loop Lesson CS1313 Spring while Loop Example #1 #include int main () { /* main */ const float minimum_power = 0; const int program_success_code = 0; const int program_failure_code = -1; float power_in_mechanical_horsepower;

while Loop Lesson CS1313 Spring while Loop Example #2 printf("What is the power "); printf("in mechanical horsepower?\n"); scanf("%f", &power_in_mechanical_horsepower); while (power_in_mechanical_horsepower < minimum_power) { printf("ERROR: you can't have a"); printf(" negative power!\n"); printf("So really, what is the power "); printf("in mechanical horsepower?\n"); scanf("%f", &power_in_mechanical_horsepower); } /* while (power_in_mechanical_horsepower <...) */ printf("The power is valid.\n"); return program_success_code; } /* main */

while Loop Lesson CS1313 Spring while Loop Example #3 % gcc -o conversions_idiot_while conversions_idiot_while.c % conversions_idiot_while What is the power in mechanical horsepower? -5 ERROR: you can't have a negative power! So really, what is the power in mechanical horsepower? -4 ERROR: you can't have a negative power! So really, what is the power in mechanical horsepower? 0 The power is valid.

while Loop Lesson CS1313 Spring Repetition and Looping Repetition means performing the same set of statements over and over. The most common way to perform repetition is via looping. A loop is a sequence of statements to be executed, in order, over and over, as long as some condition continues to be true.

while Loop Lesson CS1313 Spring while Loop C has a loop construct known as a while loop: while ( condition ) { statement1 ; statement2 ;... } The condition of a while loop is a Boolean expression completely enclosed in parentheses – just like in an if block. The sequence of statements between the while statement’s block open and block close is known as the loop body.

while Loop Lesson CS1313 Spring while Loop Behavior while ( condition ) { statement1 ; statement2 ;... } A while loop has to the following behavior: 1.The condition is evaluated, resulting in a value of either true ( 1 ) or false ( 0 ). 2.If the condition evaluates to false ( 0 ), then the statements inside the loop body are skipped, and control is passed to the statement that is immediately after the while loop’s block close. 3.If the condition evaluates to true ( 1 ), then: a.the statements inside the loop body are executed in sequence. b.When the while loop’s block close is encountered, the program jumps back up to the associated while statement and starts over with Step 1.

while Loop Lesson CS1313 Spring while Loop vs. if Block A while loop is SIMILAR to an if block, EXCEPT: UNLIKE an if block, the keyword is while. UNLIKE an if block, when a while loop gets to its block close, it jumps back up to the associated while statement; UNLIKE an if block, a while loop has EXACTLY ONE clause, which is analogous to the if clause. A while loop CANNOT have anything analogous to an else if clause nor to an else clause.

while Loop Lesson CS1313 Spring while Loop Flowchart statement_before ; while ( condition ) { statement_inside1 ; statement_inside2 ;... } statement_after ;

while Loop Lesson CS1313 Spring while Loop Example #1 #include int main () { /* main */ const float minimum_power = 0; const int program_success_code = 0; const int program_failure_code = -1; float power_in_mechanical_horsepower;

while Loop Lesson CS1313 Spring while Loop Example #2 printf("What is the power "); printf("in mechanical horsepower?\n"); scanf("%f", &power_in_mechanical_horsepower); while (power_in_mechanical_horsepower < minimum_power) { printf("ERROR: you can't have a"); printf(" negative power!\n"); printf("So really, what is the power "); printf("in mechanical horsepower?\n"); scanf("%f", &power_in_mechanical_horsepower); } /* while (power_in_mechanical_horsepower <...) */ printf("The power is valid.\n"); return program_success_code; } /* main */

while Loop Lesson CS1313 Spring while Loop Example #3 % gcc -o conversions_idiot_while conversions_idiot_while.c % conversions_idiot_while What is the power in mechanical horsepower? -5 ERROR: you can't have a negative power! So really, what is the power in mechanical horsepower? -4 ERROR: you can't have a negative power! So really, what is the power in mechanical horsepower? 0 The power is valid.

while Loop Lesson CS1313 Spring while Loop Example Flowchart printf("What is the power "); printf("in mechanical horsepower?\n"); scanf("%f", &power_in_mechanical_horsepower); while (power_in_mechanical_horsepower < minimum_power) { printf("ERROR: you can't have a"); printf(" negative power!\n"); printf("So really, what is the power "); printf("in mechanical horsepower?\n"); scanf("%f", &power_in_mechanical_horsepower); } /* while (power_in_mechanical_horsepower <...) */ printf("The power is valid.\n"); power < 0 Prompt for power. Input power. Input power again.

while Loop Lesson CS1313 Spring Execute Body How Many Times? while ( condition ) { statement1 ; statement2 ;... } If the condition evaluates to false ( 0 ), then the loop body will not be executed at all (that is, zero times). If the condition evaluates to true ( 1 ), then the loop body may be executed at least one more time.

while Loop Lesson CS1313 Spring An Infinite Loop #1 An infinite loop is a loop whose condition never evaluates to false. #include int main () { /* main */ const int computers_number = 5; const int program_success_code = 0; int users_number; printf("Enter an integer:\n"); scanf("%d", &users_number); printf("I had %d.\n", computers_number); while (users_number < computers_number) { printf("Your number is less than mine!\n"); } /* while (users_number < computers_number) */ return program_success_code; } /* main */

while Loop Lesson CS1313 Spring An Infinite Loop #2 % gcc -o infiniteloop infiniteloop.c % infiniteloop Enter an integer: 6 I had 5. % infiniteloop Enter an integer: 5 I had 5. % infiniteloop Enter an integer: 4 I had 5. Your number is less than mine!...

while Loop Lesson CS1313 Spring Aside: How to Kill a Program in Unix On most Unix systems, including ssh.ou.edu, you can quit out of a program that is currently executing by typing: Ctrl C -

while Loop Lesson CS1313 Spring Kinds of Statements Inside while Loop Between the while statement’s block open and its associated block close, there can be any kind of executable statements, and any number of them. For example: printf statements; scanf statements; assignment statements; if blocks; while loops. There are several other kinds of executable statements that can occur inside a while loop, some of which we’ll learn later in the semester.

while Loop Lesson CS1313 Spring Statements Inside while Loop In the event that the while condition evaluates to true ( 1 ), then the statements inside the while loop body will be executed one by one, in the order in which they appear in the while loop.

while Loop Lesson CS1313 Spring No Declarations Inside while Loop Notice that a while loop SHOULDN’T contain declaration statements, because the while statement is an executable statement, and ALL declarations MUST come before ANY executable statements.

while Loop Lesson CS1313 Spring Compound Statement a.k.a. Block #1 A compound statement is a sequence of statements, with a well-defined beginning and a well-defined end, to be executed, in order, under certain circumstances. A while loop is a compound statement, just like an if block. We’ll see others later. Although a while loop is actually a sequence of statements, we can treat it as a single “super” statement in some contexts. Compound statements are also known as blocks.

while Loop Lesson CS1313 Spring Compound Statement a.k.a. Block #2 In C, a compound statement, also known as a block, is delimited by curly braces. That is, a compound statement/block begins with a block open { and ends with a block close }

while Loop Lesson CS1313 Spring Another while Loop Example #1 #include int main () { /* main */ const int minimum_number = 1; const int maximum_number = 100; const int my_number = 32; const int close_distance = 5; const int very_close_distance = 1; const int negative_distance = -1; const int no_distance = 0; const int program_success_code = 0; int your_number, your_distance; int your_last_distance = negative_distance; char correct_number_hasnt_been_input = 1;

while Loop Lesson CS1313 Spring Another while Loop Example #2 printf("I’m thinking of a number between %d and %d.\n", minimum_number, maximum_number); while (correct_number_hasnt_been_input) { printf("What number am I thinking of?\n"); scanf("%d", &your_number); if ((your_number < minimum_number) || (your_number > maximum_number)) { printf("Hey! That’s not between %d and %d!\n", minimum_number, maximum_number); printf("I’ll pretend you didn’t say that.\n"); } /* if ((your_number < minimum_number) ||...) */ else if (your_number == my_number) { printf("That’s amazing!\n"); correct_number_hasnt_been_input = 0; } /* if (your_number == my_number) */

while Loop Lesson CS1313 Spring Another while Loop Example #3 else { your_distance = abs(your_number - my_number); if (your_distance == very_close_distance) { printf("You’re incredibly hot!\n"); } /* if (your_distance == very_close_distance) */ else if (your_last_distance < no_distance) { printf("Not bad for your first try.\n"); } /* if (your_last_distance < no_distance) */ else if (your_distance < your_last_distance) { printf("You’re getting warmer....\n"); } /* if (your_distance < your_last_distance) */ else if (your_distance > your_last_distance) { printf("Ouch! You’re getting colder.\n"); } /* if (your_distance > your_last_distance) */ else { printf("Uh oh. You made no progress.\n"); } /* if (your_distance >...)...else */ your_last_distance = your_distance; } /* if (your_number == my_number)...else */ } /* while (correct_number_hasnt_been_input) */ printf("Good for you!\n"); return program_success_code; } /* main */

while Loop Lesson CS1313 Spring Another while Loop Example #4 % gcc -o warmercolder warmercolder.c % warmercolder I’m thinking of a number between 1 and 100. What number am I thinking of? 0 Hey! That’s not between 1 and 100! I’ll pretend you didn’t say that. What number am I thinking of? 101 Hey! That’s not between 1 and 100! I’ll pretend you didn’t say that. What number am I thinking of? 50 Not bad for your first try. What number am I thinking of? 40 You’re getting warmer.... What number am I thinking of? 60 Ouch! You’re getting colder.

while Loop Lesson CS1313 Spring Another while Loop Example #5 What number am I thinking of? 30 You’re getting warmer.... What number am I thinking of? 35 Ouch! You’re getting colder. What number am I thinking of? 33 You’re incredibly hot! What number am I thinking of? 31 You’re incredibly hot! What number am I thinking of? 32 That’s amazing! Good for you!

while Loop Lesson CS1313 Spring Yet Another while Loop Example #1 #include int main () { /* main */ const int initial_sum = 0; const int increment = 1; const int program_success_code = 0; const int program_failure_code = -1; int initial_value, final_value; int count; int sum;

while Loop Lesson CS1313 Spring Yet Another while Loop Example #2 printf("What value would you like to "); printf("start counting at?\n"); scanf("%d", &initial_value); printf("What value would you like to "); printf("stop counting at,\n"); printf(" which must be greater than "); printf("or equal to %d?\n", initial_value); scanf("%d", &final_value); if (final_value < initial_value) { printf("ERROR: the final value %d is less\n", final_value); printf(" than the initial value %d.\n", initial_value); exit(program_failure_code); } /* if (final_value < initial_value) */

while Loop Lesson CS1313 Spring Yet Another while Loop Example #3 sum = initial_sum; count = initial_value; while (count <= final_value) { sum = sum + count; count = count + increment; } /* while (count <= final_value) */ printf("The sum of the integers from"); printf(" %d through %d is %d.\n", initial_value, final_value, sum); return program_success_code; } /* main */

while Loop Lesson CS1313 Spring Yet Another while Loop Example #4 % gcc -o whilecount whilecount.c % whilecount What value would you like to start counting at? 1 What value would you like to stop counting at, which must be greater than or equal to 1? 0 ERROR: the final value 0 is less than the initial value 1. % whilecount What value would you like to start counting at? 1 What value would you like to stop counting at, which must be greater than or equal to 1? 5 The sum of the integers from 1 through 5 is 15.

while Loop Lesson CS1313 Spring States & Traces #1 The state of a program is the set of values of all of its variables at a given moment during execution; that is, it’s a snapshot of the memory that’s being used. The state also includes information about where you are in the program when that snapshot is taken. A trace of a program is a listing of the state of the program after each statement is executed. Tracing helps us to examine the behavior of a piece of code, and so it sometimes can be useful in debugging.

while Loop Lesson CS1313 Spring States & Traces #2 Suppose that, in the previous example program, the user input 1 for initial_value and 5 for final_value. Let’s examine the program fragment around the loop. sum = initial_sum; count = initial_value; while (count <= final_value) { sum = sum + count; count = count + increment; } /* while (count <= final_value) */

while Loop Lesson CS1313 Spring States & Traces #3 sum = initial_sum; count = initial_value; while (count <= final_value) { sum = sum + count; count = count + increment; } /* while (count <= final_value) */ If we number these statements, we get: 1 sum = initial_sum; 2 count = initial_value; 3 while (count <= final_value) { 4 sum = sum + count; 5 count = count + increment; 6 } /* while (count <= final_value) */

while Loop Lesson CS1313 Spring Tracing the Loop #1 1 sum = initial_sum; 2 count = initial_value; 3 while (count <= final_value) { 4 sum = sum + count; 5 count = count + increment; 6 } /* while (count <= final_value) */ Snapshot ofTraceComments Itera- tion # After stmt # Value of sum Value of count N/A1 0 garbageHaven’t entered loop yet N/A2 01 Haven’t entered loop yet Condition evaluates to true ( 1 ) new sum = old sum + count = = new count = old count + 1 = = Jump back up to stmt #3 to start iteration #2

while Loop Lesson CS1313 Spring Tracing the Loop #2 1 sum = initial_sum; 2 count = initial_value; 3 while (count <= final_value) { 4 sum = sum + count; 5 count = count + increment; 6 } /* while (count <= final_value) */ Snapshot ofTraceComments Itera- tion # After stmt # Value of sum Value of count Condition evaluates to true ( 1 ) new sum = old sum + count = = new count = old count + 1 = = Jump back up to stmt #3 to start iteration #3

while Loop Lesson CS1313 Spring Tracing the Loop #3 1 sum = initial_sum; 2 count = initial_value; 3 while (count <= final_value) { 4 sum = sum + count; 5 count = count + increment; 6 } /* while (count <= final_value) */ Snapshot ofTraceComments Itera- tion # After stmt # Value of sum Value of count Condition evaluates to true ( 1 ) new sum = old sum + count = = new count = old count + 1 = = Jump back up to stmt #3 to start iteration #4

while Loop Lesson CS1313 Spring Tracing the Loop #4 1 sum = initial_sum; 2 count = initial_value; 3 while (count <= final_value) { 4 sum = sum + count; 5 count = count + increment; 6 } /* while (count <= final_value) */ Snapshot ofTraceComments Itera- tion # After stmt # Value of sum Value of count Condition evaluates to true ( 1 ) new sum = old sum + count = = new count = old count + 1 = = Jump back up to stmt #3 to start iteration #5

while Loop Lesson CS1313 Spring Tracing the Loop #5 1 sum = initial_sum; 2 count = initial_value; 3 while (count <= final_value) { 4 sum = sum + count; 5 count = count + increment; 6 } /* while (count <= final_value) */ Snapshot ofTraceComments Itera- tion # After stmt # Value of sum Value of count Condition evaluates to true ( 1 ) new sum = old sum + count = = new count = old count + 1 = = Jump back up to stmt #3 to start iteration # Condition evaluates to false ( 0 ), loop exited