While Loop Lesson CS1313 Spring 2009 1 while Loop Outline 1.while Loop Outline 2.while Loop Example #1 3.while Loop Example #2 4.Repetition and Looping.

Slides:



Advertisements
Similar presentations
Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
Advertisements

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.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Arithmetic Expressions Lesson #1 CS1313 Spring Arithmetic Expressions Lesson #1 Outline 1.Arithmetic Expressions Lesson #1 Outline 2.A Less Simple.
CS0004: Introduction to Programming Repetition – Do Loops.
Computer Science 1620 Loops.
Topic 6 – Repetition and Loops. CISC 105 – Topic 6 Program Repetition Repetition refers to the repeats of certain program statements within a program.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Idiotproofing Lesson CS1313 Spring Idiotproofing Outline 1.Idiotproofing Outline 2.Idiotproofing 3.Idiotproofing Quotes 4.An Idiotproof Website.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
If Lesson 3 CS1313 Spring if Lesson 3 Outline 1. if Lesson 3 Outline 2.Multiple, Related Conditions #1 3.Multiple, Related Conditions #2 4.Multiple,
User Defined Functions Lesson 2 CS1313 Spring User Defined Functions 2 Outline 1.User Defined Functions 2 Outline 2.Argument Order When Passing.
For Loop Lesson 3 CS1313 Spring for Loop Lesson 3 Outline 1. for Loop Lesson 3 Outline 2. for Loop with a float Counter: BAD! 3. float Counter Example.
For Loop Lesson 2 CS1313 Spring for Loop Lesson 2 Outline 1. for Loop Lesson 2 Outline 2. for Loop Application 3. Factorial 4. Factorial Program.
Array Lesson 1 CS1313 Spring Array Lesson 1 Outline 1.Array Lesson 1 Outline 2.Mean of a List of Numbers 3.mean: Declarations 4.mean: Greeting,
Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.
Program Looping Making Decisions Copyright © 2012 by Yong-Gu Lee
Array Lesson 2 CS1313 Spring Array Lesson 2 Outline 1.Array Lesson 2 Outline 2.Reading Array Values Using for Loop #1 3.Reading Array Values Using.
For Loop Lesson 1 CS1313 Spring for Loop Lesson 1 Outline 1. for Loop Lesson 1 Outline 2.A while Loop That Counts #1 3.A while Loop That Counts.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
Boolean Data Lesson CS1313 Fall Boolean Data Outline 1.Boolean Data Outline 2.Data Types 3.C Boolean Data Type: char or int 4.C Built-In Boolean.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
User Defined Functions Lesson 1 CS1313 Spring User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Control Structures 1 The if, for, and while Statements §5.1, §5.4, & §5.5 (Several examples here; also Lab #4)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
If Lesson 1 CS1313 Fall if Lesson 1 Outline 1.if Lesson 1 Outline 2.Absolute Value 3.Absolute Value Definition 4.Absolute Value Implementation 5.What.
Idiotproofing Lesson CS1313 Spring Idiotproofing Outline 1.Idiotproofing Outline 2.Idiotproofing 3.Idiotproofing Quotes 4.Idiotproofing Example.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
Flow Control. Comments u Comments: /* This is a comment */ –Use them! –Comments should explain: v special cases v the use of functions (parameters, return.
Chapter 4 – C Program Control
CSE 220 – C Programming Loops.
REPETITION CONTROL STRUCTURE
for Loop 1 Outline for Loop 1 Outline for Loop
Chapter 4 - Program Control
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Idiotproofing Outline
while Loop Outline while Loop Outline while Loop Example #1
User Defined Functions 2 Outline
CS1100 Computational Engineering
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Chapter 4 - Program Control
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Nested if Lesson Outline
if Lesson 2 Outline The Meaning of else How Many Clauses Will Execute?
for Loop Lesson 3 Outline
3 Control Statements:.
EPSII 59:006 Spring 2004.
More Loops Topics Counter-Controlled (Definite) Repetition
Repetition Statements (Loops) - 2
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Arithmetic Expressions Lesson #1 Outline
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:

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

while Loop Lesson CS1313 Spring while Loop Example #1 #include int main () { /* main */ const int no_clowns = 0; const int program_success_code = 0; int number_of_clowns; printf("How many clowns did you have?\n"); scanf("%d", &number_of_clowns); while (number_of_clowns < no_clowns) { printf("ERROR: you can’t have "); printf("negative clowns!\n"); printf("So really, how many clowns "); printf("did you have?\n"); scanf("%d", &number_of_clowns); } /* while (number_of_clowns < no_clowns) */ printf("The number of clowns is valid.\n"); return program_success_code; } /* main */

while Loop Lesson CS1313 Spring while Loop Example #2 % gcc -o clown_idiot_while clown_idiot_while.c % clown_idiot_while How many clowns did you have? ERROR: you can’t have negative clowns! So really, how many clowns did you have? 5 The number of clowns 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 that: 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 int no_clowns = 0; const int program_success_code = 0; int number_of_clowns; printf("How many clowns did you have?\n"); scanf("%d", &number_of_clowns); while (number_of_clowns < no_clowns) { printf("ERROR: you can’t have "); printf("negative clowns!\n"); printf("So really, how many clowns "); printf("did you have?\n"); scanf("%d", &number_of_clowns); } /* while (number_of_clowns < no_clowns) */ printf("The number of clowns is valid.\n"); return program_success_code; } /* main */

while Loop Lesson CS1313 Spring while Loop Example #2 % gcc -o clown_idiot_while clown_idiot_while.c % clown_idiot_while How many clowns did you have? ERROR: you can’t have negative clowns! So really, how many clowns did you have? 5 The number of clowns is valid.

while Loop Lesson CS1313 Spring while Loop Example Flowchart printf("How many clowns did you have?\n"); scanf("%d", &number_of_clowns); while (number_of_clowns < no_clowns) { printf("ERROR: you can’t have "); printf("negative clowns!\n"); printf("So really, how many clowns "); printf("did you have?\n"); scanf("%d", &number_of_clowns); } /* while (number_of_clowns < no_clowns) */ printf("The number of clowns is valid.\n"); clowns < 0 Prompt for # clowns. Input # clowns. Input # clowns 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 an arbitrarily large number of times.

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.coe.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