Starting Out With Java 5 (Control Structures to Objects) Chapter 3

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
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.
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.
The if Statement The code in methods executes sequentially.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
© 2012 Pearson Education, Inc. All rights reserved. Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Data Structures.
Lecture 3 Decision Structures. 4-2 Topics – The if Statement – The if - else Statement – The PayRoll class – Nested if Statements – The if - else - if.
Chapter 3 Decision Structures Starting Out with Java: From Control Structures through Objects.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2013.
Control Stuctures Module 4 Copyright © Copyright © Slide #2 Decision Structures Chapter Objectives To understand: how to write boolean expressions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 3: Decision Structures
CHAPTER 3 Decision Structures Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 4 Slide #1.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
Starting Out With Java 5 (Control Structures to Objects) Chapter 3 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005 Pearson Addison- Wesley. All rights reserved. Chapter 1 Slide #1.
CHAPTER 3 Decision Structures Copyright © 2016 Pearson Education, Ltd.
INTERMEDIATE PROGRAMMING USING JAVA
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
CS0007: Introduction to Computer Programming
Chapter 3: Decision Structures by Tony Gaddis and Godfrey Muganda
Chapter 4: Making Decisions.
Chapter 3: Decision Structures
WELCOME to….. The If Statement.
Lecture 3- Decision Structures
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Chapter 3: Decision Structures
if-else-if Statements
all loops initialization – set up the loop
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Chapter 3: Decision Structures
Chapter 17 Linked Lists.
Chapter 19 Binary Search Trees.
Chapter 4 Inheritance.
Chapter 14 Graphs and Paths.
Selection CSCE 121 J. Michael Moore.
Chapter 3:Decision Structures
Chapter 10 Datapath Subsystems.
CMSC 202 Java Primer 2.
Chapter 20 Hash Tables.
The System.exit() Method
Logic of an if statement
The Facts to Be Explained
Introduction: Some Representative Problems
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Circuit Characterization and Performance Estimation
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
Controlling Program Flow
Chapter 2 Part 1 Data and Expressions.
Chapter 3 Flow of Control Loops in Java.
Chapter 2 Reference Types.
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
CSS 161: Fundamentals of Computing
Chapter 4 Greedy Algorithms.
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

Starting Out With Java 5 (Control Structures to Objects) Chapter 3 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

Flowcharts If statements can be modeled as a flow chart. Wear a coat. Yes Is it cold outside? if (coldOutside) wearCoat(); Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

Flowcharts A block if statement may be modeled as: if (coldOutside){ Yes Is it cold outside? if (coldOutside){ wearCoat(); wearHat(); wearGloves(); } Wear a coat. Wear a hat. Wear gloves. Note the use of curly braces to block several statements together. Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

Relational Operators In most cases, the boolean expression, used by the if statement, uses relational operators. Relational Operator Meaning > is greater than < is less than >= is greater than or equal to <= is less than or equal to == is equal to != is not equal to Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

Boolean Expressions A boolean expression is any variable or calculation that results in a true or false condition. Expression Meaning x > y Is x greater than y? x < y Is x less than y? x >= y Is x greater than or equal to y? x <= y Is x less than or equal to y. x == y Is x equal to y? x != y Is x not equal to y? Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

If Statements and Boolean Expressions if (x > y) System.out.println(“X is greater than Y”); if(x == y) System.out.println(“X is equal to Y”); if(x != y) { System.out.println(“X is not equal to Y”); x = y; System.out.println(“However, now it is.”); } Example: AverageScore.java Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

Programming Style and if Statements If statements can span more than one line; however, they are still one statement. if(average > 95) System.out.println(“That’s a great score!”); is functionally equivalent to if(average > 95) System.out.println(“That’s a great score!”); Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

Programming Style and if Statements Rules of thumb: The conditionally executed statement should be on the line after the if condition. The conditionally executed statement should be indented one level from the if condition. If an if statement does not have the block curly braces, it is ended by the first semicolon encountered after the if condition. if(expression) statement; No semicolon here. Semicolon ends statement here. Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

Block if Statements Conditionally executed statements can be grouped into a block by using curly braces {} to enclose them. If curly braces are used to group conditionally executed statements, the if statement is ended by the closing curly brace. if(expression) { statement1; statement2; } Curly brace ends the statement. Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

Only this statement is conditionally executed. Block if Statements Remember that if the curly braces are not used, then only the next statement after the if condition will be executed conditionally. if(expression) statement1; statement2; statement3; Only this statement is conditionally executed. Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

Comparing Characters Characters can be tested using the relational operators. Characters are stored in the computer using the Unicode character format. Unicode is stored as a sixteen (16) bit number. Characters are ordinal, meaning they have an order in the Unicode character set. Since characters are ordinal, they can be compared to each other. char c = ‘A’; if(c < ‘Z’) System.out.println(“A is less than Z); Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

if-else Statements The if-else statement adds the ability to conditionally execute code based if the expression of the if statement is false. if(expression) statementOrBlockIfTrue; else statementOrBlockIfFalse; Example: Division.java Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

if-else Statement Flowcharts Wear a coat. Yes Is it cold outside? Wear shorts. No Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis