Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
Agenda  Perform Quiz#2 (20 Minutes)  Functions / Continued … –Functions - Definition –Types of Functions: Functions that do not accept or return a value.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
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.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Agenda Basic Logic Purpose if statement if / else statement
Java Prepared by Gary Langner Types of Loops u for loop (entrance controlled) –do an action for a definite number of times u while loop (entrance controlled.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Loops and Files. 5.1 The Increment and Decrement Operators.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Chapter 5: Structured Programming
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Lecture 7: Repeating a Known Number of Times
Chapter 5: Control Structures II
Loop Structures.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Chapter 5: Loops and Files.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Repetition-Sentinel,Flag Loop/Do_While
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
Looping and Repetition
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
REPETITION STATEMENTS
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
More Loops Topics Relational Operators Logical Operators for Loops.
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.
ICS103: Programming in C 5: Repetition and Loop Statements
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Presentation transcript:

Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure / Examples involving a do/while loop –Homework Question

Loops –Loops are used for repetition when running a program. –To make sure the loop ends, we need a condition to control the loop; otherwise, the loop will continue forever! Perform action

Types of loops while and do/while Loops –The “while” and “do/while” loops are often called indeterminate loops because they are best used when the number of repetitions are not known in advance. for Loops –The “for” loop is often called a determinate loop since it is best used when the number of repetitions is already known.

In Class Exercise #1 Which are the best types of loops for the following situations: determinate or indeterminate? –ask user to enter a group of marks until they are finished. –print a multiplication table (1 - 12) –indicate that user has inputted an invalid selection, and needs to try again (error checking)

while loop Test condition Commands if TRUE true false The “while” loop test the condition first (referred to as a pre- test). Therefore, if the pre- test condition is FALSE, the loop is NOT executed.

while loop - Example 1 main () { int i = 0; while ( i < 3 ) { printf (“i is: %d\n”, i); i = i + 1; } OUTPUT: i is: 0 i is: 1 i is: 2 Declare and assign variable i (counter) Pretest condition involving variable i Perform action in while loop if test is TRUE, advance the value of variable i (or “counter”) by 1 and return to pretest Tip: the short-form of i = i + 1; is i++; i = I - 1; is i--;

while loop - Example 2 main () { int i ; printf (“\nEnter a positive integer: “); scanf (“%d”, &i); while ( i < 0 ) { printf (“Oops! Enter a positive integer: ”); scanf (“%d”, &i); } printf (“\n”); } Declare and assign variable i (used to store selection, NOT used as a counter!) Prompt & scan value of i Notice NO semicolon “;” when performing pretest and notice braces (code block) Perform action if condition is TRUE (error-checking procedure). User is informed of mistake and must re-enter until a positive integer is finally entered

while loop - Example 2 main () { int i ; printf (“\nEnter a positive integer: “); scanf (“%d”, &i); while ( i < 0 ) { printf (“Oops! Enter a positive integer!: ”); scanf (“%d”, &i); } printf (“\n”); } It is OK to give very simple variable names like i, j, x or y when using them for looping purposes

In Class Exercise #2 Can you think of how both programs above could be modified to create “infinite loops?”

while loop - Example 1 main () { int i = 0; while ( i != 3 ) { printf (“i is: %d\n”, i); i = i + 2; } OUTPUT: i is: 0 i is: 2 i is: , etc. etc. etc ---> forever! Test condition will only be FALSE (thus stop the loop) if i is not equal to 3, but counter can skip 3 and go beyond 3! So you have an infinate loop!

while loop - Example 2 main () { int i ; printf (“\nEnter a positive integer: “); scanf (“%d”, &i); while ( i < 0 ) { printf (“Oops! Enter a positive integer: ”); } printf (“\n”); } WARNING: If you do NOT include an opportunity to allow user to re-enter again, you will encounter an “infinite loop”!!

do/while loop Test condition Commands true false The do statement performs an action first, and then is followed by the while statement to test condition (referred to as a “post test”) Therefore, action is performed at least once, even if condition is FALSE

do/while loop - Example 1 main () { int i = 10; do { printf (“i is: %d\n”, i); i--; } while (i > 0); } Declare and assign variable i (counter) Perform action first: Prompt & scan value of i After action is performed once, then perform post test) Notice while condition immediately after closing brace for do statement, and semi-colon “;” after while statement!

do/while loop - Example 2 main () { int num; do { printf (“\nGuess the secret number: ”, num); scanf (“%d”, &num); } while (num != 5); printf (“That’s right!\n”); printf (“The secret number is 5.\n\n”); } When post test is false (i.e. num is equal to 5, program continues, and prints the message “That’s right! The secret number is 5.” Declare variable num Prompt user for number, and then test and repeat loop while num is not equal to 5

Homework Question Create a program to prompt the user for a group of integers. If the user enters a negative integer, then the program should display the average of the entered integers. –What loop should you use? –Plan the program –Create & compile the source code