Logical OperatorstMyn1 Logical Operators Using logical operators, we can combine a series of comparisons into a single expression. if(letter>=‘A’ && letter<=‘Z’)

Slides:



Advertisements
Similar presentations
CSCI1402: Lecture 1 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
Advertisements

Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Some basic I/O.
Logical Operators and Conditional statements
CSCI1402: Lecture 2 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
OperatorstMyn1 Operators The sequence in which different operators in an expression are executed is determined by the precedence of the operators. Operators.
Computer Science Selection Structures.
Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a.
Logical Operators Jumps Logical Operators The different logical operators found in Java Rational Operators The different rational operators found in Java.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
1 Operators and Expressions Instructor: Mainak Chaudhuri
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
The if StatementtMyn1 The if Statement The basic if statement allows your program to execute a single statement, or a block of statements enclosed between.
1 Programming in C++ Dale/Weems/Headington Chapter 5 Conditions, Logical Expressions.
3: Controlling Program Flow Using Java operators Mathematical operators Relational operators Logical operators –Primitive type: ALL (the same with C) –String:
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
1 cuberoot /* Solve f(x) = x*x*x-5 = 0 f'(x) = 3x^2 */ public class cuberoot{ public static void main(String args[]){ double error= ; double x0.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
Floating Point ValuestMyn1 Floating Point Values Internally, floating point numbers have three pairs: a sign (positive or negative), a mantissa (has a.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Programming Principles Operators and Expressions.
Another example of input import java.io.*; //import java.lang.Integer.*; class ExampleofInput2 { private static BufferedReader stdin = new BufferedReader(
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Lazy Evaluation Computer Science 3 Gerb Objective: Understand lazy evaluation in Java.
August 6, Operators. August 6, Arithmetic Operators.
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.
The if-else StatementtMyn1 The if-else Statement It might be that we want to execute a particular statement or block of statements only when the condition.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
CS0007: Introduction to Computer Programming
CompSci 230 S Programming Techniques
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
The Lifetime of a Variable
Section 7.1 Logical Operators
Lecture 2: Data Types, Variables, Operators, and Expressions
Chapter 6 More Conditionals and Loops
Building Java Programs Chapter 4
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Building Java Programs
Lecture 4: Conditionals
if-else-if Statements
Operators August 6, 2009.
Fundamentals 2.
Building Java Programs
CSE 214 – Computer Science II Stack Applications
Recursive GCD Demo public class Euclid {
class PrintOnetoTen { public static void main(String args[]) {
Java Programming with Multiple Classes
Building Java Programs
Building Java Programs
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 4 Lecture 4-1: Scanner; if/else reading: 3.3 – 3.4, 4.1, 4.5
Factoring if/else code
Building Java Programs
Presentation transcript:

Logical OperatorstMyn1 Logical Operators Using logical operators, we can combine a series of comparisons into a single expression. if(letter>=‘A’ && letter<=‘Z’) System.out.println(“This is an upper case letter”); if(income>= || capital>= ) System.out.println(“Why do you want to borrow?”);

Logical OperatorstMyn2 The logical operators OperatorMeaning &AND |OR ^XOR(exclusive OR) ||Short-circuit OR &&Short-circuit AND !NOT

Logical OperatorstMyn3 Let’s demonstrate the difference between the Short- circuit AND (&&) and the normal AND (&). In the first example with the normal AND operator both expressions are evaluated, allowing a division by zero to occur:

Logical OperatorstMyn4 package logical; public class Main { public static void main(String[] args) { int n=10, d=0; if(d!=0 & (n%d)==0) System.out.println(d+" is a factor of "+n); } run: Exception in thread "main" java.lang.ArithmeticException: / by zero at logical.Main.main(Main.java:8) Java Result: 1 BUILD SUCCESSFUL (total time: 1 second)

Logical OperatorstMyn5 In the next example with the short-circuit AND a division by zero has been prevented:

Logical OperatorstMyn6 package logicalshortcircuit; public class Main { public static void main(String[] args) { int n=10, d=0; if(d!=0 && (n%d)==0) System.out.println(d+" is a factor of "+n); } run: BUILD SUCCESSFUL (total time: 0 seconds)

Logical OperatorstMyn7 Next example demonstrates the usage of the NOT operator:

Logical OperatorstMyn8 package logical1; public class Main { public static void main(String[] args) { int num1=15, num2=16; if(!(num1==num2)) System.out.println("The numbers are not of equal size."); else System.out.println("The numbers were of equal size."); } run: The numbers are not of equal size. BUILD SUCCESSFUL (total time: 0 seconds)

Logical OperatorstMyn9 Exclusive OR operator is only seldom (?) seen:

Logical OperatorstMyn10 package logical2; public class Main { public static void main(String[] args) { int num1=12, num2=13; if(num1==12 ^ num2==14) System.out.println("Yes, exactly ONE and ONLY ONE " + "operand was TRUE."); } run: Yes, exactly ONE and ONLY ONE operand was TRUE. BUILD SUCCESSFUL (total time: 0 seconds)