Decision Statements, Short- Circuit Evaluation, Errors.

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements.
Advertisements

Chapter 4: Making Decisions.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
true (any other value but zero) false (zero) expression Statement 2
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Logical Operators and Conditional statements
C++ for Engineers and Scientists Third Edition
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Chapter 4 The If…Then Statement
Decision Structures and Boolean Logic
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
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.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
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.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Control statements Mostafa Abdallah
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
CPS120: Introduction to Computer Science Decision Making in Programs.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
© 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  Conditional control structure, also called a decision structure  Executes.
Java Programming Fifth Edition
The if…else Selection Statement
Chapter 5 The if Statement
Chapter 4 The If…Then Statement
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Lecture 3- Decision Structures
EGR 2261 Unit 4 Control Structures I: Selection
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions and If
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
Control Structures.
Topics The if Statement The if-else Statement Comparing Strings
IF if (condition) { Process… }
CprE 185: Intro to Problem Solving (using C)
Presentation transcript:

Decision Statements, Short- Circuit Evaluation, Errors

Goals and Objectives Relational Operators Equality Operators Logical Operators AND, OR, NOT Truth Table. DeMorgan Law Compound Boolean Expression. Short-Circuit Evaluation. Simple if statement if-else statement Dangling Else Nested if statement Extended if statement switch-case statement Flowcharting

Relational Operators OperatorMeaning == equal < less than <= less than or equal > greater than >= greater than or equal != not equal

Equality Operators Test if two values are equal (==) or not equal (!=)  ==  != Note the equality operator is two equal signs side by side and should not be mistaken for the assignment operator.

Logical Operators  Produce Boolean results and used for compound expressions  ! Not  && AND  || OR

And Truth Table And && Exp1Exp2Result True False TrueFalse

Or Truth Table Or || Exp1Exp2Result True FalseTrue FalseTrue False

Not Truth Table Not ! ExpResult TrueFalse True

DeMorgan’s Law  !(p && q) is the same as !p || !q  !(p || q) is the same as !p && !q

Evaluating expressions with NOT To evaluate expression that has a NOT operator, you need to make the expression mean the exact opposite of the original expression. Example: !(x 4 !( y > 5)  y <= 5 !(x == 3)  x != 3

Compound Boolean Expressions  More than one Boolean expression in a single condition.  Formed using the logical And ( && ), logical Or ( || ), or logical Not ( ! ) operators.

Short-Circuit Evaluation Can occur during and && or || compound boolean expressions. expression1 && expression2 –For an AND operator, if expression1 evaluates to false there is no need to check expression2 because the compound expression will automatically be False. expression1 || expression2 –For an OR operator, if expression1 evaluates to true there is no need to check expression2 because the compound expression will automatically be True.

Examples of Short-Circuit Evaluation Example: x= 2  (x > 3 && x <10) x > 3 will evaluate to FALSE No need to check x<10 because the compound expression evaluates to false by the 1 st expression. Example: x = 2  (x > 1 || x < 10) –x > 1 will evaluate to TRUE –No need to check x<10 because the compound expression evaluates to TRUE by the 1 st expression.

Short-Circuit Evaluation if( y != 0 && x/y > 3) –x/y will not be check if y = 0 thus not causing an error by dividing by 0.

Simple If Statement if(boolean expression or variable) statements; Braces { } are used when there are more than one statements. Example: if(boolean expression or variable) { statement; } If the boolean expression or variable is false the statements between the braces will be skipped.

 Conditional control structure, also called a decision structure  Executes a set of statements when a condition is true  The condition is a Boolean expression  For example, the statement if (x == 5) { y = 20; } assigns the value 20 to y only if x is equal to 5. The if Statement

Simple If Statements An if statement lets a program choose whether to execute a particular statement. Indentation is important for style and readability. It shows the relationship between one statement and another. Comparing objects – because objects point to a location in memory, rather than directly storing a value, they cannot be compared using built-in relational operators. Never try to compare floating points. Example double x, y; (x == y). Because of the possibility of roundoff error.

The if-else Statement Contains an else clause that is executed when the if condition evaluates to false. For example, the statement if (x == 5) { y = 20; } else { y = 10; } assigns the value 20 to y if x is equal to 5 or the value 10 if x is not equal to 5.

if-else Statement The if statement can include an optional else clause that is executed when the if condition evaluates to false. if (boolean expression or variable) { Statements; } else { Statements; } No braces are needed if there is just one statement.

If-else Statement An if-else statement tells a program to do one thing if the condition is true and another thing if the condition is false. Block Statements – is code enclosed between braces. Indent both body statements of an if-else statement for style and readability.

Dangling Else Occurs when an else follows two if statements. An else clause is always matched to the closest unmatched if that came before it. Use braces to correct problem.

Dangling Else Example if(condition1) Statement; if(condition2) Statement; else Statement; Which if statement does the else belongs to?

Dangling Else Example Correct way if(condition1) { Statement; if(condition2) Statement; } else Statement;

Nested if-else Statements  Should be indented to make the logic clear.  Nested statement executed only when the branch it is in is executed. For example, the statement if (x == 5) { y = 20; } else { if (x > 5) { y = 10; } else { y = 0; } } evaluates the nested if-else only when x is not equal to 5.

Nested If Statements Nested if statements are if statements inside other if statements. The dangling if slide earlier is an example of nested if statement. It is very important to use braces to prevent dangling else statements when creating nested if statements. Indentation is very important and makes it easy to read.

Extended If Statements Can replace multiple level of nested if statements. Can replace a series of if statements Make the program run faster

if-else if – else Example if(condition1) –Statement1; else if (condition2) –Statement2; else if(condition3) –Statement3; else –Statement4;

The if-else if Statement  Used to decide among three or more actions.  Conditions must be properly ordered for the statement to evaluate as expected. For example, the statement if (x < 5) { y = 20; } else if (x < 10) { y = 40; } else if (x < 15) { y = 80; } would give very different results if the conditions were ordered differently.

if-else If one of the if’s or else if’s evaluates to be true the rest are skipped. It is wise to put the condition that occurs the most at the beginning of the if-else if- else statements.

Switch-case statements The switch statement is a conditional control structures that uses the result of an expression to determine which statement to execute. switch(condition) { case variable: Statement; break; case variable: Statement; break; default: Statement; break }

The switch Statement  Used to decide among three or more actions.  Uses an expression that evaluates to an integer.  The break statement moves program execution to the next statement after the switch.  The default code is optional and is executed when none of the previous cases are met: switch (numLegs) { case 2: System.out.println("human"); break; case 4: System.out.println("beast"); break; case 8: System.out.println("insect"); break; default: System.out.println("???"); break; }

Switch-case Statement Break statement – is necessary to move program control to the next case. Default statement is optional Condition should be integer or character no floating point. Can have more than one case that represents a statement. (Next Slide)

Switch – case Statements switch(condition) { case variable: Statement; break; case variable: Statement; break; default: Statement; break }

Comparing Objects & Floating Points When comparing objects, you cannot use the (==) relational operator. You cannot compare floating points using (==) relational operator because it may cause a loss of precision error.

Common Errors Semicolon after if statement –if(condition); // will compile Missing Semicolon after statement if(condition) Statement // missing semicolon compiler error else Statement; Omitting Braces if(condition) Statement; // No braces for 2 statements Statement; Dangling Else

Flowchart Symbols decision

The RPS Flowchart