Introduction to Computers and Programming Lecture 4: Mathematical and Relational Operators.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte,
Chapter 2 - Introduction to Java Applications
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 3 Variables Primitive Data Types calculations & arithmetic operators Readings: Chapter 2.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Introduction to Computers and Programming Lecture 7:
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
CMT Programming Software Applications
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
 2003 Prentice Hall, Inc. Modified for use with this class. All rights reserved. 1 Introduction to Computers and Programming in JAVA: V Primitive.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
Chapter 2 - Introduction to Java Applications
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Introduction to C Programming
Introduction to Java Programming, 4E Y. Daniel Liang.
Basic Input/Output and Variables Ethan Cerami New York
Programming with Java Instructor : Neelima Gupta.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
S.W.Ma/CIM/LWL41211/2 Prog. IIA Page 1 Lecture 2&3 Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables,
Chapter 2 Elementary Programming
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
1 Java Programming 1 Introduction to Basic GUI Lesson 4.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
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.
1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing a Line of Text 2.2.1Compiling and Executing.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Lecture 3: Operators, Expressions and Type Conversion
2.5 Another Java Application: Adding Integers
Multiple variables can be created in one declaration
Chapter 2 - Introduction to Java Applications
Chapter 2 - Introduction to Java Applications
MSIS 655 Advanced Business Applications Programming
Chapter 2 - Introduction to Java Applications
Chapter 2 - Introduction to Java Applications
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Introduction to Computers and Programming Lecture 4: Mathematical and Relational Operators

Basic Mathematical Operators

Each of the operators in the table are binary operators. A binary operator acts on two operands

 2000 Prentice Hall, Inc. All rights reserved. 4 Lets look at a program to Calculate the area of a circle // Program calculates area of a circle (uses double data types) public class circle_area { public static void main(String[] args) { double radius, area; // declare variables double radius = 3.00; // assign radius of the circle area = radius * radius * ; System.out.println("The area of the circle of radius " + radius + " is " + area); System.exit(0); }

Integer Division - The Problem Suppose you have the following code: Using a calculator, the answer is But x can only hold integer values is clearly not an integer value. int x; x = 7 / 4;

Integer Division - Solution To understand the solution, you need to remember your 3 rd Grade Math (really.) 7/4 = 1 (Integer Division) 7%4 = 3 (Modulus Division) The answer: 1 remainder 3

Example: Integer and Modulus Division /* Integer and Modulus Division */ public class mod_division { public static void main(String args[]) { int x = 5, y =10; System.out.println ("5/10: " + x/y); System.out.println ("5%10: " + x%y); } // end method main } // end class mod_division 5/10: 0 5%10: 5 No matter what, your answers must be integers. 5/10 = 0 5%10 =

Odd / Even Numbers Modulus division can also be used to determine whether a number is odd or even. Just divide by 2. –If the remainder (modulus) is 0, the number is even. Examples: –10 % 2 = 0. Hence 10 is even. –11 % 2 = 1. Hence 11 is odd. Common Programming Error: Dividing by zero is normally undefined on computer systems generally results in a fatal error.

To find out if number evenly divide by another number Modulus division can also be used to determine whether a number evenly divide into another number or not. If the remainder (modulus) is 0, the number evenly divide. Examples: –10 % 2 = 0. Hence 10 is evenly divide into 2. –11 % 2 = 1. Hence 11 is does not divide evenly into 1. –30 % 5 = 0. Hence 30 evenly divide into 5. –100 % 8 = 5. ( 100 / 8 = 12.5 ) »Hence 100 does not evenly divide into 8.

Lets do this in class- what's the answer? Can we notice a pattern? 1234 / 1000 = ? 1234 % 1000 =? 234 / 100= ? 234 % 100= ? 34 / 10= ? 34 % 10=?

Lets do this in class- what's the answer? Can we notice a pattern? 1234 / 1000 = 1 ( lost three right digits and ended up with the left (first) digit) 1234 % 1000 = 234 (lost the left most digit and ended up with the remaining 3 digits) 234 / 100= 2 ( lost the 2 right digits and ended up with the left (first) digit) 234 % 100= 34 (lost the left most digit and ended up with the remaining 2 digits) 34 / 10= 3 ( lost right digit and ended up with the left (first) digit) 34 % 10= 4 (lost the left most digit and ended up with the remaining right digit)

 2000 Prentice Hall, Inc. All rights reserved. 12 What is the out put of this program? public class date_digits { public static void main(String[] args) { int date, month, day, div; date = 1213; month = date/100; day = date % 100; System.out.println("month is " +month +"\n" + "day is " + day + "\n” ); }

 2000 Prentice Hall, Inc. All rights reserved. 13 What is the out put of this program? public class date_digits { public static void main(String[] args) { int date, month, day, div; date = 1213; month = date/100; day = date % 100; System.out.println("month is " +month +"\n" + "day is " + day + "\n"); } month is 12 day is 13 Press any key to continue...

Operator Precedence

Here’s another problem. What’s the answer to this? x = * 6; Two Options (depending on the order of operations):  Perform addition first: = 10  10 * 6 = 60  Perform multiplication first: 3*6 =18  7+18 = 25 Which option is correct? –Clearly, we cannot have this kind of ambiguity.

Operator Precedence Operator precedence represent rules for evaluating mathematical expressions. Every programming language has similar rules.

Operator Precedence Hence, option #2 is always correct –(multiplication is performed first): Example: Find the average of three variables a, b and c Do not use: a + b + c / 3 Use: (a + b + c ) / 3 x = * 6; Evaluates to x = = 25

Parentheses Are your friends Are your really good friends Because with them you can ensure expressions are evaluated as you expect Can avoid mistakes with operator precedence (one less thing to think about) –e.g. y = m * x + b ; y = (m * x) + b; –e.g. y = a * b * b + c * b – d; y = (((a * b) * b) + (c * b)) – d;

 2000 Prentice Hall, Inc. All rights reserved. 19 Lets look at a program to allow user to input data using an imput text prompt window

 2003 Prentice Hall, Inc. All rights reserved (Modified). 20 Getting Input from Input Dialog Boxes String string = JOptionPane.showInputDialog( null, “Prompt Message”, “Dialog Title”, JOptionPane.QUESTION_MESSAGE));

 2003 Prentice Hall, Inc. All rights reserved (Modified). 21 Converting Strings to Integers The input returned from the input dialog box is a string. If you enter a numeric value such as 123, it returns “123”. To obtain the input as a number, you have to convert a string into a number. To convert a string into an int value, you can use the static parseInt method in the Integer class as follows: int intValue = Integer.parseInt(intString); Where intString is a numeric string such as “123”.

 2003 Prentice Hall, Inc. All rights reserved (Modified). 22 Converting Strings to Doubles To convert a string into a double value, you can use the static parseDouble method in the Double class as follows: double doubleValue =Double.parseDouble(doubleString); where doubleString is a numeric string such as “123.45”.

 2003 Prentice Hall, Inc. All rights reserved. Outline 23 Addition2.java 1. import 2. class Addition 2.1 Declare variables (name and type) 3. showInputDialog 4. parseInt 5. Add numbers, put result in sum 1 // Addition2.java uses JOptionPane prompt window to enter data 2 // Addition program that displays the sum of two numbers. 3 4 // Java packages 5 import javax.swing.JOptionPane; // program uses JOptionPane 6 7 public class Addition2 { 8 9 // main method begins execution of Java application 10 public static void main( String args[] ) 11 { 12 String firstNumber; // first string entered by user 13 String secondNumber; // second string entered by user int number1; // first number to add 16 int number2; // second number to add 17 int sum; // sum of number1 and number // read in first number from user as a String 20 firstNumber = JOptionPane.showInputDialog( "Enter first integer" ); // read in second number from user as a String 23 secondNumber = 24 JOptionPane.showInputDialog( "Enter second integer" ); // convert numbers from type String to type int 27 number1 = Integer.parseInt( firstNumber ); 28 number2 = Integer.parseInt( secondNumber ); // add numbers 31 sum = number1 + number2; 32 Declare variables: name and type.Input first integer as a String, assign to firstNumber. Add, place result in sum.Convert strings to integers.

 2003 Prentice Hall, Inc. All rights reserved. Outline 24 Program output 33 // display result 34 JOptionPane.showMessageDialog( null, "The sum is " + sum, 35 "Results", JOptionPane.PLAIN_MESSAGE ); System.exit( 0 ); // terminate application with window } // end method main } // end class Addition2

 2003 Prentice Hall, Inc. All rights reserved (Modified). 25 Adding Integers –Location of JOptionPane for use in the program –Begins public class Addition Recall that file name must be Addition.java –Lines 10-11: main –Declaration firstNumber and secondNumber are variables 5 import javax.swing.JOptionPane; // program uses JOptionPane 7 public class Addition { 12 String firstNumber; // first string entered by user 13 String secondNumber; // second string entered by user

 2003 Prentice Hall, Inc. All rights reserved (Modified). 26 Adding Integers –Variables Location in memory that stores a value –Declare with name and type before use firstNumber and secondNumber are of type String (package java.lang ) –Hold strings Variable name: any valid identifier Declarations end with semicolons ; –Can declare multiple variables of the same type at a time –Use comma separated list –Can add comments to describe purpose of variables String firstNumber, secondNumber; 12 String firstNumber; // first string entered by user 13 String secondNumber; // second string entered by user

 2003 Prentice Hall, Inc. All rights reserved (Modified). 27 Adding Integers –Declares variables number1, number2, and sum of type int int holds integer values (whole numbers): i.e., 0, -4, 97 Types float and double can hold decimal numbers Type char can hold a single character: i.e., x, $, \n, 7 Primitive types - more in Chapter 4 15 int number1; // first number to add 16 int number2; // second number to add 17 int sum; // sum of number1 and number2

 2003 Prentice Hall, Inc. All rights reserved (Modified). 28 Adding Integers –Reads String from the user, representing the first number to be added Method JOptionPane.showInputDialog displays the following: Message called a prompt - directs user to perform an action Argument appears as prompt text If wrong type of data entered (non-integer) or click Cancel, error occurs 20 firstNumber = JOptionPane.showInputDialog( "Enter first integer" );

 2003 Prentice Hall, Inc. All rights reserved (Modified). 29 Adding Integers –Result of call to showInputDialog given to firstNumber using assignment operator = Assignment statement = binary operator - takes two operands –Expression on right evaluated and assigned to variable on left Read as: firstNumber gets value of JOptionPane.showInputDialog( "Enter first integer" ) 20 firstNumber = JOptionPane.showInputDialog( "Enter first integer" );

 2003 Prentice Hall, Inc. All rights reserved (Modified). 30 Adding Integers –Similar to previous statement Assigns variable secondNumber to second integer input –Method Integer.parseInt Converts String argument into an integer (type int ) –Class Integer in java.lang Integer returned by Integer.parseInt is assigned to variable number1 (line 27) –Remember that number1 was declared as type int Line 28 similar 23 secondNumber = 24 JOptionPane.showInputDialog( "Enter second integer" ); 27 number1 = Integer.parseInt( firstNumber ); 28 number2 = Integer.parseInt( secondNumber );

 2003 Prentice Hall, Inc. All rights reserved (Modified). 31 Adding Integers –Assignment statement Calculates sum of number1 and number2 (right hand side) Uses assignment operator = to assign result to variable sum Read as: sum gets the value of number1 + number2 number1 and number2 are operands 31 sum = number1 + number2;

 2003 Prentice Hall, Inc. All rights reserved (Modified). 32 Adding Integers –Use showMessageDialog to display results –"The sum is " + sum Uses the operator + to "add" the string literal "The sum is" and sum Concatenation of a String and another type –Results in a new string If sum contains 117, then "The sum is " + sum results in the new string "The sum is 117" Note the space in "The sum is " More on strings in Chapter JOptionPane.showMessageDialog( null, "The sum is " + sum, 35 "Results", JOptionPane.PLAIN_MESSAGE );

 2003 Prentice Hall, Inc. All rights reserved (Modified). 33 Adding Integers –Different version of showMessageDialog Requires four arguments (instead of two as before) First argument: null for now Second: string to display Third: string in title bar Fourth: type of message dialog with icon –Line 35 no icon: JOptionPane.PLAIN_MESSAGE 34 JOptionPane.showMessageDialog( null, "The sum is " + sum, 35 "Results", JOptionPane.PLAIN_MESSAGE );

 2003 Prentice Hall, Inc. All rights reserved (Modified). 34 Adding Integers

Relational Operators Decision using if statements

 2003 Prentice Hall, Inc. All rights reserved (Modified). 36 Decision Making: Equality and Relational Operators if control statement –Simple version in this section, more detail later –If a condition is true, then the body of the if statement executed –Control always resumes after the if structure –Conditions for if statements can be formed using equality or relational operators (next slide) if ( condition ) statement executed if condition true No semicolon needed after condition –Else conditional task not performed

Equality and Relational Operators

 2003 Prentice Hall, Inc. All rights reserved. Outline 38 Comparison.java 1. import 2. Class Comparison 2.1 main 2.2 Declarations 2.3 Input data ( showInputDialo g ) 2.4 parseInt 2.5 Initialize result 1 // Comparison.java 2 // Compare integers using if statements, relational operators 3 // and equality operators. 4 5 // Java packages 6 import javax.swing.JOptionPane; 7 8 public class Comparison { 9 10 // main method begins execution of Java application 11 public static void main( String args[] ) 12 { 13 String firstNumber; // first string entered by user 14 String secondNumber; // second string entered by user 15 String result; // a string containing the output int number1; // first number to compare 18 int number2; // second number to compare // read first number from user as a string 21 firstNumber = JOptionPane.showInputDialog( "Enter first integer:" ); // read second number from user as a string 24 secondNumber = 25 JOptionPane.showInputDialog( "Enter second integer:" ); // convert numbers from type String to type int 28 number1 = Integer.parseInt( firstNumber ); 29 number2 = Integer.parseInt( secondNumber ); // initialize result to empty String 32 result = ""; 33

 2003 Prentice Hall, Inc. All rights reserved. Outline 39 Comparison.java 3. if statements 4. showMessageDialo g 34 if ( number1 == number2 ) 35 result = result + number1 + " == " + number2; if ( number1 != number2 ) 38 result = result + number1 + " != " + number2; if ( number1 < number2 ) 41 result = result + "\n" + number1 + " < " + number2; if ( number1 > number2 ) 44 result = result + "\n" + number1 + " > " + number2; if ( number1 <= number2 ) 47 result = result + "\n" + number1 + " <= " + number2; if ( number1 >= number2 ) 50 result = result + "\n" + number1 + " >= " + number2; // Display results 53 JOptionPane.showMessageDialog( null, result, "Comparison Results", 54 JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); // terminate application } // end method main } // end class Comparison Test for equality, create new string, assign to result. Notice use of JOptionPane.INFORMATION_MESSAGE

 2003 Prentice Hall, Inc. All rights reserved. Outline 40 Program Output

 2003 Prentice Hall, Inc. All rights reserved (Modified) Decision Making: Equality and Relational Operators –Lines 1-12: Comments, import JOptionPane, begin class Comparison and main –Lines 13-18: declare variables Can use comma-separated lists instead: –Lines 21-30: obtain user-input numbers and parses input string into integer variables 13 String firstNumber, 14 secondNumber, 15 result;

 2003 Prentice Hall, Inc. All rights reserved (Modified) Decision Making: Equality and Relational Operators –Initialize result with empty string –if statement to test for equality using ( == ) If variables equal (condition true) –result concatenated using + operator –result = result + other strings –Right side evaluated first, new string assigned to result If variables not equal, statement skipped 32 result = ""; 34 if ( number1 == number2 ) 35 result = result + number1 + " == " + number2;

 2003 Prentice Hall, Inc. All rights reserved (Modified) Decision Making: Equality and Relational Operators –Lines 37-50: other if statements testing for less than, more than, etc. If number1 = 123 and number2 = 123 –Line 34 evaluates true (if number1 = = number 2) Because number1 equals number2 –Line 40 evaluates false (if number1 < number 2) Because number1 is not less than number2 –Line 49 evaluates true (if number1 >= number2) Because number1 is greater than or equal to number2 –Lines 53-54: result displayed in a dialog box using showMessageDialog

 2003 Prentice Hall, Inc. All rights reserved (Modified) Decision Making: Equality and Relational Operators Precedence of operators –All operators except for = (assignment) associates from left to right For example: x = y = z is evaluated x = (y = z)

 2003 Prentice Hall, Inc. All rights reserved (Modified). 45 Increment and Decrement Operators

 2003 Prentice Hall, Inc. All rights reserved (Modified). 46 Increment and Decrement Operators, cont.

 2003 Prentice Hall, Inc. All rights reserved (Modified). 47 Increment and Decrement Operators, cont. F Using increment and decrement operators makes expressions short F but it also makes them complex and difficult to read. F Avoid using these operators in expressions that modify multiple variables, or the same variable for multiple times such as this: int k = ++i + I;

 2003 Prentice Hall, Inc. All rights reserved (Modified). 48 Assignment Expressions and Assignment Statements Prior to Java 2, all the expressions can be used as statements. Since Java 2, only the following types of expressions can be statements: variable op= expression; // Where op is +, -, *, /, or % ++variable; variable++; --variable; variable--;

 2003 Prentice Hall, Inc. All rights reserved (Modified). 49 Numeric Type Conversion Consider the following statements: byte i = 100; long k = i*3+4; double d = i*3.1+k/2; int x = k; //(Wrong) long k = x; //(fine,implicit casting)

 2003 Prentice Hall, Inc. All rights reserved (Modified). 50 Type Casting double float long int short byte

 2003 Prentice Hall, Inc. All rights reserved (Modified). 51 Type Casting, cont. Implicit casting double d = 3; (type widening) Explicit casting int i = (int)3.0; (type narrowing) What is wrong?int x = 5/2.0;

 2003 Prentice Hall, Inc. All rights reserved (Modified). 52 Character Data Type char letter = 'A'; (ASCII) char numChar = '4'; (ASCII) char letter = '\u0041'; (Unicode) char numChar = '\u0034'; (Unicode) Special characters char tab = ‘\t’;

 2003 Prentice Hall, Inc. All rights reserved (Modified). 53 Appendix B: ASCII Character Set ASCII Character Set is a subset of the Unicode from \u0000 to \u007f

 2003 Prentice Hall, Inc. All rights reserved (Modified). 54 ASCII Character Set, cont. ASCII Character Set is a subset of the Unicode from \u0000 to \u007f

 2003 Prentice Hall, Inc. All rights reserved (Modified). 55 Casting between char and Numeric Types int i = ' a ' ; // Same as int i = (int) ' a ' ; char c = 97; // Same as char c = (char)97;

 2003 Prentice Hall, Inc. All rights reserved (Modified). 56 The boolean Type and Operators boolean lightsOn = true; boolean lightsOn = false; boolean b = (1 > 2); && (and) (1 < x) && (x < 100) || (or) (lightsOn) || (isDayTime) ! (not)!(isStopped)

 2003 Prentice Hall, Inc. All rights reserved (Modified). 57 Comparison Operators Operator Name < less than <= less than or equal to > greater than >= greater than or equal to == equal to != not equal to

 2003 Prentice Hall, Inc. All rights reserved (Modified). 58 Boolean Operators Operator Name ! not && and || or ^ exclusive or

 2003 Prentice Hall, Inc. All rights reserved (Modified). 59 Truth Table for Operator ! Operand!Operand true false false true

 2003 Prentice Hall, Inc. All rights reserved (Modified). 60 Truth Table for Operator && Operand1Operand2Operand1 && Operand2 falsefalsefalse falsetruefalse truefalsefalse truetruetrue

 2003 Prentice Hall, Inc. All rights reserved (Modified). 61 Truth Table for Operator || Operand1Operand2Operand1 || Operand2 falsefalsefalse falsetruetrue truefalsetrue truetruetrue

 2003 Prentice Hall, Inc. All rights reserved (Modified). 62 Truth Table for Operator ^ Operand1Operand2Operand1 ^ Operand2 falsefalsefalse falsetruetrue truefalsetrue truetruefalse

 2003 Prentice Hall, Inc. All rights reserved (Modified). 63 The & and | Operators &&: conditional AND operator &: unconditional AND operator ||: conditional OR operator |: unconditional OR operator exp1 && exp2 (1 < x) && (x < 100) (1 < x) & (x < 100)

 2003 Prentice Hall, Inc. All rights reserved (Modified). 64 The & and | Operators If x is 1, what is x after this expression? (x > 1) & (x++ < 10) If x is 1, what is x after this expression? (1 > x) && ( 1 > x++) How about (1 == x) | (10 > x++)? (1 == x) || (10 > x++)?

 2003 Prentice Hall, Inc. All rights reserved (Modified). 65 Operator Precedence How to evaluate * 4 > 5 * (4 + 3) - ++i

 2003 Prentice Hall, Inc. All rights reserved (Modified). 66 Operator Precedence var++, var-- +, - (Unary plus and minus), ++var, --var (type) Casting ! (Not) *, /, % (Multiplication, division, and modulus) +, - (Binary addition and subtraction), >= (Comparison) ==, !=; (Equality) & (Unconditional AND) ^ (Exclusive OR) | (Unconditional OR) && (Conditional AND) Short-circuit AND || (Conditional OR) Short-circuit OR =, +=, -=, *=, /=, %= (Assignment operator)