COSC The if Statement. Yves Lespérance. Lecture Notes Week 5 Control Structures

Similar documents
COSC The StringTokenizer Class. Yves Lespérance. Lecture Notes Week 7 Testing & File I/O

Character Strings COSC Yves Lespérance. Lecture Notes Week 6 Java Strings

Chapter 5 Control Structures

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

Introduction to the Java Basics: Control Flow Statements

Java I/O and Control Structures

Name Section: M/W T/TH Number Definition Matching (8 Points)

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Repetition Structures

All classes in a package can be imported by using only one import statement. If the postcondition of a method is not met, blame its implementer

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

Java I/O and Control Structures Algorithms in everyday life

Name Section: M/W T/TH Number Definition Matching (6 Points)

CS110D: PROGRAMMING LANGUAGE I

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Programming Lecture 4

Conditional Statements

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

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

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

LECTURE 5 Control Structures Part 2

Java Coding 3. Over & over again!

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

Computer Programming I - Unit 5 Lecture page 1 of 14

Introduction to Software Development (ISD) Week 3

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

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

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

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Programming Lecture 4

Programming Lecture 4

Loops / Repetition Statements

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

ECE 122. Engineering Problem Solving with Java

STUDENT LESSON A12 Iterations

Control Structures: if and while A C S L E C T U R E 4

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

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

REPETITION CONTROL STRUCTURE LOGO

Repetition CSC 121 Fall 2014 Howard Rosenthal

Chapter 4: Control structures. Repetition

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

Improved algorithm. Java code. Control flow and conditionals CSC 1051 Villanova University. Dr Papalaskari 1. Java Programè Algorithm

Control flow, conditionals, boolean expressions, block statements, nested statements. Course website:

Java code. Updated program. Control flow and conditionals CSC 1051 Villanova University. Dr Papalaskari 1. Java Programè Algorithm. Improved algorithm

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

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

Chapter Goals. Contents LOOPS

COSC Exception Handling. Yves Lespérance. Lecture Notes Week 10 Exception Handling

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Introduction. C provides two styles of flow control:

Selection and Repetition Revisited

Chapter 4: Control structures

Controls Structure for Repetition

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation

Conditionals and Loops

GROUP - A <10 questions x 4 points each = 40 points >

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

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

Dept. of CSE, IIT KGP

class objects instances Fields Constructors Methods static

CS111: PROGRAMMING LANGUAGE II

Loops (while and for)

Algorithms and Conditionals

Loops. CSE 114, Computer Science 1 Stony Brook University

CS112 Lecture: Repetition Statements

CSC 1051 Data Structures and Algorithms I

A Look Back at Arithmetic Operators: the Increment and Decrement

COMP 202 Java in one week

Chapter 3 Problem Solving and the Computer

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Programming for Engineers Iteration

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

Marking rubric for Assignment #2

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

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

Control Statements. Musa M. Ameen Computer Engineering Dept.

CSC 1051 Data Structures and Algorithms I

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

McGill University School of Computer Science COMP-202A Introduction to Computing 1

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

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

LOOPS. Repetition using the while statement

CS159. Nathan Sprague. November 9, 2015

Fundamentals of Programming Session 13

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

An introduction to Scheme

3chapter C ONTROL S TATEMENTS. Objectives

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

In this chapter you will learn:

CS 302: Introduction to Programming in Java. Lecture 11 Yinggang Huang. CS302 Summer 2012

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

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

Programming for Experimental Research. Flow Control

AP Programming - Chapter 6 Lecture

Selection and Repetition Revisited

Transcription:

COSC 1020 Yves Lespérance Lecture Notes Week 5 Control Structures Recommended Readings: Horstmann: Ch. 5 and 6 Lewis & Loftus: Ch. 3 The if Statement The if statement is used to select which is to be performed among some alternative set of operations. There are several versions. The simplest one is used to perform a statement only if a condition (boolean expression) is true: if (condition) statement E.g. double fare = 8.0; if (age <= 17) fare = 5.0; IO.println("Fare is " + fare); 1 Often, you want to perform several statements when the condition holds. You can always group several statements into one by putting them between braces. This is called a block. E.g. IO.print("Please enter your age: "); int age = IO.readInt(); if (age < 0) { IO.println("Negative age!"); IO.print("Enter correct age: "); age = IO.readInt(); A second version of the if statement is used to perform one statement if a condition is true and another statement if it is false: if (condition) statementift statementiff E.g. calculating jxj if (x < 0) abs_x = -x; abs_x = x; 2 3

We can also do selection among more than two alternatives by cascading if-s into one another. As well, we can tack on a catchall case at the end for a statement to be performed if none of the conditions are true. E.g. final int DISCOUNT_AGE_LIMIT = 16; IO.print("How old are you? "); int age = IO.readInt(); if (age <= DISCOUNT_AGE_LIMIT) { IO.println("You get the discounted fare!"); IO.println("Please remit $5.00."); { IO.println("You must pay the regular fare."); IO.println("Please remit $8.00."); if (condition1) statement1 if (condition2) statement2 if (condition3) statement3... if (conditionn) statementn statementotherwise Note that even if more than one of the conditions is true, only the statement for the first true condition is performed. 4 5 Problem: Given a numerical grade, print out the letter grade equivalent. IO.print("Your letter grade is "); if (grade >= 80) IO.println("A"); if (grade >= 70) IO.println("B"); if (grade >= 60) IO.println("C"); if (grade >= 50) IO.println("D"); IO.println("F"); What is the output if the variable grade has value 72? What if it is 47? You can have if statements nested inside other if statements. E.g. final static int HEADS = 1; final static int RECEIVE = 1; IO.print("Enter 1 for heads and 2 for tails: "); int coin = IO.readInt(); IO.print("Enter 1 to receive and 2 to kickoff: "); int choice = IO.readInt(); if (coin == HEADS) { if (choice == RECEIVE) IO.println("You won the toss and will receive."); IO.println("You won the toss and will kickoff."); IO.println("You lost the coin toss."); Note that an is matched with the most recent unmatched if. Proper indentation is important for readability; it should reflect the structure of nesting! 6 7

Problem: for each possible value of cond1 and cond2, say what the value of r is after the following code is executed, int r = 0; if (cond1) if (cond2) r = 1; r = 2; What if we add braces? int r = 0; if (cond1) { if (cond2) r = 1; r = 2; E.g. MkChange Revisited Modify the MkChange program to use singular and plural correctly, omit coins that don t have to be included, and reject negative amounts. E.g. Script started on Thu Sep 30 14:57:36 1999 Terminal is : xterm Display is: fxt23.cs.yorku.ca:0.0 tiger 41 % java MkChange2 Enter the amount in cents: 68 Change is: 2 quarters 1 dime 1 nickel 3 pennies. tiger 42 % java MkChange2 Enter the amount in cents: 97 Change is: 3 quarters 2 dimes 2 pennies. tiger 43 % java MkChange2 Enter the amount in cents: 12 Change is: 1 dime 2 pennies. tiger 44 % java MkChange2 Enter the amount in cents: 0 Change is:. tiger 45 % java MkChange2 Enter the amount in cents: -20 The amount can t be negative. Goodbye. tiger 46 % exit exit script done on Thu Sep 30 14:58:42 1999 8 9 import type.lang.; public class MkChange2 { final int QUARTER_VALUE = 25; final int DIME_VALUE = 10; final int NICKEL_VALUE = 5; IO.print("Enter the amount in cents: "); int amount = IO.readInt(); if (amount < 0) IO.println("The amount can t be negative. Goodbye."); { int nquarters = amount / QUARTER_VALUE; amount = amount % QUARTER_VALUE; int ndimes = amount / DIME_VALUE; amount = amount % DIME_VALUE; int nnickels = amount / NICKEL_VALUE; int npennies = amount % NICKEL_VALUE; IO.print("Change is:"); if (nquarters > 1) IO.print(" " + nquarters + " quarters"); if (nquarters == 1) IO.print(" " + nquarters + " quarter"); // when nquarters == 0 print nothing if (ndimes > 1) IO.print(" " + ndimes + " dimes"); if (ndimes == 1) IO.print(" " + ndimes + " dime"); if (nnickels > 1) IO.print(" " + nnickels + " nickels"); if (nnickels == 1) IO.print(" " + nnickels + " nickel"); if (npennies > 1) IO.print(" " + npennies + " pennies"); if (npennies == 1) IO.print(" " + npennies + " pennie"); IO.println("."); 10 Repetition E.G Problem: print a table of the squares of positive integers from 1 to 10 as below; make sure the colums are correctly alligned. n nˆ2 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100 11

Algorithm in pseudocode Design Must print one line at a time; in each line print n in a column of width 2, then 3 spaces, then n 2 in a column of width 3. Printing the table is a repetitive task. Know in advance how many repetitions, 10 = N MAX, sousefor loop. n starts at 1, increases by 1 each repetition, and the loop stops when n > N_MAX. So use for (int n = 1; n <= N_MAX; n++) 12 print header for n from 1 to N MAX incrementing by 1 { print n in column of width 2 print 3 spaces print n 2 in column of width 3 Code import type.lang.; public class SquaresTbl { final int N_MAX = 10; IO.println(" n nˆ2"); for (int n = 1; n <= N_MAX; n++) { IO.print(n, "2"); IO.print(" "); IO.println(n n, "3"); 13 for Loops When we want to repeatedly perform some operations thisiscalledaloop and the number of repetitions is known before the loop begins, we use a loop with a counter. Java provides the for structure for this; its syntax is: for (init; cond; update) statement init is an expression that is executed at the beginning of the loop, usually to initialize the counter; cond is a condition that is tested at the beginning of each cycle the loop continues while it remains true; update is an expression that is executed at the end of every cycle and is typically used to update the counter for the next cycle. E.g. read a positive integer n and print the sum of the first n positive integers: int sum = 0; IO.print("Enter a pos. integer: "); int n = IO.readInt(); for (int i = 1; i <= n; i++) sum = sum + i; IO.println("The sum of the first " + n + " pos. integers is " + sum); The counter can start at any value, go down as well as up, and change by an arbitrary amount, e.g. for (int i = 5; i <= n; i++) for (int i = 10; i > 0; i--) for (int i = 0; i <= n; i = i + 5) 14 15

Problem: write a program that prints a triangle on the screen as shown below; the size of the triangle (number of lines) should be read from the user. tiger 87 % java DrawTriangle Enter number of lines the triangle should contain: 4 tiger 88 % java DrawTriangle Enter number of lines the triangle should contain: 7 Design First decomposition of task: print prompt read nlines print triangle of size nlines 16 Printing triangle subtask: can t be done in one shot, must repeatedly print one line. Number of repetitions known, nlines. So use a for loop. Algorithm becomes: print prompt read nlines for i from 1 to nlines incrementing by 1 print line i of triangle Printing line i of triangle subtask: How many spaces on line i? How many stars on line i? Because number of spaces (stars) varies with line number, can t be done in one shot, must repeatedly print one space (star) at a time. Because know how many repetitions in advance, use for loops. 17 Algorithm print prompt read nlines for i from 1 to nlines incrementing by 1 { nspaces = i - 1; nstars = 2 (nlines - i) + 1; for j from 1 to nspaces incrementing by 1 print a space for j from 1 to nstars incrementing by 1 print a star skip to next line Code import type.lang.; public class DrawTriangle { IO.print("Enter number of lines the triangle shou int nlines = IO.readInt(); for(int i = 1; i <= nlines; i++) { int nspaces = i - 1; int nstars = 2 (nlines - i) + 1; for(int j = 1; j <= nspaces; j++) IO.print(" "); for(int j = 1; j <= nstars; j++) IO.print(""); IO.println(); 18 Top-Down Design Start by producing a very coarse high-level algorithm to solve the problem. This requires identifying steps in the solution or subproblems. Ok if subproblems must be solved later. Then work on subproblems separately, applying the same decomposition method. Repeat until the all subproblems have been solved and the algorithm is sufficiently detailed to be writen in code. Top-down design can be used in combination with objectoriented design. 19

Problem: write a program that prints a square wave as shown below; input the amplitude and wavelength from the user; both pos. integers; wavelength should be even. tiger 122 % java SquareWaves Enter amplitude: 3 Enter wavelength: 4 tiger 123 % java SquareWaves Enter amplitude: 4 Enter wavelength: 6 20 Design Need to repeatedly print cycles of the wave, here 2 cycles. Use constant N CYCLES. Know how many repetitions, so for loop. 1st decomposition: print prompt & read amplitude print prompt & read wavelength for i from 1 to N CYCLES print a cycle To print a cycle, there are 4 subtasks. 2nd decomposition: print row of stars of length 2amplitude print column of stars of length wavelength/2-1 indented by 2amplitude-1 spaces print row of stars of length 2amplitude print column of stars of length wavelength/2-1 21 To print row of stars of length 2amplitude, need to repeatedly print a star 2amplitude times. Algorithm: for j from 1 to 2amplitude print a star skip to next line To print column of stars of length wavelength/2-1, need to repeatedly print lines with a star on it that many times. Algorithm: for j from 1 to wavelength/2-1 print a line with a star To print column of stars of length wavelength/2-1 indented by 2amplitude-1 spaces, need to repeatedly print such a line that many times. Algorithm: for j from 1 to wavelength/2-1 print 2amplitude-1 spaces print a star and skip to next line To print 2amplitude-1 spaces, repeatedly print a space that many times. Algorithm: for k from 1 to 2amplitude-1 print a star 22 23

Code import type.lang.; public class SquareWaves { public static void main (String[] args) { final int N_CYCLES = 2; IO.print("Enter amplitude: "); int amplitude = IO.readInt(); IO.print("Enter wavelength: "); int wavelength = IO.readInt(); for (int i = 1; i <= N_CYCLES; i++) { for (int j = 1; j <= 2amplitude; j++) IO.print(""); IO.println(); for (int j = 1; j <= wavelength/2-1; j++) { for (int k = 1; k <= 2amplitude-1; k++) IO.print(" "); IO.println(""); for (int j = 1; j <= 2amplitude; j++) IO.print(""); IO.println(); for (int j = 1; j <= wavelength/2-1; j++) IO.println(""); Other Uses of for for loops are also used to iterate over members of a collections or input records. E.g. import type.lang.; public class SalaryAvg { double total = 0.0; int count = 0; IO.println("Enter salaries one per line " + "& end with negative amount..."); for(double sal = IO.readDouble(); sal >= 0.0; sal = IO.readDouble()) { total = total + sal; count++; IO.print("The average of all salaries is $"); IO.println(total/count, ",.2"); 24 25 File I/O Using UniReader/UniWriter The type.lang package provides a simple way to input from a file (or URL) or output to a file (or URL). For input, use the UniReader class: zebra 306 % java SalaryAvg Enter salaries one per line & end with negative amount. 20.50 30.50 40.50-29.0 The average of all salaries is $30.50 1. Open the file for input by calling UniReader s constructor, e.g. UniReader myreader = new UniReader("myInputFile.txt"); The value used to indicate the end of input is called a sentinel. 2. Read in your data, e.g String name = myreader.readline(); int age = myreader.readint(); 3. Close the file, e.g. myreader.close(); 26 27

E.g. import type.lang.; public class SalaryAvgFFile { final String inputfilename = "myinputfile.txt"; UniReader myreader = new UniReader(inputFileName); double total = 0.0; int count = 0; for(double sal = myreader.readdouble();!myreader.eof(); sal = myreader.readdouble()) { total = total + sal; count++; myreader.close(); IO.print("The average of all salaries is $"); IO.println(total/count, ",.2"); Note that the methods in the UniReader class work a bit differently from those in the IO class. UniReader s readint(), readdouble(), etc. do not necessarily read a whole line. You can also check whether you have reached the end of the file by calling the eof() method. Writing to a file is similar: UniWriter mywriter = new UniWriter("myOutputFile.txt");... mywriter.println("this is to be printed.");... mywriter.close(); 28 29 Scope of Variables The scope of a variable is the part of the program where it is visible, where it can be accessed. The variables declared inside a block {... are local to the block and are only visible in the block. E.g. public class ScopeEg { int v1 = 1; for(int v2 = 2; v2 <= 4; v2++) { int v3 = 3; IO.println(v3); // ok IO.println(v2); // ok IO.println(v1); // ok IO.println(v3); // not ok! IO.println(v2); // not ok! IO.println(v1); // ok 30