CS Programming I: Branches

Similar documents
CS Programming I: Branches

CS Programming I: Arrays

CS Programming I: Primitives and Expressions

CS Programming I: Using Objects

CS Programming I: Using Objects

CS Programming I: ArrayList

CS Programming I: Programming Process

CONDITIONAL EXECUTION: PART 2

CS Programming I: Programming Process

CS Programming I: Programming Process

CS Programming I: Exceptions

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

CS Programming I: Inheritance

CS Programming I: Exceptions

Example. Section: PS 709 Examples of Calculations of Reduced Hours of Work Last Revised: February 2017 Last Reviewed: February 2017 Next Review:

CT 229. Java Syntax 26/09/2006 CT229

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

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

CS Programming I: File Input / Output

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

JAVA OPERATORS GENERAL

Chapter 6. More Complex Conditionals. 6.1 Nested Conditionals

Do not turn to the next page until the start of the exam.

CSC Java Programming, Fall Java Data Types and Control Constructs

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

Read and fill in this page now. Your instructional login (e.g., cs3-ab): Your lab section days and time: Name of the person sitting to your left:

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

CS 253. January 14, 2017

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

CS Programming I: Classes

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

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CIS 110: Introduction to Computer Programming

Computer Grade 5. Unit: 1, 2 & 3 Total Periods 38 Lab 10 Months: April and May

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

CS Programming I: File Input / Output

Read and fill in this page now

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

SECTION II: LANGUAGE BASICS

Read and fill in this page now. Your lab section day and time: Name of the person sitting to your left: Name of the person sitting to your right:

Tutorial 8 (Array I)

Difference Between Dates Case Study 2002 M. J. Clancy and M. C. Linn

Lexical Considerations

CSC 1214: Object-Oriented Programming

Arrays. What if you have a 1000 line file? Arrays

The PHP language. Teaching you everything about PHP? Not exactly Goal: teach you how to interact with a database via web

Control Structures in Java if-else and switch

Chapter 4: Conditionals and Loops

Logical and Bitwise Expressions

Sequential Search (Searching Supplement: 1-2)

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

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

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Control Structures in Java if-else and switch

Logical and Bitwise Expressions

Java Basic Programming Constructs

COMP6700/2140 Operators, Expressions, Statements

Computer Components. Software{ User Programs. Operating System. Hardware

Marketing Opportunities

Topics. Chapter 5. Equality Operators

Programming Logic and Design Sixth Edition

CT 229 Java Syntax Continued

Conditionals and Loops

Year 10 OCR GCSE Computer Science (9-1)

Stat 428 Autumn 2006 Homework 2 Solutions

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

Chapter 3: Operators, Expressions and Type Conversion

Arrays. Arrays (8.1) Arrays. One variable that can store a group of values of the same type. Storing a number of related values.

CHIROPRACTIC MARKETING CENTER

Chapter 4 - Notes Control Structures I (Selection)

CS Week 5. Jim Williams, PhD

Getting started with Java

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Decision Structures. Selection. Selection options (in Java) Plain if s (3 variations) Each action could be any of: if else (3 variations)

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CHAPTER 5 FLOW OF CONTROL

Lesson 38: Conditionals #2 (W11D3)

CS111: PROGRAMMING LANGUAGE II

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Condi(onals and Loops

Chapter 6 Primitive types

09/08/2017 CS2530 INTERMEDIATE COMPUTING 9/8/2017 FALL 2017 MICHAEL J. HOLMES UNIVERSITY OF NORTHERN IOWA TODAY S TOPIC: Exceptions and enumerations.

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Lecture 02 C FUNDAMENTALS

13 th Windsor Regional Secondary School Computer Programming Competition

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CS3: Introduction to Symbolic Programming. Lecture 8: Introduction to Higher Order Functions. Spring 2008 Nate Titterton

Operators. Java operators are classified into three categories:

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

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

A Beginner s Guide to Programming Logic, Introductory. Chapter 6 Arrays

Angela Z: A Language that facilitate the Matrix wise operations Language Reference Manual

Conditional Statement

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CS1004: Intro to CS in Java, Spring 2005

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Transcription:

CS 200 - Programming I: Branches Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624

Boolean Statements

1/29 Booleans Primitive boolean b; Values: true or false Wrapper class: Boolean

2/29 Boolean Expressions An expression that evaluates to a boolean Ie true or false

2/29 Boolean Expressions An expression that evaluates to a boolean Ie true or false Comparison Operators Binary (expr1 oper expr2 ): == : Equal to!= : Not equal to > : Greater than < : Less than >= : Greater than or equal <= : Less than or equal

2/29 Boolean Expressions An expression that evaluates to a boolean Ie true or false Logical Operators Unary (oper expr ):! : Logical NOT Binary (expr1 oper expr2 ): && : Logical AND : Logical OR Don t confuse with the bitwise operators: &,, and ˆ

3/29 Operator Precedence

4/29 Truth Tables a b a b a b a b a 0 0 0 1 1 0 1 1 Logic Rules AND (&&, &) True only if both operands are true OR (, ) True if either operand is true XOR (ˆ) Exclusive OR; True if one (but not both) operand(s) is true NOT (!) Flips the truth value

4/29 Truth Tables a b a b a b a b a 0 0 0 0 1 1 0 1 1 Logic Rules AND (&&, &) True only if both operands are true OR (, ) True if either operand is true XOR (ˆ) Exclusive OR; True if one (but not both) operand(s) is true NOT (!) Flips the truth value

4/29 Truth Tables a b a b a b a b a 0 0 0 0 0 1 1 0 1 1 Logic Rules AND (&&, &) True only if both operands are true OR (, ) True if either operand is true XOR (ˆ) Exclusive OR; True if one (but not both) operand(s) is true NOT (!) Flips the truth value

4/29 Truth Tables a b a b a b a b a 0 0 0 0 0 0 1 1 0 1 1 Logic Rules AND (&&, &) True only if both operands are true OR (, ) True if either operand is true XOR (ˆ) Exclusive OR; True if one (but not both) operand(s) is true NOT (!) Flips the truth value

4/29 Truth Tables a b a b a b a b a 0 0 0 0 0 1 0 1 1 0 1 1 Logic Rules AND (&&, &) True only if both operands are true OR (, ) True if either operand is true XOR (ˆ) Exclusive OR; True if one (but not both) operand(s) is true NOT (!) Flips the truth value

4/29 Truth Tables a b a b a b a b a 0 0 0 0 0 1 0 1 0 1 0 1 1 Logic Rules AND (&&, &) True only if both operands are true OR (, ) True if either operand is true XOR (ˆ) Exclusive OR; True if one (but not both) operand(s) is true NOT (!) Flips the truth value

4/29 Truth Tables a b a b a b a b a 0 0 0 0 0 1 0 1 0 1 1 0 1 1 Logic Rules AND (&&, &) True only if both operands are true OR (, ) True if either operand is true XOR (ˆ) Exclusive OR; True if one (but not both) operand(s) is true NOT (!) Flips the truth value

4/29 Truth Tables a b a b a b a b a 0 0 0 0 0 1 0 1 0 1 1 1 0 1 1 Logic Rules AND (&&, &) True only if both operands are true OR (, ) True if either operand is true XOR (ˆ) Exclusive OR; True if one (but not both) operand(s) is true NOT (!) Flips the truth value

4/29 Truth Tables a b a b a b a b a 0 0 0 0 0 1 0 1 0 1 1 1 1 0 1 1 Logic Rules AND (&&, &) True only if both operands are true OR (, ) True if either operand is true XOR (ˆ) Exclusive OR; True if one (but not both) operand(s) is true NOT (!) Flips the truth value

4/29 Truth Tables a b a b a b a b a 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 Logic Rules AND (&&, &) True only if both operands are true OR (, ) True if either operand is true XOR (ˆ) Exclusive OR; True if one (but not both) operand(s) is true NOT (!) Flips the truth value

4/29 Truth Tables a b a b a b a b a 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 Logic Rules AND (&&, &) True only if both operands are true OR (, ) True if either operand is true XOR (ˆ) Exclusive OR; True if one (but not both) operand(s) is true NOT (!) Flips the truth value

4/29 Truth Tables a b a b a b a b a 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 1 Logic Rules AND (&&, &) True only if both operands are true OR (, ) True if either operand is true XOR (ˆ) Exclusive OR; True if one (but not both) operand(s) is true NOT (!) Flips the truth value

4/29 Truth Tables a b a b a b a b a 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 Logic Rules AND (&&, &) True only if both operands are true OR (, ) True if either operand is true XOR (ˆ) Exclusive OR; True if one (but not both) operand(s) is true NOT (!) Flips the truth value

4/29 Truth Tables a b a b a b a b a 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 Logic Rules AND (&&, &) True only if both operands are true OR (, ) True if either operand is true XOR (ˆ) Exclusive OR; True if one (but not both) operand(s) is true NOT (!) Flips the truth value

4/29 Truth Tables a b a b a b a b a 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 Logic Rules AND (&&, &) True only if both operands are true OR (, ) True if either operand is true XOR (ˆ) Exclusive OR; True if one (but not both) operand(s) is true NOT (!) Flips the truth value

4/29 Truth Tables a b a b a b a b a 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 Logic Rules AND (&&, &) True only if both operands are true OR (, ) True if either operand is true XOR (ˆ) Exclusive OR; True if one (but not both) operand(s) is true NOT (!) Flips the truth value

4/29 Truth Tables a b a b a b a b a 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 0 Logic Rules AND (&&, &) True only if both operands are true OR (, ) True if either operand is true XOR (ˆ) Exclusive OR; True if one (but not both) operand(s) is true NOT (!) Flips the truth value

5/29 TopHat Question 1 If A is 1 and B is 1, what is!a B? a 1 b 0

6/29 Bitwise Operations Performs the logical operations on each of the corresponding bits of the operands Bitwise Operators Binary (expr1 oper expr2 ): & Bitwise AND Bitwise OR ˆ Bitwise XOR

6/29 Bitwise Operations Performs the logical operations on each of the corresponding bits of the operands Eg 7 & 5 = 5 Consider the bits: Bitwise Operators 111 & 101 ----- 101 Binary (expr1 oper expr2 ): & Bitwise AND Bitwise OR ˆ Bitwise XOR

7/29 TopHat Question 2 What is 7 5? a 12 b 7 c 5 d 3

Comparing References

8/29 Reference Comparison Equality Operator (==) Generally, the equality operator compares the values of two variables Recall: value of a reference type is the referral information

Reference Comparison Equality Operator (==) Generally, the equality operator compares the values of two variables Recall: value of a reference type is the referral information Stack 6 Heap Integer i = 6; Integer j = 6; Integer k = new Integer (6); k j i 6 8/29

Reference Comparison Equality Operator (==) [Resp: Inequality Operator (!=)] Generally, the equality operator compares the values of two variables Recall: value of a reference type is the referral information Stack 6 Heap Integer i = 6; Integer j = 6; Integer k = new Integer (6); k j i 6 8/29

9/29 Comparing Values of Primitive Wrappers Integer i = 6; Integer j = 5; Integer k = new Integer ( 6); int m = 5; Instance Methods for Comparison equals(otherobj) True if values of the objects are equal False otherwise Eg iequals(k) // true

9/29 Comparing Values of Primitive Wrappers Integer i = 6; Integer j = 5; Integer k = new Integer ( 6); int m = 5; Instance Methods for Comparison equals(otherobj) True if values of the objects are equal False otherwise Eg iequals(k) // true compareto(otherobj) 0 if equal, negative if less than otherobj, and positive if greater than otherobj Eg icompareto(j) // +ve

Comparing Values of Primitive Wrappers Integer i = 6; Integer j = 5; Integer k = new Integer ( 6); int m = 5; Instance Methods for Comparison equals(otherobj) True if values of the objects are equal False otherwise Eg iequals(k) // true compareto(otherobj) 0 if equal, negative if less than otherobj, and positive if greater than otherobj Eg icompareto(j) // +ve Other Comparison Operators (In)Equality exception: j == m // true Other operators work as expected: <, <=, >, >= 9/29

10/29 String Comparison Only Instance Methods equals(otherstr) compareto(otherstr)

10/29 String Comparison Only Instance Methods equals(otherstr) compareto(otherstr) Lexicographical Comparison Starting from index 0 and compare each pair of characters at index x according to the following rules: 1 The character with the smaller Unicode value is considered smaller lexicograpically 2 No character is smaller than an existing character (Eg "foo" is smaller than "foobar")

10/29 String Comparison Only Instance Methods equals(otherstr) compareto(otherstr) equalsignorecase(otherstr) comparetoignorecase(otherstr) Lexicographical Comparison Starting from index 0 and compare each pair of characters at index x according to the following rules: 1 The character with the smaller Unicode value is considered smaller lexicograpically 2 No character is smaller than an existing character (Eg "foo" is smaller than "foobar")

11/29 TopHat Question 3 Arrange these words in lexicographical order (smallest to largest): a Javascript b C c Java d C++ e C#

Conditional Statements

12/29 If Statement if ( cond ) { truestatement ; lasttruestatement ;

12/29 If Statement Control Flow if ( cond ) { truestatement ; lasttruestatement ; cond true True Stmt Block Following statements false

13/29 If-Else Statement if ( cond ) { truestatement ; lasttruestatement ; else { falsestatement ; lastfalsestatement ;

13/29 If-Else Statement if ( cond ) { truestatement ; lasttruestatement ; else { falsestatement ; lastfalsestatement ; Control Flow true True Stmt Block cond Following statements false False Stmt Block

14/29 TopHat Question 4 What is printed out in be following code block: boolean tobe = false ; if( tobe! tobe ) { System out print (" Tautology "); else { System out print (" Contradiction ");

15/29 TopHat Question 5 What is the boolean expression to complete the following code? Assume that i is an int variable Replace?????? in the code with the appropriate boolean expression if(?????? ) { System out println ( i + " is divisible by 3"); else { System out println ( i + " is not divisible by 3");

16/29 If-Else If Statement Ending Else if ( cond1 ) { truestatements1 ; else if ( cond2 ) { truestatements2 ; else { allfalsestatements ;

16/29 If-Else If Statement Ending Else Control Flow if ( cond1 ) { truestatements1 ; else if ( cond2 ) { truestatements2 ; True 1 Stmt Block True 2 Stmt Block true true cond1 false cond2 All false else { allfalsestatements ; Following statements Else Stmt Block

17/29 If-Else If Statement No Ending Else if ( cond1 ) { truestatements1 ; else if ( cond2 ) { truestatements2 ; else if ( condn ) { truestatementsn ;

17/29 If-Else If Statement No Ending Else Control Flow if ( cond1 ) { truestatements1 ; else if ( cond2 ) { truestatements2 ; else if ( condn ) { truestatementsn ; True 1 Stmt Block True 2 Stmt Block true true True N Stmt Block cond1 Following statements cond2 true false condn false

18/29 Multiple ifs if ( cond1 ) { truestatements1 ; if ( cond2 ) { truestatements2 ; if ( condn ) { truestatementsn ;

18/29 Multiple ifs Control Flow if ( cond1 ) { truestatements1 ; if ( cond2 ) { truestatements2 ; if ( condn ) { truestatementsn ; True 1 Stmt Block True 2 Stmt Block True N Stmt Block true true true cond1 cond2 false condn false Following statements

19/29 TopHat Question 6 What does the following code print? int v1 = 3, v2 = 6; if (v1 + 2 < v2) System out print (" tic " ); else if (v1 + 4 < v2) System out print (" tac " ); else if (v1 + 4 > v2) System out print (" toe " );

20/29 TopHat Question 7 What does the following code print? int v1 = 3, v2 = 6; if (v1 + 2 < v2) System out print (" tic " ); if (v1 + 4 < v2) System out print (" tac " ); if (v1 + 4 > v2) System out print (" toe " );

case const2 : case2statements ; break ; default : defstatements ; break ; 21/29 Switch Statements switch ( expr ) { case const1 : case1statements ; break ;

Switch Statements Control Flow switch ( expr ) { case const1 : case1statements ; break ; case const2 : case2statements ; break ; default : defstatements ; break ; Case 1 Stmt Block Case 2 Stmt Block true true expr == const1 expr == const2 Following statements false No match Default Block 21/29

Switch Statements Control flow depends on break; switch ( expr ) { case const1 : case1statements ; break ; case const2 : case2statements ; break ; default : defstatements ; break ; Control Flow Case 1 Stmt Block Case 2 Stmt Block true true expr == const1 expr == const2 Following statements false No match Default Block 21/29

Switch Statements Control flow depends on break; switch ( expr ) { case const1 : case1statements ; case const2 : case2statements ; break ; default : defstatements ; break ; Control Flow Case 1 Stmt Block Case 2 Stmt Block true no break; true expr == const1 expr == const2 Following statements false No match Default Block 21/29

22/29 Switch Statements Keys Rules Finds the first match going from the top to the bottom in order Fall through A case without a break will fall through to the next case s statements

22/29 Switch Statements Keys Rules Finds the first match going from the top to the bottom in order Fall through A case without a break will fall through to the next case s statements Good Practices Only use fall though when the cases execute the same code Generally, it is best to always have a default case at the end

23/29 If-Else if and Switch Statements if (i == 1) { System out println (" one "); else if (i == 2) { System out println (" two "); else { System out println (" Not 1 or 2");

If-Else if and Switch Statements if (i == 1) { System out println (" one "); else if (i == 2) { System out println (" two "); else { System out println (" Not 1 or 2"); switch (i) { case 1: System out println (" one "); break ; case 2: System out println (" two "); break ; default : System out println (" Not 1 or 2"); break ; 23/29

TopHat Question 8 What does the following code print? int month = 8; switch ( month ) { case 1: System out println (" January "); break ; case 2: System out println (" February "); break ; case 3: System out println (" March "); break ; case 4: System out println (" April "); break ; case 5: System out println ("May "); break ; case 6: System out println ("June "); break ; case 7: System out println ("July "); break ; case 8: System out println (" August "); break ; case 9: System out println (" September "); break ; case 10: System out println (" October "); break ; case 11: System out println (" November "); break ; case 12: System out println (" December "); break ; default : System out println (" Invalid month "); break ; 24/29

TopHat Question 9 What does the following code print? int month = 8; switch ( month ) { case 1: System out println (" January "); case 2: System out println (" February "); case 3: System out println (" March "); case 4: System out println (" April "); case 5: System out println (" May "); case 6: System out println (" June "); case 7: System out println (" July "); case 8: System out println (" August "); case 9: System out println (" September "); case 10: System out println (" October "); case 11: System out println (" November "); case 12: System out println (" December "); break ; default : System out println (" Invalid month "); break ; 25/29

26/29 Ordinal Abbreviation Exercise Write a method that takes an integer as input and returns a string with the integer converted to an abbreviated ordinal Ie 1 becomes 1st, 2 becomes 2nd, 3 becomes, 3rd, etc

Using Eclipse

Eclipse Eclipse Integrated development environment (IDE) that we will use in the course In 2017, 405% use Eclipse (48% use IntelliJ) a In 2016, 482% use Eclipse (436% use IntelliJ) b a Source: http://wwwbaeldungcom/java-in-2017 b Source: http://wwwbaeldungcom/java-ides-2016 27/29

Eclipse Eclipse Integrated development environment (IDE) that we will use in the course In 2017, 405% use Eclipse (48% use IntelliJ) a In 2016, 482% use Eclipse (436% use IntelliJ) b a Source: http://wwwbaeldungcom/java-in-2017 b Source: http://wwwbaeldungcom/java-ides-2016 Installing and Using Installation: https://cs200-wwwcswiscedu/wp/install-eclipse/ Tutorials: https://cs200-wwwcswiscedu/wp/ resources/eclipse-tutorial/ 27/29

28/29 Style Guide https://cs200-wwwcswiscedu/wp/style-guide/ Examples: http: //pagescswiscedu/~cs200/resources/framejava http: //pagescswiscedu/~cs200/resources/circlejava Key Elements File Comment Header Class Comment Header Method Comment Header Good names with proper formatting Proper use of whitespace

29/29 Further Reading COMP SCI 200: Programming I zybookscom, 2015 zybook code: WISCCOMPSCI200Fall2017 Chapter 3 Using Objects Chapter 4 Branches

Appendix References Appendix

Appendix References References

Appendix References 30/29 Image Sources I https://brandwiscedu/web/logos/ http://wwwzybookscom/