Chapter 4 Control Statements: Part I

Slides:



Advertisements
Similar presentations
Control Structures Selections Repetitions/iterations
Advertisements

Decisions If statements in C.
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.
 Control structures  Algorithm & flowchart  If statements  While statements.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
Chapter 3 - Structured Program Development
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 14.1Introduction 14.2Algorithms 14.3Pseudocode 14.4Control Structures 14.5The if Selection.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Structured Program Development in C
Chapter 4 Control Statements: Part I
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
The University of Texas – Pan American
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 CSCE 1030 Computer Science 1 Control Statements in Java.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
C Lecture Notes 1 Structured Program Development.
1 C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved. 4.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
1 Control Statements: Part I Chapter 4 Control Statements: Part I Chapter 4.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Control Statements: Part1  if, if…else, switch 1.
1 JavaScript/Jscript 2 Control Structures I. 2 Introduction Before programming a script have a –Thorough understanding of problem –Carefully planned approach.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
Chapter 3 Structured Program Development in C C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
Chapter 7 JavaScript: Control Statements, Part 1
Branching statements.
CNG 140 C Programming (Lecture set 3)
Control Statements: Part 1
Control Statements: Part 1
Topic 3, Selection Statements
Java™ How to Program, 10/e Late Objects Version
JavaScript: Control Statements I
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
JavaScript: Control Statements.
JavaScript: Control Statements I
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
Lecture 2: Logical Problems with Choices
Chapter 3: Introduction to Problem Solving and Control Statements
MSIS 655 Advanced Business Applications Programming
Structured Program
1) C program development 2) Selection structure
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 - Structured Program Development
Structural Program Development: If, If-Else
Control Statements:.
Presentation transcript:

Chapter 4 Control Statements: Part I Java™ How to Program, 10/e © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

Contents The Boolean Data Type Boolean Expressions Relational Operators Logical Operators Branching Mechanisms if-else Nesting if-else dr. Amal Khalifa,Fall14

The Boolean Data Type Often in a program you need to compare two values The result of the comparison is a Boolean value: true or false. boolean b = (1 > 2);

Relational Operators Operator Description Relational Operators > 4 Relational Operators Operator Description > Greater than < Less than = Equal to  Greater than or equal to  Less than or equal to  Not equal to

Boolean Expressions Mathematical Expression Java Expression

Logical Operators Java’s logical operators enable you to form more complex conditions by combining simple conditions. The logical operators are && conditional AND || conditional OR ! logical NOT.

Joining Boolean Expressions

Precedence of operators

Examples Short-circuit evaluation  x + 1 > 2 || x + 1 < -3 means:  (x + 1) > 2 ||(x + 1) < -3 Short-circuit evaluation  (x > 0) && (y > 1)

4.1  Introduction Before writing a program to solve a problem, have a thorough understanding of the problem and a carefully planned approach to solving it. Understand the types of building blocks that are available and employ proven program-construction techniques. In this chapter we discuss Java’s if, if…else and while statements Compound assignment, increment and decrement operators Portability of Java’s primitive types © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

4.4  Control Structures Sequential execution: Statements in a program execute one after the other in the order in which they are written. Transfer of control: Various Java statements, enable you to specify that the next statement to execute is not necessarily the next one in sequence. Bohm and Jacopini Demonstrated that programs could be written without any goto statements. All programs can be written in terms of only three control structures— the sequence structure, the selection structure and the repetition structure. When we introduce Java’s control-structure implementations, we’ll refer to them in the terminology of the Java Language Specification as “control statements.” © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

4.4 Control Structures (Cont.) Sequence Structure in Java Built into Java. Unless directed otherwise, the computer executes Java statements one after the other in the order in which they’re written. The activity diagram in Fig. 4.1 illustrates a typical sequence structure in which two calculations are performed in order. Java lets you have as many actions as you want in a sequence structure. Anywhere a single action may be placed, we may place several actions in sequence. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

Sequence Structure Flow chart Add grade to total Add one to counter Sequence Structure Flow chart © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

4.4 Control Structures (Cont.) Selection Statements in Java Three types of selection statements. if statement: Performs an action, if a condition is true; skips it, if false. Single-selection statement—selects or ignores a single action (or group of actions). if…else statement: Performs an action if a condition is true and performs a different action if the condition is false. Double-selection statement—selects between two different actions (or groups of actions). switch statement Performs one of several actions, based on the value of an expression. Multiple-selection statement—selects among many different actions (or groups of actions). © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

4.5 if Single-Selection Statement Pseudocode If student’s grade is greater than or equal to 60 Print “Passed” If the condition is false, the Print statement is ignored, and the next pseudocode statement in order is performed. Indentation Optional, but recommended Emphasizes the inherent structure of structured programs The preceding pseudocode If in Java: if (studentGrade >= 60) System.out.println("Passed"); Corresponds closely to the pseudocode. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

4.5 if Single-Selection Statement (Cont.) UML Activity Diagram for an if Statement Figure 4.2 if statement UML activity diagram. Diamond, or decision symbol, indicates that a decision is to be made. Workflow continues along a path determined by the symbol’s guard conditions, which can be true or false. Each transition arrow emerging from a decision symbol has a guard condition (in square brackets next to the arrow). If a guard condition is true, the workflow enters the action state to which the transition arrow points. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

If single selection statement flow chart Grade >=60 false true Print “Passed” End If single selection statement flow chart © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

4.6 if…else Double-Selection Statement if…else double-selection statement—specify an action to perform when the condition is true and a different action when the condition is false. Pseudocode If student’s grade is greater than or equal to 60 Print “Passed” Else Print “Failed” The preceding If…Else pseudocode statement in Java: if (grade >= 60) System.out.println("Passed"); else System.out.println("Failed"); Note that the body of the else is also indented. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

4.6 if…else Double-Selection Statement (Cont.) UML Activity Diagram for an if…else Statement Figure 4.3 illustrates the flow of control in the if…else statement. The symbols in the UML activity diagram (besides the initial state, transition arrows and final state) represent action states and decisions. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

If double-selection statement flow chart Grade >=60 false Print “Failed” true Print “Passed” End If double-selection statement flow chart © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

4.6 if…else Double-Selection Statement (Cont.) Nested if…else Statements A program can test multiple cases by placing if…else statements inside other if…else statements to create nested if…else statements. Pseudocode: If student’s grade is greater than or equal to 90 Print “A” else If student’s grade is greater than or equal to 80 Print “B” else If student’s grade is greater than or equal to 70 Print “C” else If student’s grade is greater than or equal to 60 Print “D” else Print “F” © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

If double-selection statement flow chart studentGrade >=90 true Print “A” false studentGrade >=80 true Print “B” false studentGrade >=70 true Print “C” false studentGrade >=60 true Print “D” false Print “F” If double-selection statement flow chart

4.6 if…else Double-Selection Statement (Cont.) This pseudocode may be written in Java as if (studentGrade >= 90) System.out.println("A"); else if (studentGrade >= 80) System.out.println("B"); else if (studentGrade >= 70) System.out.println("C"); else if (studentGrade >= 60) System.out.println("D"); else System.out.println("F"); If studentGrade >= 90, the first four conditions will be true, but only the statement in the if part of the first if…else statement will execute. After that, the else part of the “outermost” if…else statement is skipped. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

4.6 if…else Double-Selection Statement (Cont.) Most Java programmers prefer to write the preceding nested if…else statement as if (studentGrade >= 90) System.out.println("A"); else if (studentGrade >= 80) System.out.println("B"); else if (studentGrade >= 70) System.out.println("C"); else if (studentGrade >= 60) System.out.println("D"); else System.out.println("F"); The two forms are identical except for the spacing and indentation, which the compiler ignores. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

4.6 if…else Double-Selection Statement (Cont.) Dangling-else Problem The Java compiler always associates an else with the immediately preceding if unless told to do otherwise by the placement of braces ({ and }). Referred to as the dangling-else problem. The following code is not what it appears: if (x > 5) if (y > 5) System.out.println("x and y are > 5"); else System.out.println("x is <= 5"); Beware! This nested if…else statement does not execute as it appears. The compiler actually interprets the statement as if (x > 5) if (y > 5) System.out.println("x and y are > 5"); else System.out.println("x is <= 5"); © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

4.6 if…else Double-Selection Statement (Cont.) To force the nested if…else statement to execute as it was originally intended, we must write it as follows: if (x > 5) { if (y > 5) System.out.println("x and y are > 5"); } else System.out.println("x is <= 5"); The braces indicate that the second if is in the body of the first and that the else is associated with the first if. Exercises 4.27–4.28 investigate the dangling-else problem further. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

4.6 if…else Double-Selection Statement (Cont.) Blocks The if statement normally expects only one statement in its body. To include several statements in the body of an if (or the body of an else for an if…else statement), enclose the statements in braces. Statements contained in a pair of braces (such as the body of a method) form a block. A block can be placed anywhere in a method that a single statement can be placed. Example: A block in the else part of an if…else statement: if (grade >= 60) System.out.println("Passed"); else { System.out.println("Failed"); System.out.println("You must take this course again."); } © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

4.6 if…else Double-Selection Statement (Cont.) Syntax errors (e.g., when one brace in a block is left out of the program) are caught by the compiler. A logic error (e.g., when both braces in a block are left out of the program) has its effect at execution time. A fatal logic error causes a program to fail and terminate prematurely. A nonfatal logic error allows a program to continue executing but causes it to produce incorrect results. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

4.6 if…else Double-Selection Statement (Cont.) Just as a block can be placed anywhere a single statement can be placed, it’s also possible to have an empty statement. The empty statement is represented by placing a semicolon (;) where a statement would normally be. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

4.6 if…else Double-Selection Statement (Cont.) Conditional operator (?:) Conditional operator (?:)—shorthand if…else. Ternary operator (takes three operands) Operands and ?: form a conditional expression Operand to the left of the ? is a boolean expression— evaluates to a boolean value (true or false) Second operand (between the ? and :) is the value if the boolean expression is true Third operand (to the right of the :) is the value if the boolean expression evaluates to false. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

4.6 if…else Double-Selection Statement (Cont.) Example: System.out.println( studentGrade >= 60 ? "Passed" : "Failed"); Evaluates to the string "Passed" if the boolean expression studentGrade >= 60 is true and to the string "Failed" if it is false. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

4.7 Student Class: Nested if…else Statement Class Student Class Student (Fig. 4.4) stores a student’s name and average and provides methods for manipulating these values. The class contains: instance variable name of type String to store a Student’s name instance variable average of type double to store a Student’s average in a course a constructor that initializes the name and average methods setName and getName to set and get the Student’s name methods setAverage and getAverage to set and get the Student’s average method getLetterGrade (lines 49–65), which uses nested if…else statements to determine the Student’s letter grade based on the Student’s average © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

4.7 Student Class: Nested if…else Statement (Cont.) The constructor and method setAverage each use nested if statements to validate the value used to set the average—these statements ensure that the value is greater than 0.0 and less than or equal to 100.0; otherwise, average’s value is left unchanged. Each if statement contains a simple condition. If the condition in line 15 is true, only then will the condition in line 16 be tested, and only if the conditions in both line 15 and line 16 are true will the statement in line 17 execute. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

Draw the flow chart of the nested if in setAverage method In-Class Exercise Draw the flow chart of the nested if in setAverage method © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

Nested if statement flow chart average >0.0 average <=100 true false true false This.average = average End Nested if statement flow chart © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

4.7 Student Class: Nested if…else Statement (Cont.) Class StudentTest To demonstrate the nested if…else statements in class Student’s getLetterGrade method, class StudentTest’s main method creates two Student objects. Next, lines 10–13 display each Student’s name and letter grade by calling the objects’ getName and getLetterGrade methods, respectively. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

© Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.

Chapter 5: Read 5.8 in Version 9 or 5.9 in Version 10 © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved.