CS161 Introduction to Computer Science

Slides:



Advertisements
Similar presentations
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Advertisements

Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
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.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
Objectives You should be able to describe:
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Today in CS161 Lecture #8 Learn about… Logicals: And, Or, Not Repetition: loops! Rewrite our First Program Using loops Graphics Let’s make the graphics.
Overview Go over parts of quiz? Another iteration structure for loop.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Computer Programming -1-
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Basic concepts of C++ Presented by Prof. Satyajit De
Topic 4: Looping Statements
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
The switch Statement, and Introduction to Looping
Lecture 7: Repeating a Known Number of Times
Repetition Structures (Loops)
Control Structures - Repetition
Chapter 8 Repetition Statements
Arrays, For loop While loop Do while loop
Loops October 10, 2017.
Iteration with While You can say that again.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Today in CS161 Week #4 Using if statements Learn about Repetition
Additional Control Structures
Unary Operators ++ and --
Loops CIS 40 – Introduction to Programming in Python
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
Today in CS161 Week #4 Learn about… Rewrite our First Program
Module 4 Loops.
Repetition Control Structure
Chapter 6: Repetition Statements
CS150 Introduction to Computer Science 1
Computing Fundamentals
Control Structures Part 1
Let’s all Repeat Together
A LESSON IN LOOPING What is a loop?
Objectives You should be able to describe: The while Statement
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Presentation transcript:

CS161 Introduction to Computer Science Topic #7

Today in CS161 Repetition in Programs while, do while, for loops questions with program #3 CS161 Topic #7

Repetition in Programs What if we wanted to give the user another chance to enter their input... This would be impossible without loops Algorithms that require loops look something like: Step 1: Receive Input Step 2: Echo the Input Step 3: Ask the user if this is correct Step 4: If not, repeat beginning at step #1 CS161 Topic #7

Three types of Loops Each of these can perform the same operations... There are three ways to repeat a set of code using loops: while loop do while loop for loop Each of these can perform the same operations... it is all in how you think about it! ....let’s see.... CS161 Topic #7

Using a While Loop Let’s give the user a 2nd (and 3rd, 4th, 5th...) chance to enter their data using a while loop. While loops have the form: (notice semicolons!) while (logical expression) single statement; { many statements; } CS161 Topic #7

THIS ORDER IS IMPORTANT TO REMEMBER! Using a While Loop The while statement means that while an expression is true, the body of the while loop will be executed. Once it is no longer true, the body will be bypassed. The first thing that happens is that the expression is checked, before the while loop is executed. THIS ORDER IS IMPORTANT TO REMEMBER! CS161 Topic #7

Using a While Loop The Syntax of the While Loop is: while (loop repetition condition) <body> Where, the <body> is either one statement followed by a semicolon or a compound statement surrounded by {}. Remember the body is only executed when the condition is true. Then, after the body is executed, the condition is tested again... CS161 Topic #7

Using a While Loop Notice, you must remember to initialize the loop control variable before you enter the while loop. Then, you must have some way of updating that variable inside of the body of the loop so that it can change the condition from true to false at some desired time. If this last step is missing, the loop will execute "forever" ... this is called an infinite loop. CS161 Topic #7

Using a While Loop We will need a control variable to be used to determine when the loop is done... char response = ‘n’; while (‘n’ == response) { cout << “Please enter ... “; cin >> data; cout << “We received: “ << data << “\nIs this correct? (y/n)”; cin >> response; } CS161 Topic #7

Using a While Loop One way to fix this: What is a drawback of the previous loop? The user may have entered a lower or upper case response! One way to fix this: Change the logical expression to list all of the legal responses while (‘n’ == response || ‘N’ == response) { ... } CS161 Topic #7

Using a While Loop Yet another way to fix this: To loop, assuming that they want to continually try again until they enter a Y or a y! Notice the use of AND versus OR! while (‘y’ != response && ‘Y’ != response) { ... } CS161 Topic #7

Using a While Loop Another way to fix this: Use the tolower function in the ctype.h library: #include <ctype.h> while (tolower(response) != ‘y’) { ... } CS161 Topic #7

Using a While Loop Another way to fix this: Use the toupper function in the ctype.h library: #include <ctype.h> while (toupper(response) != ‘Y’) { ... } CS161 Topic #7

Using a do while Loop This same loop could have been rewritten using a do while loop instead do while loops have the form: (notice semicolons!) do single statement; while (logical expression); { many statements; } while (logical expression); CS161 Topic #7

Using a do while Loop Things to notice about a do while statement: (1) The body of a do while statement can be one statement or a compound statement surrounded by {} (2) Each statement in the do while loop is separated by a semicolon (3) Notice the body is always executed once! Even if the logical expression is false the first time! CS161 Topic #7

Using a do while Loop Don't use a do while unless you are sure that the body of the loop should be executed at least once! char response; do { cout << “Please enter ... “; cin >> data; cout << “We received: “ << data << “\nIs this correct? (y/n)”; cin >> response; } while (‘y’ != response && ‘Y’ != response); CS161 Topic #7

Using a for loop The for loop is commonly used to loop a certain number of times. For example, you can use it to print out the integers 1 thru 9: int i; for (i=1; i <= 9; ++i) cout << i << endl; CS161 Topic #7

Using a for loop i is called the loop control variable. It is most common to use variables i, j, and k for control variables. But, mnemonic names are better! for (initialize; conditional exp; increment) <body> Note: The body of the for loop is either one statement followed by a semicolon or a compound statement surrounded by {}. CS161 Topic #7

Using a for loop The for statement will first (1) INITIALIZE VARIABLE i to 1; (2) Check the logical expression to see if it is True or False; (3) if it is True the body of the loop is executed and it INCREMENTs VARIABLE i by 1; or, if it is False the loop is terminated and the statement following the body of the loop is executed. CS161 Topic #7

Using a for Loop In C++ for (i=0; i < 10; ++i) is the same as: j+=i ;//remember this is j = j+i; is the same as: i = 0; while (i < 10) { j += i; ++i; } CS161 Topic #7

Using a for Loop We can also use a for loop to do the same loop that we have been talking about today: for (char response = ‘n’; ‘y’ != response && ‘Y’ != response; cin >> response) { cout << “Please enter ... “; cin >> data; cout << “We received: “ << data << “\nIs this correct? (y/n)”; } CS161 Topic #7

Using a for Loop Remember to use semicolons after each statement; however, a semicolon right after the parentheses will cause there to be a null body (i.e., nothing will be executed as long as you are inside the loop!): for (i=1; i <= 10; ++i) ; //null body cout << "hello"; //this happens ONLY //after i is > 10. CS161 Topic #7

Using a do while Loop When using loops, desk check for the following conditions: (1) Has the loop iterated one too many times? Or, one too few times? (2) Have you properly initialized the variables used in your while or do-while logical expressions? (3) Are you decrementing or incrementing those variables within the loop? (4) Is there an infinite loop? CS161 Topic #7

CS161 Introduction to Computer Science Questions on Prog #3?