Controls Structure for Repetition

Similar documents
Boolean Expressions. So, for example, here are the results of several simple Boolean expressions:

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Arrays. Here is the generic syntax for an array declaration: type[] <var_name>; Here's an example: int[] numbers;

Control Structures II. Repetition (Loops)

Repetition CSC 121 Fall 2014 Howard Rosenthal

CompSci 125 Lecture 11

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Iteration statements - Loops

CS 101 Spring 2007 Midterm 2 Name: ID:

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops.

Programming with Java

First Java Program - Output to the Screen

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

Repetition, Looping. While Loop

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

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

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

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

Loops. CSE 114, Computer Science 1 Stony Brook University

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

STUDENT LESSON A12 Iterations

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

Example: Monte Carlo Simulation 1

Java Coding 3. Over & over again!

CS 101 Fall 2006 Midterm 3 Name: ID:

Repetition Structures

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

CSE 1223: Introduction to Computer Programming in Java Chapter 6 ArrayLists

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Computer Programming I - Unit 5 Lecture page 1 of 14

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

Handout 4 Conditionals. Boolean Expressions.

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

CSE 1223: Exam II Autumn 2016

Flow of Control: Loops. Chapter 4

Chapter 4: Control structures. Repetition

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

1007 Imperative Programming Part II

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Midterm Examination (MTA)

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Chapter 4: Control structures

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

CSE 1223: Introduction to Computer Programming in Java Chapter 6 Arrays

Chapter 4: Control Structures I

Darrell Bethea May 25, 2011

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Introduction to Computer Programming

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

More on Classes. The job of this method is to return a String representation of the object. Here is the tostring method from the Time class:

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

CS 101 Exam 2 Spring Id Name

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

Some Sample AP Computer Science A Questions - Solutions

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

COMP 202 Java in one week

Chapter Goals. Contents LOOPS

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

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

REPETITION CONTROL STRUCTURE LOGO

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Full file at

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

CSE 114 Computer Science I

Chapter 3. Selections

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

4. Java Project Design, Input Methods

Building Java Programs

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

CMPT 125: Lecture 4 Conditionals and Loops

Example Program. public class ComputeArea {

Arrays. Eng. Mohammed Abdualal

Advanced if/else & Cumulative Sum

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

Building Java Programs

C++ Programming: From Problem Analysis to Program Design, Third Edition

CP122 CS I. Iteration

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }

Oct Decision Structures cont d

Introduction to the Java Basics: Control Flow Statements

Problem Solving With Loops

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

AP Programming - Chapter 6 Lecture

COMP 202. Java in one week

Condition Controlled Loops. Introduction to Programming - Python

Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

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

Transcription:

Controls Structure for Repetition So far we have looked at the if statement, a control structure that allows us to execute different pieces of code based on certain conditions. However, the true power of computers lies in their ability to repeat a set of instructions many times in a short period of time. Many algorithms contain directives to repeat a designated set of steps an arbitrary number of times. In Java we have a simple loop structure, called a while statement, which allows us to repeat a piece of code multiple times. Here is the general syntax of a while statement: while (<boolean expression>) { stmt1; stmt2;... stmtn; This statement is executed in the following manner: 1) First the boolean expression is evaluated. 2) If it is true, execute statements 1 through n in order. 3) If it is false, skip all the statements inside the while loop braces({). 4) Go back to step #1. In essence, you check your boolean condition each time before you decide whether or not to execute the statement in the loop. You continue doing so until you check the condition and it is false. What we can see from this construct is typically, for it to be effective, the boolean condition must first be true, and eventually become false. Thus, if the boolean condition was in terms of a variable, such as: while (x < 10) {... For this to be an effective loop, the value of x must change in the body of the loop. If it does not, then the condition will always evaluate to the same thing.

The big problem with this is if it happened to be true to begin with, it would never stop!!! This is known as an infinite loop. One of the most common problems that beginning programmers have when designing loops is having them run forever, so to speak. We will talk about how to detect these later. Now lets consider trying to write a program to do the following: 1) Prompt the user if they would like to enter a test score. 2) If so, reading it in and then asking them for another test score. 3) Continue the process above until the user says no. 4) Output the average of the test scores entered by the user. Before we get down to writing this program, let s analyze how we will need to lay everything out. To compute an average, we must know two pieces of information: 1) The sum of the numbers 2) The number of numbers being averaged. Notice that we don t actually need to know each individual number. So the idea that we have is the following: Create two variables, one to store the sum of the numbers and one to store the number of numbers. Furthermore, we will also need a variable to read in the answer of the user as to whether or not they want to enter another score. Finally, we will also need a variable to read each individual score into. In general, we want to continue reading in scores until the user says they do not want to enter any, so our general layout will look like the following: <initialize variables> while (<user says they want to enter another score>) { <ask user for score> <adjust necessary variables <check to see if they want to enter another score>

The process we have gone through is basically the algorithmic design process. Notice that we haven t truly written any code yet, but we have a very good idea of how we are going to solve this problem. Whenever you are given a problem to write a program for, you should always go through this algorithmic design process. You want to identify the variables you will need, as well as the general flow of execution. That way, once you actually write your program, it will be much easier to debug(which is simply a fancy word for fix) it. So, without further due, here is the program: public class Average { public static void main(string args[]) { Scanner stdin = new Scanner(System.in); // Declare and initialize all necessary variables. char answer; int sum, score, numscores; double average; numscores = 0; sum = 0; // Read in whether or not user would like to enter a score. System.out.println( Would you like to enter a score?(y/n) ); answer = (stdin.next()).charat(0); // Continue reading in scores as long as user wants. while (answer == y ) { // Get next score. System.out.println( Enter the next score. ); score = stdin.nextint(); // Update necessary variables. sum = sum + score; numscores = numscores + 1;

// Prompt user if they want to enter another score or not. System.out.println( Would you like to enter a score?(y/n) ); answer = (stdin.next()).charat(0); average = sum/numscores; System.out.println( The average of your scores is + average); Now, there are actually a couple subtle problems with the program above. The first is that even though the statement average = sum/numscores is mathematically correct, two things can go wrong with this statement: 1) If numscores is 0, then we are dividing by a 0, which is a big no no!!! 2) average will always be set to an integer because the quotient of two integers is defined to be an integer in Java as well. How can we solve the first problem? Notice that our English write-up of the problem looks a lot like Java code. We can simply use an if statement to check the value of numscores. How about the second problem? We know that Java will correctly divide a double by an integer, so we can change our assignment statement to: average = 1.0*sum/numscores. The expression on the right is evaluated left to right. 1.0*sum is going to give us a double as an answer, equal to sum. Then this double will get divided by numscores, also giving us a double, which will be the correct answer. So, here is our fix for the last section of our code: if (numscores > 0) { average = 1.0*sum/numscores; System.out.println( The average of your scores is + average); else

System.out.println( Sorry, you did not enter any numbers to average. ); Let s analyze our code to look for general types of constructs for particular solutions. First of all, the while loop we used is typically known as a sentinel-controlled loop. What this means is that the loop continues to run until some sentinel value, which is meant to indicate the end of the data, is read in. In this case, we simply prompted the user as to whether or not they wanted to enter another value each time. Our sentinel value was the character n. (Technically it was every character but y.) This is a standard technique. Often times you will have to read in several pieces of data, and will be looking for some value that signifies the end of that data. We could have written this program in such a manner that we asked the user to enter all of their scores at once, followed by a 1. Since it is impossible for 1 to be a test score, this would have been the sentinel value we were looking for. In any event, in a situation like this, you simply use a while loop that runs until there is equality with the sentinel value. The second standard technique introduced in this program is the user of an accumulator variable. Often times you will have to sum up a set of values. You can use an accumulator variable to do so. In this program the accumulator variable was sum. The way you typically use an accumulator variable is to initialize it to zero. Then, whenever you encounter a value you need to add into your sum, simply have a line with the following form: sum = sum + <value>; In essence, each time you execute a line of this type, you are taking the old value of sum, adding to it a value that you want to, and then storing that sum back into the accumulator variable. It is instructive to trace through a couple examples to convince yourself that this technique works.