1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
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.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
CS150 Introduction to Computer Science 1
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Chapter 5: Control Structures II (Repetition)
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
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 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
 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.
Previously Repetition Structures While, Do-While, For.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
CS 100 Introduction to Computing Seminar October 7, 2015.
Overview Go over parts of quiz? Another iteration structure for loop.
1 CS161 Introduction to Computer Science Topic #8.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
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.
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.
1 9/26/05CS150 Introduction to Computer Science 1 Life is Full of Alternatives.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Introduction to Computer Programming
REPETITION CONTROL STRUCTURE
while Repetition Structure
CS161 Introduction to Computer Science
for Repetition Structures
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Chapter 2.2 Control Structures (Iteration)
JavaScript: Control Statements I
Unary Operators ++ and --
3 Control Statements:.
Repetition Control Structure
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
2.6 The if/else Selection Structure
Let’s all Repeat Together
do/while Selection Structure
Reading from and Writing to Files
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CS150 Introduction to Computer Science 1
Life is Full of Alternatives Part 3
Reading from and Writing to Files
Presentation transcript:

1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together

2 09/20/04CS150 Introduction to Computer Science 1 Last Time  We o Looked at complex examples that use the if selection structure o Covered the if/else selection structure o Learnt about the ?: conditional operator  Today we will o Learn how to nest if/else selection structures o Start looking at examples of repetition structures

3 09/20/04CS150 Introduction to Computer Science 1 Nested if/else Selection Structures  What if there are more than two alternatives in a solution? if (condition1) statement1; else if (condition2) statement2; … else default statement;

4 09/20/04CS150 Introduction to Computer Science 1 Problem  Write a C++ program segment that allows the user the ability to input an integer from the keyboard. If the integer is positive, increment a variable poscount by 1. If the integer is negative, increment a variable negcount by 1. If neither, increment zerocount by 1

5 09/20/04CS150 Introduction to Computer Science 1 Solution cin >> intvalue; if(intvalue > 0) poscount = poscount + 1; else if(intvalue < 0) negcount = negcount + 1; else zerocount = zerocount + 1;  Can you come up with another way of doing this?

6 09/20/04CS150 Introduction to Computer Science 1 Solution  Will this solution work? cin >> intvalue; if(intvalue > 0) poscount = poscount + 1; if(intvalue < 0) negcount = negcount + 1; if(intvalue = 0) zerocount = zerocount + 1;

7 09/20/04CS150 Introduction to Computer Science 1 Problem  Write a program that displays a letter grade corresponding to an exam score A B C D 0-59 F

8 09/20/04CS150 Introduction to Computer Science 1 Repetition Structures  All the C++ programs that we have seen so far are executed only once before the program terminates  However, it is often the case that programmers would like to specify that an action continue repeating while a condition is true  This is achieved by using repetition structures, also called loops

9 09/20/04CS150 Introduction to Computer Science 1 An Example of Repetition  An example of where we might need to use repetition is if we are calculating the average grade of a class of students  We would need to continue reading in student grades until we have covered all students  In pseudocode this might be: While there are more students in the class Ask for student grade

10 09/20/04CS150 Introduction to Computer Science 1 C++ Example  Write the segment of C++ code that will sum five numbers entered by the user int sum, counter, num; sum = 0; counter = 0; while( counter < 5 ) { cout << "Enter a number: "; cin >> num; sum = sum + num; counter = counter + 1; } cout << "The sum of your numbers is: " << sum;

11 09/20/04CS150 Introduction to Computer Science 1 while Repetition Structure int sum, counter, num; sum = 0; counter = 1; while( counter <= 5 ) { cout << "Enter a number: "; cin >> num; sum = sum + num; counter = counter + 1; } cout << "The sum of your numbers is: " << sum; Initialize LCV Loop Control Variable Change the value of count

12 09/20/04CS150 Introduction to Computer Science 1 while loops  The syntax for while loops is while (condition is true) statement; while (condition is true) { statement1; statement2; … }

13 09/20/04CS150 Introduction to Computer Science 1 Key Ingredients of while loops  Initialize MUST initialize loop control variable  Test The value of the loop control variable is tested during each iteration of loop  Update Loop control variable is changed during each loop iteration If any one of these is missing or incorrect, your loop won’t run properly--not at all, too many/few times or infinitely.

14 09/20/04CS150 Introduction to Computer Science 1 Problems  Write a while loop that outputs each integer from 1 to 5  What’s the output for x = 2? 3? 5? cout << “Enter an integer”; cin >> x; product = x; count = 0; while (count < 4) { cout << product << endl; product *= x; count += 1; }

15 09/20/04CS150 Introduction to Computer Science 1 Summary  In today’s lecture we covered o Nested if/else selection structures o while repetition structure  Readings o P : nested if/else selection structures and if structures with multiple statements o P : while repetition structure