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

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

COMP-202: Foundations of Programming. Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Text Input and Conditionals

COMP 202 Java in one week

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

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

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Chapter 4. Conditionals and recursion. 4.1 The modulus operator. 4.2 Conditional execution

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Basics of Java Programming

COMP-202: Foundations of Programming

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

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Eng. Mohammed S. Abdualal

Building Java Programs

Primitive data, expressions, and variables

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

Building Java Programs

Accelerating Information Technology Innovation

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Control Structures in Java if-else and switch

Primitive Data, Variables, and Expressions; Simple Conditional Execution

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

COMP 202 Java in one week

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Quiz 1: Functions and Procedures

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

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Unit 3. Operators. School of Science and Technology INTRODUCTION

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

Visual Programming. Lecture 3: Loops, Arrays. Mahmoud El-Gayyar

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

VARIABLES AND TYPES CITS1001

COMP-202: Foundations of Programming. Lecture 4: Methods Jackie Cheung, Winter 2016

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

Elementary Programming

IEEE Floating-Point Representation 1

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

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

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

Building Java Programs

CS 106 Introduction to Computer Science I

Research Group. 2: More types, Methods, Conditionals

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

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

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

Syntax and Variables

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

CS 106 Introduction to Computer Science I

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133

CS 251 Intermediate Programming Java Basics

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

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

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Object-Oriented Programming

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

Visual C# Instructor s Manual Table of Contents

Chapter 3 Selection Statements

Intro. Scheme Basics. scm> 5 5. scm>

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

COMP 250 Winter stacks Feb. 2, 2016

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

Ex: If you use a program to record sales, you will want to remember data:

Flow of Control. Chapter 3 Part 3 The Switch Statement

CS1150 Principles of Computer Science Boolean, Selection Statements

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

Logical Operators and switch

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Chapter 3. Selections

CS 112 Introduction to Programming

CS 112 Introduction to Programming

QUIZ Friends class Y;

Control structures: if

Chapter 4: Conditionals and Recursion

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

CS 231 Data Structures and Algorithms Fall Event Based Programming Lecture 06 - September 17, Prof. Zadia Codabux

Data Structure and Programming Languages

CSCI 161 Introduction to Computer Science

APCS Semester #1 Final Exam Practice Problems

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 89 / 137

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

Software Design & Programming I

Practice with variables and types

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

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

Selections. CSE 114, Computer Science 1 Stony Brook University

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming. Lecture 7: Strings and Methods Jackie Cheung, Winter 2015

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Building Java Programs

Transcription:

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

This Lecture Finish data types and order of operations Conditionals 2

Review Questions What is the difference between how we typically use the word or in English, and logical disjunction ( )? What is the order of operations for arithmetic, including casting? 3

Revised Order of Operations Note where casting happens 1. Parenthesis 2. Casting 3. *, /, % from left to right 4. +, -, from left to right Assignment happens after the evaluation of the expression. 4

+=, ++ Programmers got lazy about writing x = x + 5; So, as a shortcut, you can write x += 5; Then they got even lazier about writing x += 1; So, as a short, you can write x++; 5

Similarly: -=, *=, /=, -- x -= 5; is the same as x = x - 5; And likewise for *=, /=. Also, x--; is the same as x -= 1; or x = x - 1; 6

++, -- You can put ++ or -- before or after a variable name, and even as part of a complex expression. After: happens after the statement is executed Before: happens before the statement is executed int x = 5, y = 5; System.out.println(x++); System.out.println(++y); 7

Recommendation Only use ++ or -- by themselves, and do not put them inside other expressions. int x = 5; int y = 2 * x++ + 4; // legal, but why? 8

Constants A constant is an identifier like a variable, except that it holds one value for its entire existence. In Java, use the final keyword to declare a constant. final double PI = 3.1415; final double E = 2.71828; If you try to modify a constant CRASH! 9

Exercise Write a method, isodd, that takes one int as input, and returns true if and only if that int is odd. Write a method, areallodd, that takes three ints as inputs, and returns true if and only if all of them are odd. 10

Challenge Question Using only type casting, multiplication and division, write some code that will take a double, and then truncate it to one decimal place. e.g., 32.756 32.7 5.0 5.0 Hint: What kind of type casting have we seen in this lecture that similarly truncates part of a double? How can we exploit this for our benefit? 11

CONDITIONALS 12

Control Flow Up to this point, our programs proceed in a "straight" fashion. Just follow each line of code in a method There are commands to execute some blocks of code sometimes, and others at other times. These commands refer to the control flow because you control where the code is going. 13

Control Flow 1. if statements 2. if-else statements 3. if-else if-else statements 4. while loops 5. for loops 14

Conditionals Example: Print different things depending on what the String we are given is. String languagechoice = "English"; if (languagechoice.equals("english")) { System.out.println("You chose English."); else { System.out.println("You chose non-english."); 15

Structure of a Conditional String languagechoice = "English"; if (languagechoice.equals("english")) { else { System.out.println("You chose English."); System.out.println("You chose non-english."); The condition to check (make sure it is surrounded by parentheses) 16

Structure of a Conditional String languagechoice = "English"; if (languagechoice.equals("english")) { System.out.println("You chose English."); else { System.out.println("You chose non-english."); The first block of code gets executed if the condition evaluates to true. 17

Structure of a Conditional String languagechoice = "English"; if (languagechoice.equals("english")) { else { System.out.println("You chose English."); System.out.println("You chose non-english."); The second block of code gets evaluated if the condition evaluates to false. Or if there is no else block, no extra code block is run. 18

if if vs. if-else What is the difference? if (condition) { //some commands here if (not condition) { //more commands here if (condition) { //some commands here else { //more commands here Both blocks on the left could execute, if at the end of the first block, (not condition) somehow becomes true. 19

Going Into Two if Statements int x = 3; if (x > 0) { System.out.println("Positive. Resetting value."); x = 0; if (x <= 0) { System.out.println("Not positive"); 20

Only One Branch Executes int x = 3; if (x > 0) { System.out.println("Positive. Resetting value."); x = 0; else { System.out.println("Not positive"); 21

Exercise Write a program that asks for two Strings from the user (i.e., you have to use args in the main method). The program prints Equal! if the two Strings are the same, ignoring case. Otherwise, it doesn t print anything. 22

if-else if- else For when you have a more complicated set of conditions with more than two options. Option 1: Nested if-else statements if (option1) { else { if (option2) { else { if (option3) { 23

Better Way Option 2: All at the same level if (option1) { else if (option2) { else if (option3) { else { 24

Order Matters Much like else, the "else if" is only entered if the prior conditions were false. So, there can be a big difference if the order of the options are changed. 25

What Is Wrong Here? if (money > 0.0) { System.out.println("Positive balance"); else if (money > 1000.0) { System.out.println("You're rich!"); else { System.out.println("Uh-oh."); 26

Omitting Curly Braces A dangerous game. if (x < 0) System.out.println("x is less than zero."); System.out.println("x is definitely negative."); Remember that Java ignores the indentation, so this is very misleading! 27

Exercise Write a simple calculator program. It takes three arguments from the user. The first and third are ints. The second one is one of +, -, *, /, or %. The program prints the result of the computation. > run SimpleCalc 3 + 4 prints 7 > run SimpleCalc 3 / 4 prints 0 > run SimpleCalc 30 % 4 prints 2 28