Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

Control Structures Corresponds with Chapters 3 and 4.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
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 (!)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either.
1 Fall 2007ACS-1903 Chapter 3 Decision Structures Variable Scope The Conditional Operator The switch Statement DecimalFormat Class printf.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
C++ for Engineers and Scientists Third Edition
The switch Statement, DecimalFormat, and Introduction to Looping
© 2012 Pearson Education, Inc. All rights reserved. Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Data Structures.
Chapter 3 Decision Structures 1. Contents 1.The if Statement 2.The if-else Statement 3.The if-else-if Statement 4.Nested if Statement 5.Logical Operators.
Lecture 3 Decision Structures. 4-2 Topics – The if Statement – The if - else Statement – The PayRoll class – Nested if Statements – The if - else - if.
Starting Out with Java: From Control Structures through Objects
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 3 Decision Structures Starting Out with Java: From Control Structures through Objects.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Flow of Control Part 1: Selection
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2013.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Control Stuctures Module 4 Copyright © Copyright © Slide #2 Decision Structures Chapter Objectives To understand: how to write boolean expressions.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
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.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Control statements Mostafa Abdallah
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
JAVA PROGRAMMING Control Flow. Jeroo: Finish Activities 1-7 Finish InputTest program w/changes.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
CHAPTER 3 Decision Structures Copyright © 2016 Pearson Education, Ltd.
Chapter 4: Control Structures I
CNG 140 C Programming (Lecture set 3)
Chapter 3: Decision Structures by Tony Gaddis and Godfrey Muganda
Chapter 4: Control Structures I
Chapter 3: Decision Structures
The switch Statement, and Introduction to Looping
Lecture 3- Decision Structures
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Chapter 3: Decision Structures
Chapter 3: Decision Structures
Chapter 4: Control Structures I
Chapter 4: Loops and Files
Chapter 3:Decision Structures
The System.exit() Method
Seating “chart” Front Back 4 rows 5 rows 5 rows 4 rows 2 rows 2 rows
PROGRAM FLOWCHART Selection Statements.
Chap 7. Advanced Control Statements in Java
Presentation transcript:

Chapter 3:Decision Structures

 3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical Operators  3.6 Comparing String Objects  3.7 More About Variable Declaration and Scope  3.9 The switch Statement  3.10 Creating Objects with the DecimalFormat Class  3.12 Common Errors to Avoid

Variable Scope  In Java, a local variable does not have to be declared at the beginning of the method.  The scope of a local variable begins at the point it is declared and terminates at the end of the method.  When a program enters a section of code where a variable has scope, that variable has come into scope, which means the variable is visible to the program.

VariableScope.java // Get the user's first name. String firstName; firstName = JOptionPane.showInputDialog( "Enter your " + "first name."); String lastName; lastName = JOptionPane.showInputDialog( "Enter your " + "last name."); JOptionPane.showMessageDialog(null, "Hello " + firstName + " " + lastName);

The Conditional Operator  The conditional operator is a ternary (three operand) operator.  The conditional operator allows a programmer to write a simple if-else type statement.  The format of the operators is: expression1 ? expression2 : expression3  The conditional operator can also return a value.

The Conditional Operator  The conditional operator can be used as a shortened if-else statement: x > y ? z = 10 : z = 5;  This line is functionally equivalent to: if(x > ) z = 10; else z = 5;

The Conditional Operator  Many times, the conditional operator is used to supply a value. number = x > y ? 10 : 5;  This is functionally equivalent to: if(x > y) number = 10; else number = 5;

ConsultantCharges.java double hours, // To hold the hours worked charges; // To hold the charges String input; // To hold user input input = JOptionPane.showInputDialog( "How many hours were worked? "); hours = Double.parseDouble(input); hours = hours < 5 ? 5 : hours; charges = 50.0 * hours; JOptionPane.showMessageDialog(null, "The charges are $" + charges);

The switch Statement  The if-else statements allow the programmer to make true / false branches.  The switch statement allows the programmer to use an ordinal value to determine how a program will branch.  The switch statement can evaluate an integer type or character type variable and make decisions based on the value.

The switch Statement  The switch statement takes the form: switch (SwitchExpression) { case CaseExpression: // place one or more statements here break; case CaseExpression: // place one or more statements here break; default: // place one or more statements here }

The switch Statement  The switch statement takes an ordinal value (byte, short, int, long, char) as the SwitchExpression. switch (SwitchExpression) { … }  The switch statement will evaluate the expression.  If there is an associated case statement that matches that value, program execution will be transferred to that case statement.

The switch Statement  Each case statement will have a corresponding CaseExpression that must be unique. case CaseExpression: // place one or more statements here break;  If the SwitchExpression matches the CaseExpression, the Java statements between the colon and the break statement will be executed.

The switch Case  The break statement ends the case statement.  The break statement is optional.  If a case does not contain a break, then program execution continues into the next case.

NoBreaks.java switch (number) { case 1: System.out.println("You entered 1."); case 2: System.out.println("You entered 2."); case 3: System.out.println("You entered 3."); default: System.out.println("That's not 1, 2, or 3!"); }

PetFood.java switch(foodGrade) { case 'a': case 'A': System.out.println("30 cents per lb."); break; case 'b': case 'B': System.out.println("20 cents per lb."); break; case 'c': case 'C': System.out.println("15 cents per lb."); break; default: System.out.println("Invalid choice."); }

The switch Case  The default case is optional and will be executed if no CaseExpression matches the SwitchExpression.

SwitchDemo.java // Determine the number entered. switch (number) { case 1: System.out.println("You entered 1."); break; case 2: System.out.println("You entered 2."); break; case 3: System.out.println("You entered 3."); break; default: System.out.println("That's not 1, 2, or 3!"); }

The DecimalFormat Class  When printing out double and float values, the full fractional value will be printed.  The DecimalFormat class can be used to format these values.  In order to use the DecimalFormat class, the Java import statement must be used.  At the top of the program the statement import java.text.DecimalFormat; must be used.

Format1.java double number1 = ; double number2 = ; double number3 = ; double number4 = ; DecimalFormat formatter = new DecimalFormat("#0.00"); // Display the formatted variable contents. System.out.println(formatter.format(number1)); System.out.println(formatter.format(number2)); System.out.println(formatter.format(number3)); System.out.println(formatter.format(number4));

Format2.java double number1 = ; double number2 = ; double number3 = ; double number4 = ; // Create a DecimalFormat object. DecimalFormat formatter = new DecimalFormat("000.00"); // Display the formatted variable contents. System.out.println(formatter.format(number1)); System.out.println(formatter.format(number2)); System.out.println(formatter.format(number3)); System.out.println(formatter.format(number4));

Format3.java double number1 = ; double number2 = ; double number3 = ; double number4 = ; double number5 = ; DecimalFormat formatter = new DecimalFormat("#,##0.00"); System.out.println(formatter.format(number1)); System.out.println(formatter.format(number2)); System.out.println(formatter.format(number3)); System.out.println(formatter.format(number4)); System.out.println(formatter.format(number5));

Format4.java double number1 = 0.12; double number2 = 0.05; DecimalFormat formatter = new DecimalFormat("#0%"); // Display the formatted variable contents. System.out.println(formatter.format(number1)); System.out.println(formatter.format(number2));