Learning Outcomes for this week. Inf1-OP. A Foundation for Programming. A Foundation for Programming

Similar documents
Tutorials. Inf1-OP. Learning Outcomes for this week. A Foundation for Programming. Conditionals and Loops 1

1.3 Conditionals and Loops

CS 112 Introduction to Programming

1.3 Conditionals and Loops

1.3 Conditionals and Loops

1.3 Conditionals and Loops. 1.3 Conditionals and Loops. Conditionals and Loops

1.3 Conditionals and Loops

1.3 Conditionals and Loops

! Sequence of statements that are actually executed in a program. ! Conditionals and loops: enable us to choreograph control flow.

3. Conditionals & Loops

3. Conditionals and loops

3. Conditionals & Loops

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

Inf1-OP. Arrays 1. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. January 30, School of Informatics

Arrays. Inf1-OP. Arrays. Many Variables of the Same Type. Arrays 1. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein

Inf1-OP. Arrays 1. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

Condi(onals and Loops

Lecture 3: Loops. While Loops. While Loops: Powers of Two. While Loops: Newton-Raphson Method

2.3 Flow Control. Flow-Of-Control. If-Else: Leap Year. If-Else

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

Conditionals, Loops, and Style

Lecture 3: Loops. While Loops. While Loops: Newton-Raphson Method. While Loops: Powers of Two

Conditionals, Loops, and Style. Control flow thus far. if statement. Control flow. Common branching statement Evaluate a boolean expression

Inf1-OOP. Arrays. Many Variables of the Same Type. Arrays Introduction to Arrays Arrays in Java Shuffling a Deck Multidimensional Arrays Summary/Admin

Inf1-OOP. Textbooks. Who and What. Organizational Issues. Why Java? Course Overview. Hello, World! in Java. Ewan Klein, Perdita Stevens

Inf1-OOP. Arrays 1. Perdita Stevens, adapting earlier version by Ewan Klein. January 11, School of Informatics

Inf1-OOP. Textbooks. Who and What. Organisational issues. Why Java? Course Overview. Hello, World! in Java

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

Inf1-OOP. OOP Exam Review. Perdita Stevens, adapting earlier version by Ewan Klein. March 16, School of Informatics

CONDITIONAL EXECUTION

CONDITIONAL EXECUTION

CMPT 125: Lecture 4 Conditionals and Loops

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

Who and what can help? Inf1-OP. Lecturer: Timothy Hospedales TA: Natalia Zon

Inf1-OP. Course Overview. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

AP CS Unit 3: Control Structures Notes

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1.

Variables and data types

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

Introduction to Computer Science Unit 2. Notes

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

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

CS111: PROGRAMMING LANGUAGE II

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

Inf1-OOP. Data Types. A Foundation for Programming. type value set operations. Overview. Using Data Types 1. Image Processing

CSC 1051 Data Structures and Algorithms I

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

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Mr. Monroe s Guide to Mastering Java Syntax

CS111: PROGRAMMING LANGUAGE II

Inf1-OP. Collections. Perdita Stevens, adapting earlier version by Ewan Klein. January 9, School of Informatics

Inf1-OP. Inf1-OP Exam Review. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 16, School of Informatics

Java Coding 3. Over & over again!

Quiz 1: Functions and Procedures

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

CSEN 202 Introduction to Computer Programming

COMP 202 Java in one week

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

IT 1033: Fundamentals of Programming Loops

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

COMP 202. Java in one week

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

CS11 Java. Fall Lecture 1

(Refer Slide Time: 00:26)

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

A Foundation for Programming

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

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

Introduction to Computer Science Unit 2. Notes

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

Rigidity of arrays. Inf1-OP. ArrayList. ArrayList: Methods. Declaration. Collections

CSC 1051 Data Structures and Algorithms I

C++ Reference NYU Digital Electronics Lab Fall 2016

COMP6700/2140 Control Flow

Module 4: Decision-making and forming loops

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

Control Structures in Java if-else and switch

Scope of this lecture. Repetition For loops While loops

Controls Structure for Repetition

204111: Computer and Programming

Lecture 6: While Loops and the Math Class

Loops. CSE 114, Computer Science 1 Stony Brook University

Administrative Stuff. Inf1-OP. Additional help? Who to contact for help? Course Overview

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

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

Introduction to Software Development (ISD) Week 3

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013

Program Control Flow

Program Control Flow

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

CS 106 Introduction to Computer Science I

Object Oriented Programming with Java

CS 106 Introduction to Computer Science I

COMP 111. Introduction to Computer Science and Object-Oriented Programming

AP Computer Science A Summer Assignment 2017

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

CSC 1051 Data Structures and Algorithms I

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

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

Transcription:

Learning Outcomes for this week Inf1-OP Conditionals and Loops 1 Perdita Stevens, adapting earlier version by Ewan Klein School of Informatics January 9, 2016 Use if and if-else statements to execute a sequence of statements based on the truth value of Boolean expressions. Use nested if-else statements to compute results based on a number of mutually exclusive alternatives. Use while-loops to repeatedly execute a sequence of statements based on the truth value of Boolean expressions. Use for-loops to repeatedly execute a sequence of statements based on an initialization statement, a Boolean test, and an increment statement. Use for-loops to compute finite sums and finite products. 1 Thanks to Sedgewick&Wayne for much of this content A Foundation for Programming A Foundation for Programming objects functions and modules graphics, sound and image I/O arrays conditionals and loops objects functions and modules graphics, sound and image I/O arrays conditionals and loops Math text I/O Math text I/O primitive data types assignment statements primitive data types assignment statements

A Foundation for Programming Control Flow Control flow: A sequence of statements that are actually executed in a program Conditionals and loops enable us to choreograph control flow objects functions and modules statement 1 boolean 1 graphics, sound and image I/O arrays statement 2 conditionals and loops Math text I/O primitive data types assignment statements statement 3 statement 1 boolean 2 statement 2 statement 4 statement 3 If Statement If / conditional statement: Evaluate a boolean expression E. If value of E is, execute some statements. If value of E is, execute some other statements this is the else part of a conditional statement. If Statement If / conditional statement sometimes called branching structures: if (boolean expression) { statement T; else { statement F; can be any sequence of statements boolean expression boolean expression statement T statement F if (x > y) { int t = x; x = y; y = t; sequence of statements

If Statement If Statement: Examples If / conditional statement: Evaluate a boolean expression. If, execute some statements. If, execute some other statements. if (x < 0) x = -x; if (x > y) max = x; else max = y; absolute value put x and y into ascending order if (x < 0) x = -x; if (x > y) { int temp = x; x = y; y = temp; x < 0? x > y? maximum of x and y if (x > y) max = x; else max = y; x = -x; max = x; max = y; error check for division operation if (den == 0) { System.out.println("Division by zero"); else { System.out.println("Quotient = " + num / den); While Loop The while loop is a structure for expressing repetition. Evaluate a boolean expression. If, execute some statements. loop continuation condition Repeat. while (boolean expression) { statement 1; statement 2; loop body boolean expression statement 2 statement 1 While Loop: Powers of Two Print powers of 2 that are 2 n for some n. Set n = 6. Increment loop counter i by 1, from 0 to n. Double val each time. while (i <= n) { i = i + 1; i val i n Output 0 1 0 1 1 2 1 2 2 4 2 4 3 8 3 8 4 16 4 16 5 32 5 32 6 64 6 64 7 128 Start Again

Powers of Two public class PowersOfTwo { public static void main(string[] args) { int n = Integer.parseInt(args[0]); while (i <= n) { i = i + 1; % java PowersOfTwo 3 0 1 1 2 2 4 3 8 While Loop Challenge Q: Is anything wrong with the following version of PowersOfTwo? while (i <= n) i = i + 1; A: Need curly braces around statements in while loop. Otherwise, only the first of the statements is executed before returning to while condition; enters an infinite loop, printing 0 1 for ever. (How to stop an infinite loop? At the Linux command-line, hit Control-c.) The Increment Operator while (i <= n) { i = i + 1; standard assignment: i = i + 1; semantically equivalent shorthand: i++; while (i <= n) { i++; For Loop The for loop is another common structure for repeating things. Execute initialization statement. Evaluate a boolean expression. If, execute some statements. Then execute the increment statement. Repeat. loop continuation condition for (init; boolean expression; increment) { statement 1; statement 2; loop body init boolean expression increment statement 2 statement 1

Subdivisions of a Ruler Output % java Ruler 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 Subdivisions of a Ruler the hard way Output public class Ruler { public static void main(string[] args) { String ruler1 = "1"; String ruler2 = ruler1 + " 2 " + ruler1; String ruler3 = ruler2 + " 3 " + ruler2; String ruler4 = ruler3 + " 4 " + ruler3; System.out.println(ruler1); System.out.println(ruler2); System.out.println(ruler3); System.out.println(ruler4); % java Ruler 1 1 2 1 1 2 1 3 1 2 1 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 Subdivisions of a Ruler with for loop Subdivisions of a Ruler with for loop Output Initialize ruler to " " (empty string). For each value i from 1 to n: sandwich two copies of ruler on either side of i. public class RulerN { public static void main(string[] args) { int n = Integer.parseInt(args[0]); String ruler = " "; for (int i = 1; i <= n; i++) { ruler = ruler + i + ruler; System.out.println(ruler); i 1 2 3 ruler " " " 1 " " 1 2 1 " " 1 2 1 3 1 2 1 " % java Ruler 1 1 % java Ruler 2 1 2 1 % java Ruler 3 1 2 1 3 1 2 1 % java Ruler 4 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 % java Ruler 100 Exception in thread "main" java.lang.outofmemoryerror

Loop Examples 1 Loop Examples 2 Print largest power of two that is n while (val <= n / 2) { System.out.println(val); Print the result of computing the finite sum (1 + 2 +... + n) int sum = 0; for (int i = 1; i <= n; i++) { sum += i; Loop Examples 3 Print the result of computing the finite product (n! = 1 2... n) int product = 1; for (int i = 1; i <= n; i++) { product *= i;

Nested If Statements How to classify Scottish weather: degrees C < -5-5 to 0 1 to 10 > 10 verdict wear a sweater nippy normal roastin' String verdict; if (temp < -5) verdict = "wear a sweater"; else { if (temp < 1) verdict = "nippy"; else { if (temp < 11) verdict = "normal"; else verdict = "roastin'"; 4 mutually exclusive alternatives Nested If Statements We don t necessarily need all those braces. Output public class ScottishWeather { public static void main(string[] args) { String verdict; int temp = Integer.parseInt(args[0]); if (temp < -5) verdict = "wear a sweater"; else if (temp < 1) verdict = "nippy"; else if (temp < 11) verdict = "normal"; else verdict = "roastin'"; System.out.println("Verdict: " + verdict); % java ScottishWeather -1 Verdict: nippy % java ScottishWeather 1 Verdict: normal Nested If Statements Is there anything wrong with the logic of the following code? degrees C < -5-5 to 0 1 to 10 > 10 verdict wear a sweater nippy normal roastin' String verdict; int temp = Integer.parseInt(args[0]); if (temp < -5) verdict = "wear a sweater"; if (temp < 1) verdict = "nippy"; if (temp < 11) verdict = "normal"; if (temp >= 11) verdict = "roastin'"; 4 mutually exclusive alternatives Summary Control flow: Sequence of statements that are actually executed in a program run. Conditionals and loops: enable us to choreograph the control flow. Control Flow Description Examples straight-line programs conditionals loops all statements are executed in the order given certain statements are executed depending on the values of certain variables certain statements are executed repeatedly until certain conditions are met if, if-else while, for

Tutorials This Week s Reading Start this week please let the ITO know if you need to switch tutorial groups. Labs continue this week and every week (except ILW). Java Tutorial pp68-86, i.e. Chapter 3 Language Basics from Expressions, Statements and Blocks to the end of the chapter.