1 2. Program Construction in Java. 2.4 Selection (decisions)

Slides:



Advertisements
Similar presentations
3-1 Chapter 3 Flow of Control (part a - branching)
Advertisements

Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Conditional statements and Boolean expressions. The if-statement in Java (1) The if-statement is a conditional statement The statement is executed only.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Lecture 7 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
true (any other value but zero) false (zero) expression Statement 2
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Branch Statements (Decision). Flow of Control  The order in which a program performs actions.  A branching statement chooses one of two or more possible.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
MS3304: Week 6 Conditional statements. Overview The flow of control through a script Boolean Logic Conditional & logical operators Basic decision making.
Decision Making Selection structures (if....else and switch/case)
Making Decisions (True or False) Relational Operators >greater than =greater than or equal 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.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 4 – Logical Operators & Branching.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
Chapter 6 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Java Programming Fifth Edition
Chapter 3: Decisions and Loops
Chapter 6 Conditionals.
Selection—Making Decisions
Lecture 3- Decision Structures
Chapter 3 Branching Statements
CiS 260: App Dev I Chapter 4: Control Structures II.
Expressions and Control Flow in JavaScript
Control Structures.
Chapter 4 Conditionals.
IF if (condition) { Process… }
Control Structures: Selection Statement
Selection Statements.
Chapter 4: Control Structures I (Selection)
Program Flow.
Control Structures: Selection Statement
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
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.
Chapter 6 Conditionals.
Presentation transcript:

1 2. Program Construction in Java

2.4 Selection (decisions)

3 Making decisions In programming, making decisions involves evaluating a logical (boolean) expression (i.e. questions with the answer true or false ) and passing control to different branches depending on the answer.

4 Making decisions A human equivalent is to evaluate the statement it is raining: ‣ if true we go to the cinema, ‣ if false we go for a picnic. Such decisions, where program flow could go either way are called selection.

5 The if statement ‣ if (condition_to_test) { statements to execute if true; }

6 The if...else statement ‣ if (condition_to_test) { statements to execute if true; } ‣ else { statements to execute if false; }

7 if..else ‣ if (condition_to_test) { statements to execute if true; } ‣ else if (other_condition_to_test) { statements to execute if true; } ‣ else { statements to execute if false; }

8 if..else You can use as many else if blocks as you wish, but if this gets complex, consider the switch statement (see later).

9 Boolean expressions The condition_to_test takes the form of a boolean expression, i.e. one that has the value true or false, e.g. ‣ count < 5 or ‣ taxRate == 17.5

10 Boolean expressions if (a < b) (if a is less than b ), if (a <= b) (if a is less than or equal to b ), if (a == b) (if a is equal to b, note the two signs), if (a > b) (if a is greater than b ).

11 Boolean expressions if (a >= b) (if a is greater than or equal to b ) if (a != b) (if a is not equal to b ) if (!a) (if a is not true) (where a is a boolean variable) if (pass) (where pass is a boolean variable).

12 AND and OR Expressions can be combined using && (AND) and || (OR): ‣ if (a < b && c == d) means both must be true, ‣ if (a < b || c == d) means either can be true. & is the ampersand, | the break bar.

13 Comparing Strings Remember, Strings are not primitives, you cannot use ==, > or <. s1.equals(s2) returns a boolean true or false. s1.compareTo(s2) returns -1 if s1 comes before s2 alphabetically, 0 if they are equal and +1 if it is after.

14 Nesting It is syntactically correct to nest as many if blocks inside other as you wish: ‣ if (condition_to_test) { if (another_condition_to_test) { statements to execute if both conditions are true; } }

15 The switch statement An alternative to the multi-way if is the switch statement: ‣ switch (name_of_variable){ case 'A:...statements to execute if the variable's value is A... break; ‣ continued...

16 The switch statement ‣ case 'B:...statements to execute if the variable's value is B... break; case 'C:...statements to execute if the variable's value is C... break; }

17 The switch statement Note the whole switch block is enclosed by one pair of braces. The different cases are followed by colons (:). The break; line jumps control to the end of the block.