Conditionals & boolean operators

Slides:



Advertisements
Similar presentations
Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Chapter 4: Control Structures I (Selection)
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
1 Fall 2007ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
Chapter 6 Horstmann Programs that make decisions: the IF command.
1 Fall 2008ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.
2-1 Making Decisions Sample assignment statements PayAmount = Hours * Rate PayAmount = 40*Rate + (Hours – 40)*Rate*1.5.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
C++ for Engineers and Scientists Third Edition
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
An Introduction to Programming Using Alice Boolean Logic.
Problem Solving with Decisions
Flow of Control Part 1: Selection
Simple Control Structures IF, IF-ELSE statements in C.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
ITM 352 Flow-Control: if and switch. ITM © Port, KazmanFlow-Control - 2 What is "Flow of Control"? Flow of Control is the execution order of instructions.
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Copyright 2003 Scott/Jones Publishing Making Decisions.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures.
Lesson Two: Everything You Need to Know
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
What is a Boolean expression?
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
Lesson thirteen Conditional Statement "if- else" ©
TestScore < 80 testScore * 2 >= < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Random Functions Selection Structure Comparison Operators Logical Operator
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 4: Control Structures I
Chapter 4: Making Decisions.
Chapter 4: Control Structures I
If statement.
2-1 Making Decisions Sample assignment statements
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Chapter 4: Control Structures I (Selection)
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Presentation transcript:

Conditionals & boolean operators Chapter 3

Condition Statement A conditional statement is sometimes called a selection statement. Each decision is based on a boolean expression called a condition.

Relational Operators boolean expression use relational operators to make a decision

Logical Comparison Operators Six different comparison operators are used in mathematics and computer programming. Condition In Math In Programming A equals B A = B A = B or A == B A is not equal to B A  B A != B A is less than B A < B A is greater than B A > B A is less than or equal to B A  B A <= B A is greater than or equal to B A  B A >= B

Logical Conditions Logical comparisons that are either true or false are most often used as the basis for the true and false values in Boolean logic. They are often used for simple conditions in branching and looping instructions. if (hours > 40) pay overtime if (age < 12) stay in the back seat while (count  10) print count increment count

100th Anniversary Edition Boolean Logic Boolean logic is a form of mathematics in which the only values used are true and false. Boolean logic is the basis of all modern computing. There are three basic operations in Boolean logic – AND, OR, and NOT. 100th Anniversary Edition

boolean Logic Operators Sometimes 2 conditions need to be checked. Compound boolean expression use logic operators in boolean expressions. && AND || OR ! Not answer = (x > 7 && x == 9) Both conditions must be true answer = (x > 7 || x == 9) Only one condition needs to be true answer = !(x > 7 || x == 9) Not reverses the result (opposite)

Compound Conditions Compound Boolean conditions can be created using the Boolean AND, OR and NOT operations in branching and looping instructions. if ( (hours > 40) && (type = hourly) ) pay overtime if ( (age < 12) || (height < 42 in.) ) stay in the back seat while ( (count <= 10) && ! (status = away) ) print name.count increment count

Writing boolean statements with && AND And operator will be true only if both expressions evaluate to true. a b a && b true false

Writing boolean statements with && AND int x = 2 int y = 90 (x < 10 && y < 97) (x > 10 && y < 97) (If one were false the whole thing would be false.) Condition would produce True T T Condition would produce False False True

Writing an or || boolean statement: The outcome will be true as long as one of the expressions evaluates to true. a b a || b true false

Boolean Operators int x = 2 int y = 90 Writing an or || boolean statement: (x < 10 || y < 97) (x > 10 || y < 97) Condition would produce True True True Condition would produce True False True

Boolean Operators Not ! It reverses the value of a boolean expression outcome True False

Boolean Operators Not ! int x = 2 int y = 90 Writing an && with ! boolean statement: (!(2 < 10) && (90 < 97)) !((2 < 10) && (90 < 97)) Reverses the whole things after evaluated. Condition would produce false (!(2 > 10) && (90 < 97)) Condition would produce False !True True True True !True Condition would produce True !False True

Writing Boolean Statements You must write the full condition when using logic operators ): x > y > z x and y are both less than 0 neither x nor y is less than 0 x is equal to y but not equal to z (x>y && y > z); (x<0 && y<0); !(x<0 && y<0); (!(x<0) && (!y<0)); ((x==y) && (!x==z));

Operator precedence Operator Associativity * / % left to right + - < <= > >= == != && (and) || (or) = += -= *= /= right to left

if, if-else, if-else-if-else Statements The if-else class of statements should have the following form if (condition) { statements; } else if (condition) { if (condition) { statements; } else { if (condition) { statements; }

Conditional Statements Programming style Note that if there is only a single statement in the if or else block, curly brackets are not needed. If there is more than one statement in one of these blocks, the curly brackets are required. if (boolean condition) statement; else if (boolean condition) { statement; } else { Curly brackets optional Curly brackets required

Conditional Statements Nested if statements If statements will execute every if that is true public void grade(int testScore) { if (testScore >= 90) System.out.println("Your grade is A"); if (testScore >= 80) System.out.println("Your grade is B"); if (testScore >= 70) System.out.println("Your grade is C"); else System.out.println("Your grade is F"); } testScore = 90 Your grade is A Your grade is B Your grade is C

Conditional Statements Nested if statements When you use the if else structure, it will stop and execute the first if else that is true. public void grade2(int testScore) { if (testScore >= 90){ System.out.println("Your grade is A"); } else if (testScore >= 80 ){ System.out.println("Your grade is B");} else if (testScore >= 70 ){ System.out.println("Your grade is C"); } else{ System.out.println("Your grade is F"); } } testScore = 90 Your grade is A

No braces? if (condition) //AVOID! THIS OMITS THE BRACES {}! statement; If you do not use braces the first statement after the if condition is the only one that goes with it.

Need braces public void grade2(int testScore) { if (testScore >= 90) System.out.println("Your grade is A"); System.out.println("First if statement"); if (testScore >= 80) System.out.println("Your grade is B"); System.out.println("Second if statement"); if (testScore >= 70) System.out.println("Your grade is C"); System.out.println("Third if statement"); if(testScore < 70) System.out.println("Your grade is F"); System.out.println("Last if statement"); } testScore = 90; Your grade is A First if statement Your grade is B Second if statement Your grade is C Third if statement Last if statement

What prints int i=3; if(i<10) System.out.println("hello"); System.out.println("goodbye");  

What prints int i=3, j=14; if(i<10) System.out.println("hello"); if(j>10) System.out.println("howdy"); System.out.println("goodbye");

common errors If(total = 25); { } if(total == 10) Do not capitalize if Use the == to show equality