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

Size: px
Start display at page:

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

Transcription

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

2 Announcements Slides will be posted before the class. There might be few minor changes later. Extra class on the 20 th of May (13:05-15:25) at TR0100 Assignment 1 extended due date. New Due Date : 17 th of May 23:30) Try all the exercises on the class slides. See me during office hours for any doubts and troubles solving them. Thanks for the responses on the survey.

3 Survey Results Can be a bit faster Good, slower is better Prefer more details Very helpful

4 Review Variables Data Types Strings User inputs

5 Review import java.util.scanner; public class SimpleInterest { // A program to compute Simple Interest public static void main(string[] args) { double principal, rateinterest; int yrs; Scanner inputreader = new Scanner(System.in); System.out.println( Enter the Principal Amount ); principal = inputreader.nextdouble(); System.out.println( Enter the Annual Rate of Interest ); rateinterest = inputreader.nextdouble(); System.out.println( Enter the number of years since loan ); yrs = inputreader.nextint(); double simpleinterest = (principal * rateinterest * yrs) / 100; System.out.println( Your simple interest after + yrs + years = + simpleinterest); } }

6 Data Types Review What data type and value do the following expressions evaluate to? (int) (3 / 2) (7 / 4) < (7.0 / 4.0) 25 + Hello ((int) ) / 3.0

7 This Lecture Boolean Expressions Mathematical Expressions Flow Control One step at a time.

8 Expressions An expression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value. An Expression has Operator acting on the Operands to generate a single valued result. int x = ; String s = Hello + COMP202 ; The data type of the result depends both on the operator and the operands. An Expression is always evaluated from left to right, unless it is explicitly specified. There are exceptions depending on the precedence of the operator. We will see operator precedence later in the class.

9 Boolean Expressions

10 Boolean Expressions Boolean expressions evaluate to either true or false. mynumber > 0 // can be either true or false You can assign the result of a boolean expression to a variable of type boolean: boolean positive; positive = (mynumber > 0);

11 Boolean Expressions in Java A boolean expression is one of the following: Comparison of two values using a comparison operator like <. ( x < 5) true or false (Java's boolean literals these are keywords). A variable which has type boolean. (boolean x;) The result of a logical operator over other boolean variables.

12 Comparison Operators The result of a comparison is always true or false. Used to compare numeric or character values (remember ASCII?) == : equal to!= : not equal to < : less than > : greater than <= : less than or equal to >= : greater than or equal to

13 Examples int x = 5; x < 6 x <= 5 x > 5 x >= 3 x == 5 x!= 5 true true false true true false

14 Careful! Terrible things will happen if you mistype == as = int x = 5; int y = 3; (x = y); x is overwritten with the value of y, i.e. x=3 after this statement. This expression will return the value assigned to x, that will be int in this case not boolean (because, = is not a comparison operator). (x = = y); This sets check to false because x is not equal to y. This is the right way to compare. This expression will return a boolean (true or false).

15 Logical Operators These take boolean expressions as input, and produce a result of type boolean.! Logical NOT Logical OR && Logical AND They can have either one operand (unary), as in NOT, or two operands (binary), as in OR and AND.

16 ! Operator The NOT operator flips the truth value of the boolean expression that follows. For an input boolean expression X: X!X true false false true

17 ! Operator Examples!(2 < 3)!(3 < 2)!true!false!(!(3>2))!(September comes after August)!(Earth is the 6 th planet from the sun)

18 && Operator The AND operator is true if both of the input boolean expressions evaluate to true. For input boolean expressions X and Y: X Y X&&Y true true true true false false false true false false false false

19 && Operator Examples (1 < 2) && (2 < 3) (1 < 1) && (2 < 3) (!(1 < 1)) && (2 < 3) (Television starts with a T) && (Java can also be coffee) (true && false)

20 Operator The OR operator is true if at least one of the input boolean expressions evaluate to true. For input boolean expressions X and Y: X Y X Y true true true true false true false true true false false false

21 Operator Examples (1 < 1) (2 < 3)!((3 < 4) (100 > 1000)) ((Vegetables are healthy) (Fruit is healthy)) (true false)

22 Short-Circuit Evaluation The evaluation of && and stops as soon as you know the end result: If the left operand of && is false, the whole thing is false, so the second is never looked at. (p1 && p2) if p1 is false, p2 is never looked at. If the left operand of is true, the whole thing is true, so the second is never looked at. (p1 p2) if p1 is true, p2 is never looked at. This seems like a minor detail, but is important.

23 How This Is Useful Use the first part to check to make sure the second part can actually be run ((x!= 0) && ((1 / x) < 5)) This can be used to check if the program is trying to divide by zero.

24 Example Question Which of the following best describes the set of all pairs of values for boolean variables a and b, such that (!a && b) ==!(a b) evaluates to true? a) Empty set b) Only one pair: a == true, b == false c) Two pairs in which a == true d) Two pairs in which a!= b e) All four possible combinations of values ANSWER: ( C )

25 Mathematical Expressions

26 Mathematical Operators Some operators: Addition (+) Subtraction (-) Multiplication (*) Division (/) Remainder (a.k.a., modulo) (%) How these operators actually function depends on the data type of the operands.

27 + Operator As seen already, int + int (int) int + double (or double + int) (double) Also defined for: String + String "3" + "4" "34" (String) String + primitive data type (or p.d.t. + String) 3 + "4" "34" (String)

28 -, * Operators Subtraction (e.g., 3-4) and multiplication work as you would expect. Like for +, if you mix ints and doubles, you get a double. But not defined for as many data types String * String ERROR

29 Integer Division If both operands to the division operator are int, the result is also an int (with the fractional part discarded) 11 / / 3-3 Division by 0 with integers causes a runtime error. Not detected at compile time! 1 / 0 CRASH

30 % Modulo Operators Modulo operator gives the remainder of a division. Example: 5 % % %

31 Operator Precedence 1. Parenthesis 2. Casting 3. *, /, % from left to right 4. +, -, from left to right Assignment happens after the evaluation of the expression. To reduce ambiguity and to make your life easy, always use parenthesis.

32 Operator Precedence Few Examples: 1.0 / 2 4 / / 3 (double) 1 / 2 (double) (1 / 2) ( ) * * 3 You can learn more on operator precedence at: ators.html

33 +=, ++ (shorthand operators) 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++;

34 -=, *=, /=, -- 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;

35 ++, -- (Increment Operator) You can put ++ or -- before or after a variable name, and even as part of a complex expression. After (post increment) x++: use and then increment. Before (pre increment) ++x: increment and then use. int x = 5, y = 5; System.out.println(x++); System.out.println(++y);

36 Recommendation Only use ++ or -- by themselves, and do not put them inside other expressions. int x = 5; int y = 2 * x++ + 4; // legal, but confusing Other shorthand operators are not very useful and are not seen in general programming practices.

37 Example Question 1) What is the output? int i = 10; int n = i++; System.out.println( Value i = + i + and n = + n); Answer: Value i = 11 and n = 10 2) What is the output? int i = 10; i++; int n = i; System.out.println( Value i = + i + and n = + n); Answer: Value i = 11 and n = 11

38 Try it out Write a program that takes a 3-digit number and checks if the digits are in ascending order. For example: If the input is 321, your program should output true. If the input is 213, your program should output false. Hint: break it down into the following steps: 1. Set up main method in a class 2. Get the Scanner working 3. Separate every digit of the number (as in unit place, ten s place, hundred place) 4. Use Boolean expressions to generate the required result.

39 Conditionals (Testing)...

40 Control Flow Up to this point, our programs proceed in a "straight" fashion. Just follow each line of code in the main method There are commands that can decide if a particular block of code needs to be executed or not. These commands refer to the control flow because you control where the program execution goes next.

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

42 if and if-else if (<condition>) { <body statements 1> } if (<condition>) { <body statements 1> } else { <body statements 2> }

43 ( if ) and (if-else)

44 Example Example: Print different things depending on what the String we are given is. Scanner scanner = new Scanner(System.in); String languagechoice = scanner.nextline(); if (languagechoice.equals("english")) { System.out.println("You chose English."); } else { System.out.println("You chose non-english."); }

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

46 Structure of a Conditional if (languagechoice.equals("english")) { System.out.println("You chose English."); } else { System.out.println("You chose non-english."); } The block of code executes if the condition evaluates to true.

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

48 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.

49 Going Into Two if Statements int x = 1; if (x > 0) { System.out.println("Positive. Altering the value."); x--; } if (x <= 0) { System.out.println("Not positive"); }

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

51 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 (condition 1) { } else { } if (condition 2) { } else { } if (condition 3) { }

52 Better Way (else if) Option 2: All at the same level if (condition 1) { } else if (condition 2) { } else if (condition 3) { } else { }

53 if - else if - else

54 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.

55 What Is Wrong Here? if (money > 0.0) { System.out.println("Positive balance"); } else if (money > ) { System.out.println("You're rich!"); } else { System.out.println("Uh-oh."); } What happens if the variable money = 1200?

56 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!

57 Example Question 1) What will be the value of x after the following section of code executes: int x = 5; if (x > 3) x = x 2; else Answer: B x = x + 2; A. 1 B. 3 C. 5 D. 7 2) What will be the value of b after the following section of code executes: int a = 4, b = 0; if (a < 3) b = 4; else if ( (a < 10) && (b>0) ) b = 3; else if ( (a > 5) (b==0) ) Answer: C b = 2; else b = 1; A. 1 B. 0 C. 2 D. 4

58 Try it out!! Extend your previous program to convert temperatures. 1. Now, it first asks if the user wants to convert from Fahrenheit to Celsius. 2. If the user replies "true", it works as before. 3. Otherwise, it converts from Celsius to Fahrenheit.

59 Try it out!! Read three positive integers x, y, and z from the user and report the maximum and the minimum integers.

60 Summary Boolean Expressions Mathematical Expressions Conditional Statements if if else if else if

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

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

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

COMP-202: Foundations of Programming Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016 More Tutoring Help The Engineering Peer Tutoring Services (EPTS) is hosting free tutoring sessions

### Basics of Java Programming

Basics of Java Programming Lecture 2 COP 3252 Summer 2017 May 16, 2017 Components of a Java Program statements - A statement is some action or sequence of actions, given as a command in code. A statement

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

COMP-202 Unit 2: Java Basics CONTENTS: Using Expressions and Variables Types Strings Methods Assignment 1 Assignment 1 posted on WebCt and course website. It is due May 18th st at 23:30 Worth 6% Part programming,

### COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015 Announcements Midterm Exams on 4 th of June (12:35 14:35) Room allocation will be announced soon

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

COMP-202: Foundations of Programming Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015 Announcements Check the calendar on the course webpage regularly for updates on tutorials and office hours.

### Chapter 3: Operators, Expressions and Type Conversion

101 Chapter 3 Operators, Expressions and Type Conversion Chapter 3: Operators, Expressions and Type Conversion Objectives To use basic arithmetic operators. To use increment and decrement operators. To

### Object-Oriented Programming

Object-Oriented Programming Java Syntax Program Structure Variables and basic data types. Industry standard naming conventions. Java syntax and coding conventions If Then Else Case statements Looping (for,

### Building Java Programs

Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 1 2 Data and expressions reading: 2.1 3 The computer s view Internally, computers store everything as 1 s and 0

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

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1 Motivation In the programs we have written thus far, statements are executed one after the other, in the order in which they appear. Programs often

### IEEE Floating-Point Representation 1

IEEE Floating-Point Representation 1 x = ( 1) s M 2 E The sign s determines whether the number is negative (s = 1) or positive (s = 0). The significand M is a fractional binary number that ranges either

Lecture Set 4: More About Methods and More About Operators Methods Definitions Invocations More arithmetic operators Operator Side effects Operator Precedence Short-circuiting main method public static

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

Selections EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG Learning Outcomes The Boolean Data Type if Statement Compound vs. Primitive Statement Common Errors

### PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

PRIMITIVE VARIABLES CS302 Introduction to Programming University of Wisconsin Madison Lecture 3 By Matthew Bernstein matthewb@cs.wisc.edu Variables A variable is a storage location in your computer Each

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

Building Java Programs Chapter 2: Primitive Data and Definite Loops Copyright 2008 2006 by Pearson Education 1 Lecture outline data concepts Primitive types: int, double, char (for now) Expressions: operators,

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

Topic 4 Variables Once a programmer has understood the use of variables, he has understood the essence of programming -Edsger Dijkstra What we will do today Explain and look at examples of primitive data

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

Data and Variables Data Types Expressions Operators Precedence String Concatenation Variables Declaration Assignment Shorthand operators Review class All code in a java file is written in a class public

### Chapter 3. Selections

Chapter 3 Selections 1 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator 6. The Switch Statement 7. Useful Hints 2 1. Flow of

### CS 112 Introduction to Programming

CS 112 Introduction to Programming Conditional Statements Boolean Expressions and Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

### CS 112 Introduction to Programming

CS 112 Introduction to Programming Conditional Statements Boolean Expressions and Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

### COMP 202 Java in one week

CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator COMP 202 Java in one week The Java Programming Language A programming language

### COMP 202 Java in one week

COMP 202 Java in one week... Continued CONTENTS: Return to material from previous lecture At-home programming exercises Please Do Ask Questions It's perfectly normal not to understand everything Most of

### false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44

### Operators. Java operators are classified into three categories:

Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

### Building Java Programs

Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 1 Data and expressions reading: 2.1 self-check: 1-4 videos: Ch. 2 #1 2 Data types type: A category or set of data

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

Motivating Examples (1.1) Selections EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG 1 import java.util.scanner; 2 public class ComputeArea { 3 public static void main(string[] args)

Lecture Notes 1. Comments a. /* */ b. // 2. Program Structures a. public class ComputeArea { public static void main(string[ ] args) { // input radius // compute area algorithm // output area Actions to

### Chapter 2: Data and Expressions

Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University April 21, 2015 Chapter 2: Data and Expressions CS 121 1 / 53 Chapter 2 Part 1: Data Types

### cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

topics: introduction to java, part 1 cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 cis20.1-fall2007-sklar-leci.2 1 Java. Java is an object-oriented language: it is

### Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Data Types Java is a strongly-typed 1 programming language. Every variable has a type. Also, every (mathematical) expression has a type. There are two categories of data types: primitive data types, and

### false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 45

### Building Java Programs

Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 Copyright 2009 by Pearson Education Data and expressions reading: 2.1 self-check: 1-4 videos: Ch. 2 #1 Copyright

### Chapter 2: Data and Expressions

Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University January 15, 2015 Chapter 2: Data and Expressions CS 121 1 / 1 Chapter 2 Part 1: Data

### Lecture 3 Operators MIT AITI

Lecture 3 Operators MIT AITI - 2004 What are Operators? Operators are special symbols used for mathematical functions assignment statements logical comparisons Examples: 3 + 5 // uses + operator 14 + 5

Lecture Set 4: More About Methods and More About Operators Methods Definitions Invocations More arithmetic operators Operator Side effects Operator Precedence Short-circuiting main method public static

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

COMP-202 Unit 2: Java Basics CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables Part 1: Printing to the Screen Recall: HelloWorld public class

### Full file at

Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

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

JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

### Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

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

COMP-202 Unit 2: Java Basics CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables Tutorial 0 Help with setting up your computer to compile and

### CEN 414 Java Programming

CEN 414 Java Programming Instructor: H. Esin ÜNAL SPRING 2017 Slides are modified from original slides of Y. Daniel Liang WEEK 2 ELEMENTARY PROGRAMMING 2 Computing the Area of a Circle public class ComputeArea

### Programming with Java

Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules

### Primitive data, expressions, and variables

How the computer sees the world Primitive data, expressions, and variables Readings:.. Internally, the computer stores everything in terms of s and 0 s Example: h 0000 "hi" 0000000 0 0000 How can the computer

### Chapter 4: Control Structures I

Chapter 4: Control Structures I Java Programming: From Problem Analysis to Program Design, Second Edition Chapter Objectives Learn about control structures. Examine relational and logical operators. Explore

### COMP-202 Unit 4: Programming with Iterations

COMP-202 Unit 4: Programming with Iterations Doing the same thing again and again and again and again and again and again and again and again and again... CONTENTS: While loops Class (static) variables

### 3. Java - Language Constructs I

Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks

### Programming Lecture 3

Programming Lecture 3 Expressions (Chapter 3) Primitive types Aside: Context Free Grammars Constants, variables Identifiers Variable declarations Arithmetic expressions Operator precedence Assignment statements

### Building Java Programs

Building Java Programs Chapter 2: Primitive Data and Definite Loops These lecture notes are copyright (C) Marty Stepp and Stuart Reges, 2007. They may not be rehosted, sold, or modified without expressed

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

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb 9 2006 based on slides by Kurt Eiselt http://www.cs.ubc.ca/~tmm/courses/cpsc111-06-spr

### Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Topics 1. Expressions 2. Operator precedence 3. Shorthand operators 4. Data/Type Conversion 1/15/19 CSE 1321 MODULE 2 2 Expressions

### ECE 122 Engineering Problem Solving with Java

ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction Outline Problem: How do I input data and use it in complicated expressions Creating complicated expressions

### Operators. Lecture 3 COP 3014 Spring January 16, 2018

Operators Lecture 3 COP 3014 Spring 2018 January 16, 2018 Operators Special built-in symbols that have functionality, and work on operands operand an input to an operator Arity - how many operands an operator

### Chapter 2. Elementary Programming

Chapter 2 Elementary Programming 1 Objectives To write Java programs to perform simple calculations To obtain input from the console using the Scanner class To use identifiers to name variables, constants,

### Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Java Programming Fundamentals - Day 1 07.09.2016 Instructor: Jason Yoon Website: http://mryoon.weebly.com Quick Advice Before We Get Started Java is not the same as javascript! Don t get them confused

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

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

Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:

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

COMP-202 Unit 2: Java Basics CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables Part 1: Printing to the Screen Recall: HelloWorld public class

### Lesson 7 Part 2 Flags

Lesson 7 Part 2 Flags A Flag is a boolean variable that signals when some condition exists in a program. When a flag is set to true, it means some condition exists When a flag is set to false, it means

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

Announcements Lab Friday, 1-2:30 and 3-4:30 in 26-152 Boot your laptop and start Forte, if you brought your laptop Create an empty file called Lecture4 and create an empty main() method in a class: 1.00

### Unit 3. Operators. School of Science and Technology INTRODUCTION

INTRODUCTION Operators Unit 3 In the previous units (unit 1 and 2) you have learned about the basics of computer programming, different data types, constants, keywords and basic structure of a C program.

### Primitive Data, Variables, and Expressions; Simple Conditional Execution

Unit 2, Part 1 Primitive Data, Variables, and Expressions; Simple Conditional Execution Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Overview of the Programming Process Analysis/Specification

### Conditional Programming

COMP-202 Conditional Programming Chapter Outline Control Flow of a Program The if statement The if - else statement Logical Operators The switch statement The conditional operator 2 Introduction So far,

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

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition Learn about repetition (looping) control structures Explore how to construct and use: o Counter-controlled

### COMP 202. Java in one week

COMP 202 CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator Java in one week The Java Programming Language A programming language

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

Tester vs. Controller Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG For effective illustrations, code examples will mostly be written in the form of a tester

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

Table of Contents 1 INTRODUCTION... 1 2 IF... 1 2.1 BOOLEAN EXPRESSIONS... 3 2.2 BLOCKS... 3 2.3 IF-ELSE... 4 2.4 NESTING... 5 3 SWITCH (SOMETIMES KNOWN AS CASE )... 6 3.1 A BIT ABOUT BREAK... 7 4 CONDITIONAL

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

### CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CONTENTS: Compilation Data and Expressions COMP 202 More on Chapter 2 Programming Language Levels There are many programming language levels: machine language assembly language high-level language Java,

### Elementary Programming

Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG Learning Outcomes Learn ingredients of elementary programming: data types [numbers, characters, strings] literal

### JAVA OPERATORS GENERAL

JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

### Data Structure and Programming Languages

204700 Data Structure and Programming Languages Jakarin Chawachat From: http://ocw.mit.edu/courses/electrical-engineering-and-computerscience/6-092-introduction-to-programming-in-java-january-iap-2010/index.htm

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

COMP-202 Unit 2: Java Basics CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables Assignment 1 Assignment 1 posted on WebCt and course website.

### Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

### CMPT 125: Lecture 3 Data and Expressions

CMPT 125: Lecture 3 Data and Expressions Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 3, 2009 1 Character Strings A character string is an object in Java,

### CS1150 Principles of Computer Science Boolean, Selection Statements

CS1150 Principles of Computer Science Boolean, Selection Statements Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 Math Center https://www.uccs.edu/mathcenter/schedules

### Prof. Navrati Saxena TA: Rochak Sachan

JAVA Prof. Navrati Saxena TA: Rochak Sachan Operators Operator Arithmetic Relational Logical Bitwise 1. Arithmetic Operators are used in mathematical expressions. S.N. 0 Operator Result 1. + Addition 6.

### Topic 4 Expressions and variables

Topic 4 Expressions and variables "Once a person has understood the way variables are used in programming, he has understood the quintessence of programming." -Professor Edsger W. Dijkstra Based on slides

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

Boolean Expressions Now we have the ability to read in some information, calculate some formulas and display the information to the user in a nice format. However, the real power of computer programs lies

### 1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

Contents 1 Introduction 3 1.1 Java, the beginning.......................... 3 1.2 Java Virtual Machine........................ 4 1.3 A First Program........................... 4 1.4 BlueJ.................................

### 4 Programming Fundamentals. Introduction to Programming 1 1

4 Programming Fundamentals Introduction to Programming 1 1 Objectives At the end of the lesson, the student should be able to: Identify the basic parts of a Java program Differentiate among Java literals,

### Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Numerical Data CS 180 Sunil Prabhakar Department of Computer Science Purdue University Problem Write a program to compute the area and perimeter of a circle given its radius. Requires that we perform operations

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

Review Primitive Data Types & Variables int, long float, double boolean char String Mathematical operators: + - * / % Comparison: < > = == 1 1.3 Conditionals and Loops Introduction to Programming in

### Mr. Monroe s Guide to Mastering Java Syntax

Mr. Monroe s Guide to Mastering Java Syntax Getting Started with Java 1. Download and install the official JDK (Java Development Kit). 2. Download an IDE (Integrated Development Environment), like BlueJ.

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

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition Learn about repetition (looping) control structures Explore how to construct and use: o Counter-controlled

### Building Java Programs Chapter 2

Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson 2013. All rights reserved. Data types type: A category or set of data values. Constrains the operations that can

### Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lectures Control Structures

Introduction to Computer Science, Shimon Schocken, IDC Herzliya Lectures 3.1 3.2 Control Structures Control Structures, Shimon Schocken IDC Herzliya, www.intro2cs.com slide 1 Control structures A program

### Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

OOP in Java 1 Outline 1. Getting started, primitive data types and control structures 2. Classes and objects 3. Extending classes 4. Using some standard packages 5. OOP revisited Parts 1 to 3 introduce

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

More Things We Can Do With It! More operators and expression types More s 11 October 2007 Ariel Shamir 1 Overview Variables and declaration More operators and expressions String type and getting input

### Accelerating Information Technology Innovation

Accelerating Information Technology Innovation http://aiti.mit.edu Cali, Colombia Summer 2012 Lesson 02 Variables and Operators Agenda Variables Types Naming Assignment Data Types Type casting Operators

### Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Interpreted vs Compiled Python 1 Java Interpreted Easy to run and test Quicker prototyping Program runs slower Compiled Execution time faster Virtual Machine compiled code portable Java Compile > javac

### COMP 110 Introduction to Programming. What did we discuss?

COMP 110 Introduction to Programming Fall 2015 Time: TR 9:30 10:45 Room: AR 121 (Hanes Art Center) Jay Aikat FB 314, aikat@cs.unc.edu Previous Class What did we discuss? COMP 110 Fall 2015 2 1 Today Announcements

### WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS OPERATORS Review: Data values can appear as literals or be stored in variables/constants Data values can be returned by method calls Operators: special symbols

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

Entry Point of Execution: the main Method Elementary Programming EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG For now, all your programming exercises will

### Decisions (If Statements) And Boolean Expressions

Decisions (If Statements) And Boolean Expressions CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Last updated: 2/15/16 1 Syntax if (boolean_expr)

### Warm-Up: COMP Programming with Iterations 1

Warm-Up: Suppose I have a method with header: public static boolean foo(boolean a,int b) { if (a) return true; return b > 0; Which of the following are valid ways to call the method and what is the result?

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

CS 231 Data Structures and Algorithms Fall 2018 Event Based Programming Lecture 06 - September 17, 2018 Prof. Zadia Codabux 1 Agenda Event-based Programming Misc. Java Operator Precedence Java Formatting

### COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site I have decided to keep this site for the whole semester I still hope to have blackboard up and running, but you

### false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44