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

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

1.3 Conditionals and Loops

1.3 Conditionals and Loops

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

CS 112 Introduction to Programming

1.3 Conditionals and Loops

1.3 Conditionals and Loops

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

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

1.3 Conditionals and Loops

Condi(onals and Loops

Conditionals, Loops, and Style

3. Conditionals & Loops

3. Conditionals and loops

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

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

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

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

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

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

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

3. Conditionals & Loops

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

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

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. OOP Exam Review. Perdita Stevens, adapting earlier version by Ewan Klein. March 16, School of Informatics

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

CONDITIONAL EXECUTION

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

AP CS Unit 3: Control Structures Notes

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

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

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

CONDITIONAL EXECUTION

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

CS111: PROGRAMMING LANGUAGE II

CMPT 125: Lecture 4 Conditionals and Loops

CS111: PROGRAMMING LANGUAGE II

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

A Foundation for Programming

Quiz 1: Functions and Procedures

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

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

Introduction to Computer Science Unit 2. Notes

CSEN 202 Introduction to Computer Programming

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

COMP 202 Java in one week

(Refer Slide Time: 00:26)

Inf1-OP. Functions aka Static Methods. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. 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

COMP 202. Java in one week

Mr. Monroe s Guide to Mastering Java Syntax

Loops. CSE 114, Computer Science 1 Stony Brook University

Java Coding 3. Over & over again!

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Module 4: Decision-making and forming loops

Variables and data types

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

IUSE Knowledge Test. 1. Demographic Questions. IUSE Knowledge Test. 2. Computational Thinking Knowledge Test

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

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

Introduction. C provides two styles of flow control:

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

Introduction to Software Development (ISD) Week 3

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

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

Functions / Static Methods. Why are functions so helpful? Inf1-OP. Find Nearest Neighbour to a Central Point

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

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Class Foo. instance variables. instance methods. Class FooTester. main {

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

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

CT 229 Methods Continued

Control Structures in Java if-else and switch

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

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

Admin. CS 112 Introduction to Programming. Recap: Exceptions. Summary: for loop. Recap: CaesarFile using Loop. Summary: Flow Control Statements

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

CS 112 Introduction to Programming

C++ Reference NYU Digital Electronics Lab Fall 2016

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

Introduction to Computer Science Unit 2. Notes

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

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Functions / Static Methods. Inf1-OP. What s this static method business? Functions / Static Methods. Haskell function: type declaration

CSC 1051 Data Structures and Algorithms I

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

CS 106 Introduction to Computer Science I

CS11 Java. Fall Lecture 1

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

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

CSC 1051 Data Structures and Algorithms I

Chapter 6 Arrays. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

CS1150 Principles of Computer Science Loops (Part II)

Debugging [continued]

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

APCS Semester #1 Final Exam Practice Problems

Transcription:

Tutorials Inf1-OP Conditionals and Loops 1 Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein Start this week please let the ITO know if you need to switch tutorial groups. Labs continue this week and every week (except FLW). School of Informatics February 26, 2018 1 Thanks to Sedgewick&Wayne for much of this content Learning Outcomes for this week A Foundation for Programming 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. Use Eclipse to write, compile, execute and test Java code objects functions and modules graphics, sound and image I/O arrays conditionals and loops Math text I/O primitive data types assignment statements

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 Control Flow Conditional Statements Control flow: A sequence of statements that are actually executed in a program Conditionals and loops enable us to choreograph control flow statement 1 boolean 1 statement 2 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 (swap) 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 Loops (While) Repeat. while (boolean expression) { statement 1; statement 2; loop body statement 2 boolean expression 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; Start Again 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 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. init increment Loops (For) Then execute the increment statement. Repeat. loop continuation condition boolean expression statement 2 statement 1 for (init; boolean expression; increment) { statement 1; statement 2; loop body

Anatomy of a For Loop For Loop: Powers of Two initialize another variable in a separate statement for ( int i = 0 ; i <= N ; i++ ) { declare and initialize a loop control variable loop continuation condition loop body increment loop variable Print the first n powers of 2. Set n = 6. Double val each time. for ( i <= n; i++) { Start Again val i i n Output 1 0 0 1 2 1 1 2 4 2 2 4 8 3 3 8 16 4 4 16 32 5 5 32 64 6 6 64 128 7 When to use While and when to use For? Rule of thumb Loop Examples 1 Print largest power of two that is n know iteration count while (val <= n / 2) { System.out.println(val); Use For Use While

Loop Examples 2 Loop Examples 3 Print the result of computing the finite sum (1 + 2 +... + n) int sum = 0; for (int i = 1; i <= n; i++) { sum += i; 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 Conditionals

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

This Week s Reading Java Tutorial pp68-86, i.e. Chapter 3 Language Basics from Expressions, Statements and Blocks to the end of the chapter.