Boolean Expressions Conditional Statements & Expressions CSC 1401: Introduction to Programming with Java Lecture 4 – Part 2 Wanda M. Kunkle.

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
Boolean Types & Compound Conditionals CSC 1401: Introduction to Programming with Java Lecture 4 – Part 3 Wanda M. Kunkle.
Introduction to Computers and Programming Lecture 5 New York University.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle.
Chapter 4 Making Decisions
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 4 Making.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
UNIT II Decision Making And Branching Decision Making And Looping
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Introduction to Java Java Translation Program Structure
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
Java Programming Fifth Edition
- Standard C Statements
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Factoring if/else code
Chapter 4: Making Decisions.
Introduction To Robot Decision Making
Lecture 2: Logical Problems with Choices
Control Structures: Selection Statement
Introduction to Primitive Data types
Introduction To Robot Decision Making
SELECTIONS STATEMENTS
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Control Structures: Selection Statement
Dale Roberts, Lecturer IUPUI
Introduction to Primitive Data types
Presentation transcript:

Boolean Expressions Conditional Statements & Expressions CSC 1401: Introduction to Programming with Java Lecture 4 – Part 2 Wanda M. Kunkle

Boolean Expressions A boolean expression is an expression that evaluates to true or false. The word boolean is derived from the name of George Boole, a 19 th -century English logician and mathematician whose work was associated with expressions of this type.

Relational Operators Relational operators are used to form boolean expressions. Relational operators can be used to compare two values of the same primitive (i.e., simple) type, such as int or char. The relational operators we use in Java are the same as those we use in algebra, except that some of them look a little different.

Relational Operators Algebraic Operator Java Operator Java Expression Meaning of Java Expression >> num1 > num2 num1 is greater than num2 ≥>= num1 >= num2 num1 is greater than or equal to num2 << num1 < num2 num1 is less than num2 ≤<= num1 <= num2 num1 is less than or equal to num2 === num1 == num2 num1 is equal to num2 ≠!= num1 != num2 num1 is not equal to num2 For the examples given in the table below, assume that num1 and num2 are both integer ( int ) types.

Additional Examples of Boolean Expressions in Java Comparing integers int num1 = 5, num2 = 8, num3 = 13; num2 >= num1 // evaluates to true num1 == num3 // evaluates to false Comparing characters char letter1 = 'a', letter2 = ‘B’, letter3 = 'c'; letter1 < letter3 // evaluates to true letter1 < letter2 // evaluates to false (why?)

ASCII/Unicode Values for Letter and Digit Characters Character ASCII/Unicode Value '0' through '9' 48 through 57 'A' through 'Z' 65 through 90 'a' through 'z' 97 through 122

Question? Can anyone now explain why the second example comparing letters on slide 5 evaluates to false? Here it is again: char letter1 = 'a', letter2 = ‘B’; letter1 < letter2 // evaluates to false (why?)

Additional Examples of Boolean Expressions in Java Comparing strings Strings cannot be compared using the relational operators in Java (Aside: C++ yes, Java no). Strings cannot be compared using the relational operators in Java (Aside: C++ yes, Java no). Counter example: String myName = "Wanda", upperName = "Anne", lowerName = "anne"; myName == upperName // doesn’t work; actually // compares the locations of the // String objects in memory // instead of their contents Counter example: String myName = "Wanda", upperName = "Anne", lowerName = "anne"; myName == upperName // doesn’t work; actually // compares the locations of the // String objects in memory // instead of their contents

Additional Examples of Boolean Expressions in Java Comparing strings Strings can be compared using the methods implemented in the String class. Strings can be compared using the methods implemented in the String class. Example: String myName = "Wanda", upperName = "Anne", lowerName = "anne"; myName.equals(upperName) // evaluates to false upperName.equals(lowerName) // evaluates to false // (why?) upperName.equalsIgnoreCase(lowerName) // evaluates to true // (why?) Example: String myName = "Wanda", upperName = "Anne", lowerName = "anne"; myName.equals(upperName) // evaluates to false upperName.equals(lowerName) // evaluates to false // (why?) upperName.equalsIgnoreCase(lowerName) // evaluates to true // (why?)

Control Structures The Java programs we have written so far have consisted of statements that execute in the order in which they appear in our source code. The Java programs we have written so far have consisted of statements that execute in the order in which they appear in our source code. We will not always want every statement to execute, however. As a result, Java provides us with control, or decision, structures which enable us to control what statements in a program execute. These control structures can be divided into two general categories: Selection structures Selection structures Repetition structures Repetition structures

Selection Structures A selection structure is used to choose among alternative courses of action. There are several types: if if if-else if-else If-else-if If-else-if switch switch

Selection Structures if Usage: Usage: Used to test if a single boolean expression is true or false Statements are only executed if the boolean expression is true Note: Boolean expressions are also called conditions Note: Boolean expressions are also called conditions Format: if (Condition) { // Braces are only needed when there // are multiple statements Statement(s) } Format: if (Condition) { // Braces are only needed when there // are multiple statements Statement(s) } Example: float grade = 95; if (grade >= 90) out.writeln("You got an A!“); Example: float grade = 95; if (grade >= 90) out.writeln("You got an A!“);

Selection Structures if-else Usage: Usage: Used in either-or situations The statements executed depend on whether the condition is true or false Format: if (Condition) { // Braces are only needed when there // are multiple statements Statement(s) } else { Statement(s) } Format: if (Condition) { // Braces are only needed when there // are multiple statements Statement(s) } else { Statement(s) } Example: float grade = 85; if (grade >= 60) out.writeln("You passed! :-)"); else out.writeln("You didn't pass. :-("); Example: float grade = 85; if (grade >= 60) out.writeln("You passed! :-)"); else out.writeln("You didn't pass. :-(");

Selection Structures if-else-if Usage: Usage: Used in situations where there are multiple alternatives (more than two choices) The statements executed depend on which condition is true Format: if (Condition1) { // Braces are only needed when there // are multiple statements Statement(s) } else if (Condition2) { Statement(s) } else if (Condition3) { Statement(s) } else if … Format: if (Condition1) { // Braces are only needed when there // are multiple statements Statement(s) } else if (Condition2) { Statement(s) } else if (Condition3) { Statement(s) } else if …

Selection Structures if-else-if Example: float grade = 75; if (grade >= 90) out.writeln("You got an A!"); else if (grade >= 80) out.writeln("You got a B."); else if (grade >= 70) out.writeln("You got a C."); else if (grade >= 60) out.writeln("You got a D."); else out.writeln("You got an F."); Example: float grade = 75; if (grade >= 90) out.writeln("You got an A!"); else if (grade >= 80) out.writeln("You got a B."); else if (grade >= 70) out.writeln("You got a C."); else if (grade >= 60) out.writeln("You got a D."); else out.writeln("You got an F.");