Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Conditional statements and Boolean expressions. The if-statement in Java (1) The if-statement is a conditional statement The statement is executed only.
A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Nested conditional statements. Previously discussed Conditional statements discussed so far: Syntax of the if-statement: if-statement if-else-statement.
The switch statement: an N-way selection statement.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
Programming a computer. What does programming a computer mean ? Programming a computer: Since a computer can only execute machine instructions (encoded.
The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)
Shorthand operators.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
The character data type char
Exercises A-Declare a variable of double type with initial value=0.0; B- Declare a constant of String type with initial value=“Good” C- Declare a variable.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
The Rectangle Method. Introduction Definite integral (High School material): A definite integral a ∫ b f(x) dx is the integral of a function f(x) with.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
The dangling-else ambiguity. Previously discussed The Java compiler (translator) consider white space characters (i.e., SPACE, TAB and New line) as insignificant.
Chapter 2 Elementary Programming
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Working with arrays (we will use an array of double as example)
Assignment statements using the same variable in LHS and RHS.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
The if-else statement. The if-else statement in Java The if-else statement is the second conditional statement in Java The if-else statement selects one.
Mixing integer and floating point numbers in an arithmetic operation.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
Integer numerical data types. The integer data types (multiple !) The integer data types use the binary number system as encoding method There are a number.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
The while-statement. Syntax and meaning of the while-statement The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.
Arithmetic expressions containing Mathematical functions.
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
Software Technology - I Tools to do operations.....
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.
Simple algorithms on an array - compute sum and min.
The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Programming Principles Operators and Expressions.
The if-else statement. Introducing the if-else statement Programming problem: Re-write the a,b,c-formula program to solve for complex number solutions.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CompSci 230 S Programming Techniques
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Chapter 7: Expressions and Assignment Statements
User input We’ve seen how to use the standard output buffer
Java Programming: From Problem Analysis to Program Design, 4e
Operators and Expressions
While Statement.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
The Boolean (logical) data type boolean
C Operators, Operands, Expressions & Statements
Chapter 2: Java Fundamentals
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
The for-statement.
Presentation transcript:

Boolean expressions, part 1: Compare operators

Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A compare operator will return the value true if the test is successful A compare operator will return the value false if the test is unsuccessful

Compare operators (cont.) Compare operators in Java: Operator symbol Example Meaning <a < b Returns true if a < b, otherwise returns false <=a <= b Returns true if a ≤ b, otherwise returns false >a > b Returns true if a > b, otherwise returns false >=a >= b Returns true if a ≥ b, otherwise returns false ==a == b Returns true if a is not equal to b, otherwise returns false !=a != b Returns true if a is not equal to b, otherwise returns false

Example program: test divisibility Problem description: Algorithm: Write a Java program that reads in a number a and a number b The program print a message when a is divisible by b A number a is divisible by the number b if and only if: The remainder of the division a/b is equal to 0

Example program: test divisibility (cont.) Java program: import java.util.Scanner; public class Divisible { public static void main(String[] args) { int a, b; Scanner in = new Scanner(System.in); // Construct Scanner object System.out.print("Enter a: "); a = in.nextInt(); // Read in number into a System.out.print("Enter b: "); b = in.nextInt(); // Read in number into b if ( (a % b) == 0 ) System.out.println(a + " is divisible by " + b); }

Example program: test divisibility (cont.) Explanation: The expression (a % b) == 0 will: 1.First compute the remainder of the division a/b 2.Then compare the result (i.e., the remainder of the division) to the value 0

Example program: test divisibility (cont.) Example Program: (Demo above code) –Prog file: Divisible01.java How to run the program: Right click on link and save in a scratch directory To compile: javac Divisible01.java To run: java Divisible01

Comparing integer and floating point values Automatic conversion rule for compare operators: The same automatic conversion rules used for arithmetic operators apply for compare operators The automatic conversion rules for arithmetic operators were summarized on this webpage: yllabus/04/conversion.html

Priority of the compare operators For practical purposes, you can assume that: All compare operators have the same priority This is because you cannot have back to back compare operations Example: this is illegal Because you cannot have back to back compare operations, there is no need to decide which one has higher priority. a < b == c

Priority of the compare operators (cont.) Priority ranking of the compare operators against the previously discussed operators: Priority level Operator(s) Description Associativity 1( ) Brackets 2(int) − Casting, negationRight to left 3++, -- Increment, decrement 4* / % Multiple, divide, remainder Left to right 5+ - Add, subtract Left to right 6 >= == != Compare operators 7 = += -=...Assignment operators Right to left

Priority of the compare operators (cont.) Reference:

Priority of the compare operators (cont.) Example 1: boolean a; Statement: a = 3 > 1; Operators in statement: = > Executed as follows: a = 3 > 1; // > has higher priority than = a = true;

Priority of the compare operators (cont.) Example 2: boolean a; Statement: a = <= 5 - 2; Operators in statement: = + <= - Executed as follows: a = <= 5 - 2; // + and - has highest priority a = 7 <= 3; // <= has higher priority than = a = true;