Making Decisions. Agenda

Similar documents
Values, Variables, Types & Arithmetic Expressions. Agenda

Introduction to Programming

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

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

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

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

Control Structures in Java if-else and switch

Decision Making in C

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

Introduction to Object-Oriented Programming

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

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

Building Java Programs

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso

Control Structures in Java if-else and switch

Building Java Programs

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

Flow Control. CSC215 Lecture

CS1150 Principles of Computer Science Boolean, Selection Statements

Introduction to Programming Using Java (98-388)

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

COMP-202: Foundations of Programming. Lecture 6: Conditionals Jackie Cheung, Winter 2016

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

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

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Lecture 5 Tao Wang 1

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

Computer Science & Engineering 150A Problem Solving Using Computers

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

LECTURE 5 Control Structures Part 2

Building Java Programs

Accelerating Information Technology Innovation

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

Topic 4 Expressions and variables

boolean & if-then-else

Lesson 7 Part 2 Flags

Other conditional and loop constructs. Fundamentals of Computer Science Keith Vertanen

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

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

In this chapter, you will:

CS 106 Introduction to Computer Science I

LECTURE 04 MAKING DECISIONS

CS 102 / CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2010

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

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.

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

CS111: PROGRAMMING LANGUAGE II

CSCE 206: Structured Programming in C++

Lecture 3. The syntax for accessing a struct member is

Do not turn over until you are told to do so by the Invigilator.

Language Reference Manual

Announcements. 1. Forms to return today after class:

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

Chapter 4: Making Decisions

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 4: Making Decisions

CS349/SE382 A1 C Programming Tutorial

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

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

Java is an objet-oriented programming language providing features that support

Introduction to Software Development (ISD) Week 3

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

logical operators and else-if statements

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

VARIABLES AND TYPES CITS1001

Decision Making -Branching. Class Incharge: S. Sasirekha

Topic 5: Enumerated Types and Switch Statements

Selection Structures II

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Flow of Control. Chapter 3

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

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Datatypes, Variables, and Operations

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

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

Quick Reference Guide

CS 106 Introduction to Computer Science I

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

CS302: Self Check Quiz 2

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

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

CSC 1300 Exam 4 Comprehensive-ish and Structs

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

CSCI 2101 Java Style Guide

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

Decision Structures. Lesson 03 MIT 11053, Fundamentals of Programming

DECISION MAKING STATEMENTS

CS110D: PROGRAMMING LANGUAGE I

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

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

CSc Introduction to Computing

Conditional Statement

Control Statements Review CSC 123 Fall 2018 Howard Rosenthal

Lecture 8. Daily Puzzle

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Transcription:

Making Decisions Lecture 5 - B Object-Oriented Programming Agenda Blocks Statements Selection Statement If-else Statement Nested if / if-else Statements Switch Statement Relational Operators Logical Operators Common Errors Lecture 5 - B Object-Oriented Programming 2 1

Blocks (1) Question Why we use curly braces in our classes, methods, etc? Answer Curly braces is the simplest type of structure a program can provide. It allows the compiler to do memory management of the executing program. Anything declared inside a pair of curly braces is a selfcontained entity. Lecture 5 - B Object-Oriented Programming 3 Blocks (2) int temp; temp * 2; System.out.println(temp) System.out.println(temp) The statements inside the curly braces is a self-contained code. temp will be removed from the memory at the end of the ending brace. The last line will give an error because no temp variable is available at this place in the memory. Blocks are used with classes, methods and many other constructs. Some of which we are going to study today. Lecture 5 - B Object-Oriented Programming 4 2

Statements So far we have studied code that execute only once. This code also executes in a sequence. The execution of a program trace is called its flow-of-control. What if we need some statements to execute depending on one condition and others depending on another statement. Statement 1 Statement 2 Statement 3 Statement 4 Lecture 5 - B Object-Oriented Programming 5 Selection Statement Choice between different actions during execution Choice is based on a criterion specified during the selection statement criteria : boolean condition (if, if-else) Choice: if it rains I will go shopping (if) if it rains I will go shopping, otherwise I will go to the park (if-else) criteria : a specific value among a set of values (switch) Example: On Monday, I do my shopping On Tuesday, I work On Wednesday, I go to the library On Thursday, I go to the swimming pool Etc. Lecture 5 - B Object-Oriented Programming 6 3

Simple if Statement if ( <conditional expression> ) <statement> if ( <conditional expression> ) <statement_1> <statement_2> <statement_n> Lecture 5 - B Object-Oriented Programming 7 Simple if statement Lecture 5 - B Object-Oriented Programming 8 4

Simple if Statement if ( <conditional expression> ) <statement> public void asimplecondition(double price, double money) if (money >= price) System.out.println("Payment accepted"); Condition must evaluate to true End of if statement Lecture 5 - B Object-Oriented Programming 9 Simple if Statement if ( <conditional expression> ) <statement_1> <statement_2> <statement_n> Lecture 5 - B Object-Oriented Programming 10 5

Simple if Statement public void asimplecondition2 (double price, double money) double change; if (money >= price) System.out.println("Payment accepted"); change = money - price; System.out.println("Your change is " + change); End of if statement Lecture 5 - B Object-Oriented Programming 11 Simple if Statement On the importance of for if statement public void asimplecondition3 (double price, double money) double change; if (money >= price) System.out.println("Payment accepted"); change = money - price; System.out.println("Your change is " + change); Lecture 5 - B Object-Oriented Programming 12 6

Simple if Statement On the importance of for if statement public void asimplecondition3 (double price, double money) double change; End of if statement if (money >= price) System.out.println("Payment accepted"); change = money - price; System.out.println("Your change is " + change); Lecture 5 - B Object-Oriented Programming 13 if-else Statement if ( <conditional expression> ) <statement_1> else <statement_2> Lecture 5 - B Object-Oriented Programming 14 7

if-else Statement if ( <conditional expression> ) <statement_1> <statement_n> else <statement_o> <statement_z> Lecture 5 - B Object-Oriented Programming 15 if-else Statement Lecture 5 - B Object-Oriented Programming 16 8

if-else Statement public void asimpleconditionwithelse(double price, double money) if (money >= price) System.out.println("Payment accepted"); else System.out.println("Payment not accepted"); Condition evaluates to true Condition evaluates to false Lecture 5 - B Object-Oriented Programming 17 if-else Statement public void asimpleconditionwithelse2(double price, double money) if (money >= price) double change; System.out.println("Payment accepted"); change = money - price; System.out.println("Your change is " + change); else double missingmoney; missingmoney = price - money; System.out.println("Payment not accepted"); System.out.println("Please complete with " + missingmoney); Lecture 5 - B Object-Oriented Programming 18 9

Nested if /if-else Statements public void nesteifa(int x, int y) if ( x > 0 ) if (y > 0) System.out.println("Both x and y are positive"); else System.out.println("x is negative, y may be positive or negative"); End of second if statement Lecture 5 - B Object-Oriented Programming 19 Nested if /if-else Statements public void nesteifc (int x, int y) if ( x > 0 ) if (y > 0) System.out.println("Both x and y are positive"); else System.out.println("x is negative, y may be positive or negative"); Be careful with the use of Changes the tested values Lecture 5 - B Object-Oriented Programming 20 10

Nested if /if-else Statements public void nesteifb(int x, int y) if ( x > 0 ) if (y > 0) System.out.println("Both x and y are positive"); else System.out.println("x is positive and y is negative"); Be careful to the use of Changes the tested values Lecture 5 - B Object-Oriented Programming 21 Nested if /if-else Statements public void nestedif2 (int testscore) char grade; if (testscore >= 90) grade = 'A'; else if (testscore >= 80) grade = 'B'; else if (testscore >= 70) grade = 'C'; else if (testscore >= 60) grade = 'D'; else grade = 'F'; System.out.println("Grade = " + grade); Lecture 5 - B Object-Oriented Programming 22 11

switch Statement switch ( <integral expression> ) case label_1: <statement_1> case label_2: <statement_2> case label_n: <statement_n> default: <statement> Evaluated First Value of integral expr compared with first case label Value of integral expr compared with second case label Continues until the end unless break Lecture 5 - B Object-Oriented Programming 23 switch Statement switch ( <integral expression> ) case label_1: <statement_1> case label_2: <statement_2> case label_n: <statement_n> default: <statement> char byte short int (enumerated type) Lecture 5 - B Object-Oriented Programming 24 12

switch Statement public void switchexample(char digit) switch (digit) case '1': System.out.println("First "); break; case '2': System.out.println("Second ");break; case '3': System.out.println("Third ");break; case '4': System.out.println("Fourth ");break; case '5': System.out.println("Fifth ");break; default: System.out.println("Other "); Lecture 5 - B Object-Oriented Programming 25 switch Statement public void switchexample2(int digit) switch (digit) case 1: System.out.println("First "); case 2: System.out.println("Second "); case 3: System.out.println("Third "); case 4: System.out.println("Fourth "); case 5: System.out.println("Fifth "); default: System.out.println("Other "); If no break, all statements after condition are satisfied until the end of the switch statement Lecture 5 - B Object-Oriented Programming 26 13

Relational Operators Numerical expressions (variables, constants, literal constants, etc ) can be compared using these operators. They return a boolean value: either true or false Note that == is used to test for equality; don t confuse it with = of assignment! boolean iseven = ((x % 2) == 0); Lecture 5 - B Object-Oriented Programming 27 Logical Operators And (&&) takes two boolean expressions and returns true only if both expressions are true Or ( ) takes two boolean expressions and returns true if at least one is true Not (!) takes one boolean expression and returns its negation Examples: boolean bool1 = (3 == 2) && (2 < 3); // false boolean bool2 = (!bool1) (5.6 >= 8); // true boolean bool3 =!(bool1 && bool2); // true Lecture 5 - B Object-Oriented Programming 28 14

Common Errors Compound Conditional Error value >= 0 value <= 10 This expression is a tautology. A tautology is a boolean expression that is always true. Contriving Compound Boolean Expression If (value < 0 && value > 10) //some code This condition can never be true. A number cannot be both be less than zero and greater than ten. Lecture 5 - B Object-Oriented Programming 29 Readings Book Name: Big Java Author: Cay Horstmann Content: Chapter # 5 Lecture 5 - B Object-Oriented Programming 30 15

Acknowledgements While preparing this course I have greatly benefited from the material developed by the following people: Andy Van Dam (Brown University) Mark Sheldon (Wellesley College) Robert Sedgewick and Kevin Wayne (Princeton University) Mark Guzdial and Barbara Ericsson (Georgia Tech) Richard Halterman (Southern Adventist University) Lecture 5 - B Object-Oriented Programming 31 16