 Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Logic & program control part 2: Simple selection structures.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Decision Structures Chapter 4. Chapter 4 Objectives To understand: o What values can be stored in a Boolean variable o What sequence structures are and.
This section gives several examples of if statements. In this first example, if a certain condition holds true, the single line of code immediately following.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
* Calls to methods may have implicit parameters. * Explicit parameters may not be needed because the method has the implicit parameter to work with. *
C++ for Engineers and Scientists Third Edition
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Java Unit 9: Arrays Declaring and Processing Arrays.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
4.1 Instance Variables, Constructors, and Methods.
Unit 2 – Week 4 Reasoning with Linear Equations and Inequalities Lesson 1.
Chapter 3 Making Decisions
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Chapter 4 Introduction to Control Statements Section 1 - Additional Java Operators Section 2 - If Statements Section 3 - If-Else Statements Section 4.
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Bordoloi and Bock CONTROL STRUCTURES: CONDITIONAL CONTROLS.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Summer Computing Workshop. Session 3 Conditional Branching  Conditional branching is used to alter the normal flow of execution depending on the value.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
Controlling Program Flow with Decision Structures.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Java Programming Fifth Edition
CNG 140 C Programming (Lecture set 3)
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 3: Decisions and Loops
EGR 2261 Unit 4 Control Structures I: Selection
Debugging and Random Numbers
The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Decision Structures and Boolean Logic
Selection By Ramin && Taimoor
Making Decisions in a Program
Topics The if Statement The if-else Statement Comparing Strings
IF if (condition) { Process… }
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Java Programming Loops
Chapter 4: Decision Structures and Boolean Logic
The System.exit() Method
Lecture 6: Conditionals AP Computer Science Principles
Boolean Expressions to Make Comparisons
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Chapter 4: Decision Structures and Boolean Logic
Controlling Program Flow
Presentation transcript:

 Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold true.  It is also possible to devise logical expressions which have the meaning of exclusive or, which means one or the other, but not both hold true.  Parentheses can be used in logical expressions to clarify the grouping of different conditions.  Once the logical operators are available it becomes possible to construct complex conditions.

 if(condition1)  {  if(condition2)  {  statement1;  }  else  {  statement2;  }  else  {  if(condition3)  {  statement3;  }  else  {  statement4;  }  statement1 is executed if condition1 is true and condition2 is true.  statement2 is executed if condition1 is true and condition2 is not true.  statement3 is executed if condition1 is not true and condition3 is true.  statement4 is executed if condition1 is not true and condition3 is not true.  The combination of conditions for the previous example is repeated here:

 The code given below, using the logical operators, is equivalent to the code given previously, using nested if s:  if(condition1 && condition2)  {  statement1;  }  else if(condition1 && !condition2)  {  statement2;  }  else if(!condition1 && condition3)  {  statement3;  }  else if(!condition1 && !condition3)  {  statement4;  }  else  {  }  statement1 is executed if condition1 is true and condition2 is true.  statement2 is executed if condition1 is true and condition2 is not true.  statement3 is executed if condition1 is not true and condition3 is true.  statement4 is executed if condition1 is not true and condition3 is not true.

 It is the programmer’s choice whether to use nested if s or logical operators.  It is possible to construct arbitrarily complex logical expressions.  Depending on the variables at hand and their meanings, you may have statements involving more than one numerical variable.  They could take the following form:  if(x 12)…

 You may also have comparisons involving a single numerical variable that bring up properties of number lines from algebra. Here are four such examples:  if(x 12) Never true:  A value can never be less than 5 and more than 12 at the same time. The statement after this if statement will never be executed in any case.

 if(x 12)  True for values outside of the range from 5 to 12:  if(x > 5 && x < 12)  True for values inside the range from 5 to 12

 if(x > 5 || x < 12) Always true:

 When using if statements, execution of certain blocks of code depends on the truth value of conditions such as numeric comparisons of equality and inequality.  Depending on the values of variables, each condition has a value of true or false.

 Java has a simple type called boolean that can contain truth values.  true and false are keywords designating the two values a boolean variable can contain.  Here is an example of declaration and assignment:  boolean someResult;  someResult = true;  someResult = false;

 The next question to consider is that of notifying the user when one of several possible actions is taken by a method.  The way to do this is by giving the method a type and a return value that signifies which action was taken.  For a method where there are essentially two alternatives, either some action or no action, a boolean return value is a reasonable choice.

 The increaseSeedCount() method is rewritten below to return a value indicating whether the seedCount was increased or not.  The method is now typed boolean and one of the two boolean values is returned at the end of the two different execution paths through the method.  public boolean increaseSeedCount(int addedNumber)  {  if(addedNumber > 0)  {  seedCount = seedCount + addedNumber;  return true;  }  else  return false;  }

 In code that makes use of the method, a call would take the form illustrated in the following fragment:  boolean callResult;  int moreSeeds = 7;  Cup4 myCup = new Cup4(5);  callResult = myCup.increaseSeedCount(moreSeeds);

 After the call it would be possible to take certain actions in the program depending on whether the return value was true or false.  The structure of such actions might be further if statements.  Note that when using boolean values in if conditions, it is not necessary to use a comparison symbol to test equality.

 It is sufficient to use a variable containing the truth value by itself.  For example, a line of code like that shown below could follow the code shown above.  The variable callResult alone could serve as the contents of an if condition:  if(callResult)  …

 It is also possible to write code in the form shown below.  This is not recommended for beginning programmers because it can be difficult to read.  The call to increase the seedCount is embedded in the parentheses of the if statement.  When the call is made, a truth value is returned.  The action of the if statement depends on this return value, which is unnamed and “invisible”.  if(myCup.increaseSeedCount(moreSeeds))  …

 It is true in general that it is not necessary to capture the return value.  There may be cases where you aren’t interested in the return value.  If so, it can be ignored and this is not a syntactical error.  For example, this is a valid line of code:  myCup.increaseSeedCount(moreSeeds);

 Notice that in the increaseSeedCount() method code there were two return statements.  They were in the two mutually exclusive alternatives of an if statement.  Whenever a return statement is encountered this ends the execution of the method.  It is possible to consolidate such a situation by assigning a return value to a variable and returning the variable at the bottom of the method.

 Here is the method modified in that way:  public boolean increaseSeedCount(int addedNumber)  {  boolean returnValue;   if(addedNumber > 0)  {  seedCount = seedCount + addedNumber;  returnValue = true;  }  else  returnValue = false;   return returnValue;  }

 This code is neither better nor worse than the previous version in an absolute sense.  Conceptually, it does illustrate the fact that you can ultimately only return once out of any method.  Also, this might be useful in the case where you don’t want to return right away.  For instance, you may want to decide to return true or false, then do some other functionality before returning.