SWE 510: Object Oriented Programming in Java

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

3-1 Chapter 3 Flow of Control (part a - branching)
The if-else Statements
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Branch Statements (Decision). Flow of Control  The order in which a program performs actions.  A branching statement chooses one of two or more possible.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
SYDE223 Tutorial 3: Introduction to Java Annie En-Shiun Lee January 19, 2010.
UNIT II Decision Making And Branching Decision Making And Looping
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
Chapter 3 Flow of Control Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Control Structures special statements whose purpose is to change the order of execution in a program. Sequential structures – Statements executed sequentially.
DAT602 Database Application Development Lecture 5 JAVA Review.
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,
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control.
Chapter 3 Flow of Control Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008 Pearson.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Flow of Control (part 2) Joe McCarthy CSS 161: Fundamentals of Computing.
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
1 Week 6 Branching. 2 What is “Flow of Control”? l Flow of Control is the execution order of instructions in a program l All programs can be written with.
Flow of Control Chapter 3 Flow of control Branching Loops
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Chapter 4: Control Structures SELECTION STATEMENTS.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Control statements Mostafa Abdallah
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Switch statement.
Chapter 3 Flow of Control
Chapter 3 Flow of Control
Chapter 4: Control Structures I
Midterm Review.
Boolean expressions and if-else statements
Chapter 4: Control Structures I
5.3 Multiple Alternatives (Part 1)
Introduction to programming in java
C++ Conditions Test Cases
Flow of Control.
Chapter 3 Branching Statements
Chapter 5: Control Structures II
Chapter 4: Control Structures I
Pemrograman Dasar Smt I 2004/2005
Control Structure Chapter 3.
Lecture Notes – Week 2 Lecture-2
Conditionals.
The Java switch Statement
Chapter 3 Flow of Control
Program Flow.
Control Structure.
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.
CSS 161: Fundamentals of Computing
Selection Statements August 22, 2019 ICS102: The course.
Presentation transcript:

SWE 510: Object Oriented Programming in Java Branching Mechanism This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the instructor’s class materials. SWE 510: Object Oriented Programming in Java

SWE 510: Object Oriented Programming in Java Flow of Control Branching mechanisms if-else if switch (break) Looping mechanisms while do while for Boolean expressions evaluate to either true or false and decide which branch the control should go to if the control should stay in a loop SWE 510: Object Oriented Programming in Java

SWE 510: Object Oriented Programming in Java if-else Statement Prev_Statement Syntax Previous_Statement if ( Boolean_Expression ) Yes_Statement else No_Statement Next_Statement Examples if ( time < limit ) System.out.println( “You made it” ); System.out.println( “You missed the deadline” ); if ( side1 + side2 > side3 && side2 + side3 > side1 && side3 + side1 > side2 ) System.out.println( “Forms a triangle” ); System.out.println( “Does not form a triangle” ); Boolean Expression? true Yes_Statement false No_Statement Next_Statement SWE 510: Object Oriented Programming in Java

SWE 510: Object Oriented Programming in Java Omitting the else Part Prev_Statement Syntax Previous_Statement if ( Boolean_Expression ) Yes_Statement Action_Statement Examples if ( weight > ideal ) calorieAllotment = calorieAllotment - 500; if ( mph > 60 ) System.out.println( “Too fast” ); Boolean Expression? true Action_Statement false Next_Statement SWE 510: Object Oriented Programming in Java

SWE 510: Object Oriented Programming in Java Compound Statements Each Yes_Statement and No_Statement branch of an if-else can be a made up of a single statement or many statements Syntax if ( Boolean_Expression ) { Yes_Statement1; Yes_statement2; ...; } else No_Statement1; No_Statement2; Example if ( myScore > yourScore ) System.out.println( “I win!” ); wager += 100; System.out.println( “I wish these were golf scores” ); wager = 0; Yes_Statement1 Yes_Statement2 … Boolean Expression? true false No_Statement1 No_Statement2 … SWE 510: Object Oriented Programming in Java

SWE 510: Object Oriented Programming in Java Nested Statements if-else statements and if statements both contain smaller statements within them Example if ( side1 == side2 ) { System.out.println( “Isoceles” ); if ( side2 == side3 ) System.out.println( “Equilateral” ); } Equivalent if ( side1 == side2 ) System.out.println( “Isoceles” ); if ( side1 == side2 && side2 == side3 ( System.out.println( “Equilateral” ); SWE 510: Object Oriented Programming in Java

Multiway if-else Statement Syntax if (Boolean_Expression_1) Statement_1 else if (Boolean_Expression_2) Statement_2 else if (Boolean_Expression_n) Statement_n else Statement_For_All_Other_Possibilities Example if ( testScore >= 95 ) grade = ‘A’; else if ( testScore >= 85 ) grade = ‘B’; else if ( testScore >= 75 ) grade = ‘C’; else if ( testScore >= 65 ) grade = ‘D’; grade = ‘F’; System.out.println( “grade = ” + grade ); Expression_1? Statement_1 Expression_2? Statement_2 Expression_n? Statement_n Other_Statement SWE 510: Object Oriented Programming in Java

SWE 510: Object Oriented Programming in Java What’s Redundant? Improve Textbook p101’s code if ( netIncome <= 15000 ) tax = 0; else if ( ( netIncome > 15000 ) && ( netIncome <= 30000 ) ) tax = 0.05 * ( netIncome – 15000 ); else { fivePercentTax = 0.05 * 15000; tenPercentTax = 0.10 * ( netIncome – 30000 ); tax = fivePercentTax + tenPercentTax; } true netIncome <= 15000 Tax = 0 false true netIncome <= 30000 Tax = 0.05 * … false Tax = 0.05 * …+ 0.10 * ….; SWE 510: Object Oriented Programming in Java

SWE 510: Object Oriented Programming in Java switch Statement The switch statement is the only other kind of Java statement that implements multiway branching When a switch statement is evaluated, one of a number of different branches is executed Syntax switch (Controlling_Expression) { case Case_Label_1: Statement_Sequence_1 break; case Case_Label_2: Statement_Sequence_2 case Case_Label_n: Statement_Sequence_n default: Default_Statement Sequence } Must be a constant: char, int, short, or byte Controlling_Exp Label_1? true Statement_1 false Label_2? Statement_2 Label_n? Statement_n default_Statement Optional but recommended Executed in none of label_1 ~ n SWE 510: Object Oriented Programming in Java

SWE 510: Object Oriented Programming in Java Example 1 (Textbook p103) int vehicleClass; double toll; ...; switch ( vehicleClass ) { case 1: System.out.println( “Passenger car.” ); toll = 0.50; break; case 2: System.out.println( “Bus.” ); toll = 1.50; case 3: System.out.println( “Truck.” ); toll = 2.00; default: System.out.println( “Unknown vehicle class!” ); } A controlling expression must return char, int, short, or byte. Case labels must be a constant. Recommended to detect error cases. SWE 510: Object Oriented Programming in Java

Example 2 (java.sun.com tutorial) A controlling expression must return char, int, short, or byte. class SwitchDemo { public static void main(String[] args) { int month = 8; switch (month) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 5: System.out.println("May"); break; case 6: System.out.println("June"); break; case 7: System.out.println("July"); break; case 8: System.out.println("August"); break; case 9: System.out.println("September"); break; case 10: System.out.println("October"); break; case 11: System.out.println("November"); break; case 12: System.out.println("December"); break; default: System.out.println("Invalid month."); break; } Case labels must be a constant. Recommended to detect error cases. SWE 510: Object Oriented Programming in Java

switch Statement without breaks Syntax switch (Controlling_Expression) { case Case_Label_1: Statement_Sequence_1 case Case_Label_2: Statement_Sequence_2 break; case Case_Label_m; Statement_Sequence_m case Case_Label_n: Statement_Sequence_n default: Default_Statement Sequence } Controlling_Exp Label_1? true Statement_1 false Label_2? Statement_2 Label_m? Statement_m Label_n? Statement_n Default_stmt SWE 510: Object Oriented Programming in Java

Example 3 (java.sun.com tutorial) class SwitchDemo2 { public static void main(String[] args) { int month = 2; int year = 2000; int numDays = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; break; case 4: case 6: case 9: case 11: numDays = 30; case 2: if ( ((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0) ) numDays = 29; else numDays = 28; default: System.out.println("Invalid month."); } System.out.println("Number of Days = " + numDays); SWE 510: Object Oriented Programming in Java

PITFALL: Forgetting a break class SwitchDemo2 { public static void main(String[] args) { int month = 2; int year = 2000; int numDays = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; case 4: case 6: case 9: case 11: numDays = 30; case 2: if ( ((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0) ) numDays = 29; else numDays = 28; default: System.out.println("Invalid month."); } System.out.println("Number of Days = " + numDays); Compiler does not complain about no breaks. Control continues down to the next case. SWE 510: Object Oriented Programming in Java

SWE 510: Object Oriented Programming in Java Conditional Operator A notational variant on certain forms of the if-else statement Also called the ternary operator or arithmetic if Syntax ( Boolean_Expression ) ? Yes_Expression : No_Expression; // if Boolean_Expression is true, evaluate Yes_Expression, // otherwise evaluate No_Expression, and finally return the // resulted value. Example max = ( n1 > n2 ) ? n1 : n2; Equivalent if ( n1 > n2 ) max = n1; else max = n2; SWE 510: Object Oriented Programming in Java