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

Similar documents
Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

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

Control Structures in Java if-else and switch

CS111: PROGRAMMING LANGUAGE II

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

AP CS Unit 3: Control Structures Notes

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

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

CS111: PROGRAMMING LANGUAGE II

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

Control Structures in Java if-else and switch

Following is the general form of a typical decision making structure found in most of the programming languages:

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

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Lecture 5 Tao Wang 1

Decision Structures. Lesson 03 MIT 11053, Fundamentals of Programming

AP Computer Science A Summer Assignment 2017

Topics. Chapter 5. Equality Operators

Lecture 4. CS118 Term planner. The simple if statement. Control flow: selection. The if... else statement cont... The if... else statement.

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Introduction to Computer Science Unit 2. Notes

Warmup : Name that tune!

CS 112 Introduction to Programming

CS 112 Introduction to Programming

( &% class MyClass { }

SELECTION. (Chapter 2)

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

CMPT 125: Lecture 4 Conditionals and Loops

Chapter 3. Selections

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

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CONCORDIA UNIVERSITY Summer 2005 Comp 248 /1 Section AA Introduction to Programming Final Examination/A

Lab 8: IF statement. Conditionals and Loops. Copyright 2012 Pearson Education, Inc.

Repetition, Looping. While Loop

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

BRANCHING if-else statements

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Combo Lecture Lecture 14/15. Instructor: Craig Duckett

Lecture 5: Methods CS2301

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

Supplementary Test 1

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Lecture 14. 'for' loops and Arrays

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

Getting started with Java

CS 106 Introduction to Computer Science I

Scope of this lecture. Repetition For loops While loops

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

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

CONDITIONAL EXECUTION

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

Algorithms and Conditionals

CSEN 202 Introduction to Computer Programming

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

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

CONDITIONAL EXECUTION

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

Practice with variables and types

PROGRAMMING FUNDAMENTALS

Practice with variables and types

Course Outline. Introduction to java

Exercise 4: Loops, Arrays and Files

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

8. Decision-Making Statements. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Give one example where you might wish to use a three dimensional array

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

The design recipe. Readings: HtDP, sections 1-5. (ordering of topics is different in lectures, different examples will be used)

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

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

Introduction to Computer Science Unit 2. Notes

CSE 142, Summer 2015

Building Java Programs

STUDENT LESSON A12 Iterations

CS 101 Spring 2007 Midterm 2 Name: ID:

Lists, loops and decisions

CS1004: Intro to CS in Java, Spring 2005

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

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

Chapter 3. Iteration

Chapter 3 Selection Statements

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

CONDITIONAL EXECUTION: PART 2

MEHMET YAYAN - İSMAİL HAKKI ÖZTÜRK CS101/SEC.-2 CLASS NOTES 1. March 28-30, 2007

COMP 202 Java in one week

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

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

CPSC 219 Extra review and solutions

Handout 4 Conditionals. Boolean Expressions.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Logical Operators and switch

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Conditionals, Loops, and Style

ECE 122 Engineering Problem Solving with Java

Transcription:

Administration Conditional Statements CS 99 Summer 2000 Michael Clarkson Lecture 4 Lab 2 due now on floppy Lab 3 due tomorrow via FTP need Instruct account password Lab 4 posted this afternoon Prelim 1 in 1 week review session TBA 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 2 Agenda ifstatements switch statements?:(the conditional operator) if The if (also if-then) statement chooses whether to run code, or not: if (grade > 60) { System.out.println( You are passing! ); 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 3 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 4 Flow of control Syntax condition? true block false if (condition) { Example: if (temperature > 80) { System.out.println( It s hot! ); 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 5 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 6 1

Syntax [2] The condition of an if statement can be any Boolean expression There may be any number of statements in the body of the if What isn t allowed in the body? Nested ifs A nested statement is a statement inside another, e.g.: if (month == 12) { if (day == 31) { System.out.println( It s New Year s Eve ); How else could this be written? 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 7 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 8 or else! It s also possible to make an if statement choose between two blocks of code. if (condition) { else { if-else Semantics (meaning): evaluate the condition if the condition is true, execute the first block (the then clause) if the condition is false, execute the second block (the else clause) no matter which block was executed, resume executing with the code after both blocks 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 9 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 10 if-else [2] if-else [3] Example: print the larger of two numbers true then clause condition? false else clause if (num1 > num2) { System.out.println(num1); else { System.out.println(num2); How would this look in a larger program? 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 11 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 12 2

class LargerOfTwo { public static void main(string[] args) { int num1 = Console.readInt(); int num2 = Console.readInt(); if (num1 > num2) { System.out.println(num1); else { System.out.println(num2); System.out.println( All done ); 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 13 Robust Programs A robust program is completely protected against all possible crashes from bad data and unexpected values. Example: System.out.print( Enter a positive number: ); p = Console.readInt(); if (p <= 0) { System.out.println( You entered a nonpositive number ); else { // do whatever we expected to do, e.g., Math.sqrt(p) 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 14 Robust Programs [2] A Complicated if This kind of protection can be used many places in a program Requires extra code (and time!) at the tradeoff of fewer possible crashes Essential for professionals For students, can detract from programming principles In this class, you can assume we will always give your program valid input unless we specifically warn you otherwise 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 15 How could we express the following in Java? If a student s grade is: 90-100, print A 80-89, print B 70-79, print C 60-69, print D 0-59, print F 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 16 A Complicated if [2] Extended if An extended if expresses a multiway decision a choice of one of several alternatives Allows problems that would require deeply nested ifs to be expressed more concisely if (grade >= 90) { System.out.println( A ); else { if (grade >= 80) { System.out.println( B ); else { if (grade >= 70) { System.out.println( C ); else { if (grade >= 60) { System.out.println( D ); else { System.out.println( F ); 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 17 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 18 3

Extended if [2] A Less Complicated if if (condition1) { // block1 else if (condition2) { // block2 else if (conditionn) { // blockn else { // else clause Semantics: Pick the first true condition Execute its block only If no conditions are true, execute the else clause if (grade >= 90) { System.out.println( A ); else if (grade >= 80) { System.out.println( B ); else if (grade >= 70) { System.out.println( C ); else if (grade >= 60) { System.out.println( D ); else { System.out.println( F ); 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 19 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 20 Braces Optional If there s only one statement in the body of an if, the braces are optional: if (temperature < 0) System.out.println( It s c-c-cold! ); Strong recommendation: put them in anyway! fewer bugs don t have to add them later avoid the dangling-else problem Dangling Else Problem // test for a perfect square if (num > 0) if (num == Math.pow(Math.sqrt(num), 2)) System.out.println(num); else System.out.println( Non-positive number ); If num is 20, this prints Non-positive number. Why? 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 21 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 22 Dangling Else [2] switch statements if (num > 0) { if (num == Math.pow(Math.sqrt(num), 2)) { System.out.println(num); else { System.out.println( Non-positive number ); Similar to extended ifs Evaluates a single expression, then chooses one of many paths based on that value Could always be replaced by a nested if, but is sometimes simpler and easier to read No ambiguity when fully braced. 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 23 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 24 4

switch Example switch Syntax switch (age) { case 18: System.out.println( Legal voting age ); case 21: System.out.println( Legal drinking age ); case 25: System.out.println( Able to rent a car ); default: System.out.println( Not an interesting age ); 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 25 switch (integer expression) { case constant expression: //statements [] [more cases] [default: // default clause] 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 26 switch Example [2] Conditional Operator switch (num) { case 1: case 2: case 3: case 4: System.out.println( Less than 5 ); case 5: case 6: case 7: case 8: case 9: System.out.println( Less than 10 ); Java s only ternary operator?: boolean-expression? expression : else-expression Semantics: Evaluate the boolean-expression If true, use expression as the value of the operation If false, use else-expression as the value of the operation 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 27 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 28 Conditional Operator [2] Acts as an abbreviation for if-else Except that it has a value has operands that are expressions, not full statements is not as easily readable You should avoid this operator unless you have a good reason to use it?: Examples max = (num1 > num2)? num1 : num2; abs = (num1 > 0)? num1 : -num1; 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 29 7/5/00 CS 99 Summer 2000 Michael Clarkson Lecture 4 30 5