Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
3. S/E with Control Structures 3.1 Relational Operators and Expressions 3.2 If and if-else Statements 3.3 The Type Double 3.4 Program Design with the While.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
BUILDING JAVA PROGRAMS CHAPTER 4 Conditional Execution.
Copyright 2010 by Pearson Education Building Java Programs Chapter 4 Lecture 4-2: Advanced if/else ; Cumulative sum reading: 4.1, 4.3, 4.5; "Procedural.
1 BUILDING JAVA PROGRAMS CHAPTER 4 CONDITIONAL EXECUTION.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: Scanner ; if/else reading: , 4.2, 4.6.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Control Structures: Selection.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
CS305j Introduction to Computing Conditional Execution 1 Topic 12 Conditional Execution "We flew down weekly to meet with IBM, but they thought the way.
Topic 11 Scanner object, conditional execution Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Conditionals (Cont’d). 2 Nested if/else question Formula for body mass index (BMI): Write a program that produces output like the following: This program.
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Building Java Programs
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
1 Building Java Programs Chapter 5 Lecture 5-3: Boolean Logic and Assertions reading: 5.3 – 5.5.
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.
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
1 CSE 142 Lecture Notes Conditional Execution with if Statements; Methods that Return Values (briefly) Chapters 3 and 4 Suggested reading: ;
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CS305j Introduction to Computing More Conditional Execution 1 Topic 13 More Conditional Execution " Great dancers are not great because of their technique;
Copyright 2008 by Pearson Education 1 The if statement Executes a block of statements only if a test is true if ( test ) { statement ;... statement ; }
Copyright 2010 by Pearson Education The if/else statement reading: 4.1, 4.6.
Building Java Programs
Chapter 4 Repetition Statements (loops)
Building Java Programs
Building Java Programs Chapter 4
Lecture 4: Program Control Flow
Factoring if/else code
Building Java Programs
Lecture 4: Conditionals
Topic 11 Scanner object, conditional execution
Building Java Programs
Type boolean boolean: A logical type whose values are true and false.
Building Java Programs
Building Java Programs
Executes a block of statements only if a test is true
Lecture Notes – Week 3 Lecture-2
Building Java Programs
CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 6: Conditionals AP Computer Science Principles
Building Java Programs
Building Java Programs
Building Java Programs
Logic of an if statement
Chapter 2 Programming Basics.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 4
Building Java Programs
Factoring if/else code
Building Java Programs
Building Java Programs
Presentation transcript:

Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises: #7 videos: Ch. 4 #2-4

Copyright 2008 by Pearson Education 2 The if statement Executes a block of statements only if a test is true if ( test ) { statement ;... statement ; } Example: double gpa = console.nextDouble(); if (gpa >= 2.0) { System.out.println("Application accepted."); }

Copyright 2008 by Pearson Education 3 The if/else statement Executes one block if a test is true, another if false if ( test ) { statement(s) ; } else { statement(s) ; } Example: double gpa = console.nextDouble(); if (gpa >= 2.0) { System.out.println("Welcome to Mars University!"); } else { System.out.println("Application denied."); }

Copyright 2008 by Pearson Education 4 Relational expressions A test in an if is the same as in a for loop. for (int i = 1; i <= 10; i++) {... if (i <= 10) {... These are boolean expressions, seen in Ch. 5. Tests use relational operators: Opera tor MeaningExampl e Val ue == equals == 2 tru e != does not equal 3.2 != 2.5 tru e < less than 10 < 5fal se > greater than 10 > 5tru e <= less than or equal to 126 <= 100 fal se >= greater than or equal to 5.0 >= 5.0 tru e

Copyright 2008 by Pearson Education 5 Logical operators: &&, ||, ! Conditions can be combined using logical operators: "Truth tables" for each, used with logical values p and q: OperatorDescriptionExampleResult && and (2 == 3) && (-1 < 5)false || or (2 == 3) || (-1 < 5)true ! not !(2 == 3)true pqp && qp || q true false true falsetruefalsetrue false p !p!p truefalse true

Copyright 2008 by Pearson Education 6 Evaluating logic expressions Relational operators have lower precedence than math. 5 * 7 >= * (7 - 1) 5 * 7 >= * 6 35 >= >= 33 true Relational operators cannot be "chained" as in algebra. 2 <= x <= 10 (assume that x is 15 ) true <= 10 error! Instead, combine multiple tests with && or || 2 <= x && x <= 10 (assume that x is 15 ) true && false false

Copyright 2008 by Pearson Education 7 Logical questions What is the result of each of the following expressions? int x = 42; int y = 17; int z = 25; y < x && y <= z x % 2 == y % 2 || x % 2 == z % 2 x = y + z !(x < y && x < z) (x + y) % 2 == 0 || !((z - y) % 2 == 0)