Dale Roberts Program Control using Java - Selection Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2005 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Dale Roberts Introduction to Java - Access Specifiers Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
Dale Roberts Program Control using Java - Boolean Expressions Dale Roberts, Lecturer Computer Science, IUPUI Department of.
1 CSCE 1030 Computer Science 1 Control Statements in Java.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Dale Roberts Object Oriented Programming using Java - Enumerations Dale Roberts, Lecturer Computer Science, IUPUI Department.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Dale Roberts Object Oriented Programming using Java - OOD to OOP: ATM Case Study Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Chapter 05 (Part III) Control Statements: Part II.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Essentials of Counter-Controlled Repetition Counter-controlled repetition requires: Control variable (loop counter) Initial value of the control variable.
 2009 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Dale Roberts Introduction to Java - Input, Program Control and Instantiation Dale Roberts, Lecturer Computer Science, IUPUI
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Control Statements: Part1  if, if…else, switch 1.
 2005 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2007 Pearson Education, Inc. All rights reserved Control Statements: Part2.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 4 – C Program Control
Control Statements: Part 2
Control Statements: Part 2
Topic 3, Selection Statements
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Control Statements: Part 2
Control Statements: Part 2
MSIS 655 Advanced Business Applications Programming
Program Control using Java - Theory
Advanced Programming Chapters 5 & 6: Control Structures
MSIS 655 Advanced Business Applications Programming
Chapter 4 - Program Control
4 C Program Control.
Chapter 6 Control Statements: Part 2
Control Statements: Part 2
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
Dale Roberts, Lecturer IUPUI
Presentation transcript:

Dale Roberts Program Control using Java - Selection Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts Control Structures (Cont.) Selection Statements if statement Single-selection statement if…else statement Double-selection statement switch statement Multiple-selection statement

Dale Roberts if Single-Selection Statement if statements Execute an action if the specified condition is true Can be represented by a decision symbol (diamond) in a UML activity diagram Transition arrows out of a decision symbol have guard conditions Workflow follows the transition arrow whose guard condition is true

Dale Roberts 4 Fig. 4.2 | if single-selection statement UML activity diagram.

Dale Roberts if…else Double-Selection Statement if…else statement Executes one action if the specified condition is true or a different action if the specified condition is false Conditional Operator ( ? : ) Java’s only ternary operator (takes three operands) ? : and its three operands form a conditional expression Entire conditional expression evaluates to the second operand if the first operand is true Entire conditional expression evaluates to the third operand if the first operand is false

Dale Roberts 6 Fig. 4.3 | if …else double-selection statement UML activity diagram.

Dale Roberts 7 Fig | switch multiple-selection statement UML activity diagram with break statements.

Dale Roberts 8 Software Engineering Observation 5.2 Provide a default case in switch statements. Including a default case focuses you on the need to process exceptional conditions.

Dale Roberts 9Outline GradeBook.j ava (1 of 5) Lines 8-14

Dale Roberts 10Outline GradeBook.j ava (2 of 5) Lines Display prompt

Dale Roberts 11Outline GradeBook.j ava (3 of 5) Line 57 Line 72 controlling expression Lines Loop condition uses method hasNext to determine whether there is more data to input switch statement determines which case label to execute, depending on controlling expression (grade / 10 ) is controlling expression

Dale Roberts 12Outline GradeBook.j ava (4 of 5) Line 91 default case default case for grade less than 60

Dale Roberts 13Outline GradeBook.j ava (5 of 5)

Dale Roberts 14Outline GradeBookT est.java (1 of 2) Lines Call GradeBook public methods to count grades

Dale Roberts 15Outline GradeBookT est.java (2 of 2) Program output

Dale Roberts 16 Portability Tip 5.1 The keystroke combinations for entering end-of-file are system dependent.

Dale Roberts 17 Good Programming Practice 5.8 Although each case and the default case in a switch can occur in any order, place the default case last. When the default case is listed last, the break for that case is not required. Some programmers include this break for clarity and symmetry with other cases.

Dale Roberts 18 switch Multiple-Selection Statement (Cont.) Expression in each case Constant integral expression Combination of integer constants that evaluates to a constant integer value Character constant E.g., ‘A’, ‘7’ or ‘$’ Constant variable Declared with keyword final

Dale Roberts break and continue Statements break/continue Alter flow of control break statement Causes immediate exit from control structure Used in while, for, do…while or switch statements continue statement Skips remaining statements in loop body Proceeds to next iteration Used in while, for or do…while statements

Dale Roberts 20 Common Programming Error 5.7 Forgetting a break statement when one is needed in a switch is a logic error.

Dale Roberts 21Outline BreakTest.ja va Line 9 Lines Program output Loop 10 times Exit for statement (break) when count equals 5

Dale Roberts 22Outline ContinueTe st.java Line 7 Lines 9-10 Program output Loop 10 timesSkip line 12 and proceed to line 7 when count equals 5

Dale Roberts 23 Software Engineering Observation 5.3 Software Engineering Observation 5.3 Some programmers feel that break and continue violate structured programming. Since the same effects are achievable with structured programming techniques, these programmers do not use break or continue.

Dale Roberts 24 Software Engineering Observation 5.4 There is a tension between achieving quality software engineering and achieving the best-performing software. Often, one of these goals is achieved at the expense of the other. For all but the most performance-intensive situations, apply the following rule of thumb: First, make your code simple and correct; then make it fast and small, but only if necessary.

Dale Roberts Acknowledgements Deitel, Java How to Program