Repetition Algorithms

Similar documents
Loops / Repetition Statements

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include

PROBLEM SOLVING WITH LOOPS. Chapter 7

Algorithms. Abdelghani Bellaachia, CSCI 1121 Page: 1

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Score score < score < score < 65 Score < 50

CS110D: PROGRAMMING LANGUAGE I

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

BIL101E: Introduction to Computers and Information systems Lecture 8

C Programming for Engineers Structured Program

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Final Examination Semester 3 / Year 2010

Loops / Repetition Statements

Lecture Transcript While and Do While Statements in C++

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

8 Algorithms 8.1. Foundations of Computer Science Cengage Learning

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Problem Solving with Decisions. T.Fatin Alhila

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

Loops (while and for)

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Chapter 3 Problem Solving and the Computer

STUDENT LESSON A12 Iterations

Theory of control structures

Outline. Program development cycle. Algorithms development and representation. Examples.

Review: Repetition Structure

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

Ms. Payne, WCSS. This is very much like asking a question, and having a yes/no answer (also known a true/false). The general structure for this is:

Computer Programming. Decision Making (2) Loops

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

Chapter 3 Structured Program Development

Chapter 8 Algorithms 1

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

FORM 2 (Please put your name and form # on the scantron!!!!)

REPETITION CONTROL STRUCTURE LOGO

Chapter 1: Problem Solving Skills Introduction to Programming GENG 200

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Computer System and programming in C

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Outline. Review Choice Statements. Review Sequential Flow. Review Choice Before Loops. Review Choice After Loops

Chapter 5 Conditional and Iterative Statements. Statement are the instructions given to the computer to perform any kind of action.

CS112 Lecture: Repetition Statements

Lab 2: Structured Program Development in C

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

Introduction. C provides two styles of flow control:

Administrative Notes January 19, 2017

Initial Coding Guidelines

Fundamental of Programming (C)

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Use the scantron sheet to enter the answer to questions (pages 1-6)

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Fundamentals of Programming

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Logic & Algorithms Foundations of Computer Science Behrouz A. Forouzan, Brooks/Cole Thomson Learning, Pacific Grove, USA, 2003.

Individual research task. You should all have completed the research task set last week. Please make sure you hand it in today.

Chapter Two: Program Design Process and Logic

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

Principles of Computer Science I

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Equivalent. Lesson 7(Chapter 8) Sample Problem 4. Problem Analysis Chart. Roman Decimal I = 1 V = 5 X = 10 L = 50 C = 100 D = 500 M = 1000

Math 6 Unit 2: Understanding Number Review Notes

Adding Integers. Unit 1 Lesson 6

Repetition Structures Chapter 9

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CHAPTER 2 PROBLEM SOLVING TECHNIQUES. Mr Mohd Hatta Bin Hj Mohamed Ali Computer Programming BFC2042

Loops and Files. of do-while loop

Learning Objectives: General Description: DONE DONE Structure Chart

Control Structure: Loop

n Group of statements that are executed repeatedly while some condition remains true

LECTURE 5 Control Structures Part 2

Chapter 5: Control Structures

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 5. Aykut Erdem, Erkut Erdem, Fuat Akal

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Midterm Practice Problems

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Add Subtract Multiply Divide

Final Examination Semester 2 / Year 2010

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

CS1100 Introduction to Programming

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

Programming for Engineers Iteration

Chapter 6. Repetition Statements. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Test #2 October 8, 2015

Decision Making -Branching. Class Incharge: S. Sasirekha

CSC 121 Spring 2017 Howard Rosenthal

Programming Logic and Design Sixth Edition

How Do Robots Find Their Way?

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation

Transcription:

Repetition Algorithms

Repetition Allows a program to execute a set of instructions over and over. The term loop is a synonym for a repetition statement.

A Repetition Example Suppose that you have been asked to write a program that allows the user to enter 5 integers and displays the sum of the integers on the screen.

A Repetition Example Input Processing Output 5 integers Num1 Num2 Num3 Num4 Num5 1, Num2, Num3, Num4 and Num5 Calculate the Total Display label and Total Total

A Repetition Example AddFive 1, Num2, Num3, Num4, and Num5 Total = Num1 + Num2 + Num3 + Num4 + Num5 Display label and Total

A Repetition Example - While AddFive(While Version) Counter = 1 While Counter <= 5 Counter = Counter + 1 While

A Repetition Example Total Counter Num AddFive(While Version) Counter = 1 While Counter <= 5 Counter = Counter + 1 While

A Repetition Example - Until AddFive(Until Version) Counter = 1 Do until Counter = 6 Counter = Counter + 1 do

A Repetition Example Total Counter Num AddFive(Until Version) Counter = 1 Do until Counter = 6 Counter = Counter + 1 do

A Repetition Example - For AddFive(For Version) For Counter = 1 to 5 for

A Repetition Example Total Counter Num AddFive(For Version) For Counter = 1 to 5 for

A Repetition Example While Exit AddFive (While Exit Version) Counter = 1 Do Counter = Counter + 1 While Counter <= 5

A Repetition Example Total Counter Num AddFive (While Exit Version) Counter = 1 Do Counter = Counter + 1 While Counter <= 5

A Repetition Example Until Exit AddFive (Until Exit Version) Counter = 1 Do Counter = Counter + 1 Until Counter = 6

A Slightly Different Example Assume that you have been asked to write a program that allows the user to enter and add positive integers. The user will enter any negative number when he/she is finished entering numbers and would like to see the result. The dummy number used to stop processing should not be added to the total.

A Slightly Different Example Input Processing Output A set of positive integers. Any negative number can be used to stop processing. Repeat for each number Get the number Add number to total Display label and Total Total

A Slightly Different Example AddPositiveNumbers (While Version) Do while Num >= 0 while

A Slightly Different Example Total Num AddPositiveNumbers (While Version) Do while Num >= 0 while

A Slightly Different Example AddPositiveNumbers (While Exit Version 1) Do While Num >= 0

A Slightly Different Example Total Num AddPositiveNumbers (While Exit Version) Do While Num >= 0

A Slightly Different Example AddPositiveNumbers (While Exit Version 2) Do If Num >= 0 Then if While Num >= 0

A Slightly Different Example Total Num AddPositiveNumbers (While Exit Version 2) Do If Num >= 0 Then if While Num >= 0

With Any Problem Follow the same process Can you ask clarifying questions? Can you create an IPO chart? Can you write an algorithm? Can you do an example or describe the process in English? Can you generalize that? Does any of the processing involve selection? Does any of the processing involve repetition?

Practice Problem Assume that you are creating a program that will count the number of students in a class who are getting an A. The user will enter the letter grade for each student in the class, one grade at a time and will enter an S when all grades have been entered. The program will display the number of A grades to the screen.

Practice Problem Assume that you are creating a program that will count the number of students in a class who are passing a course. The user will enter an integer value between 1 and 100 for each student in the class, one grade at a time and will enter a 0 when all grades have been entered. Students who score below 70 do not pass the course. The program will display the number of passing scores to the screen.

A More Complex Repetition Problem Assume that you are creating a program that will be used by customers to locate a specific movie in a video store. The customer should be allowed to enter the name of the movie and the program will display the location of the movie on the screen.

A More Complex Repetition Problem LookupMovie Found = False Get MovieTitle Get first MovieRecord Do Until Found or Of File If MovieTitle = MovieRecord.Title Then Found = True Else Get nextmovierecord if Do If Found Then Display label and MovieRecord.Location Else Display Not Available message If

Practice Problem Assume that you are creating a program that will determine if a number is a prime number. The user enters an integer between 4 and 100. The program prints either Prime or Composite.

Practice Problem Assume that you ve been asked to write a program that displays a multiplication chart like the one given below. The user enters an integer that represents the dimension of the chart. 1 2 3 4 2 4 6 8 3 6 9 12 4 8 12 16