© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Chapter 4 Decision Making Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold.
3-1 Chapter 3 Flow of Control (part a - branching)
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
© 2007 Lawrenceville Press Slide 1 Chapter 3 Margins.
1 Review Quisioner Kendala: Kurang paham materi. Praktikum Pengaruh teman.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Control Structures Selections Repetitions/iterations
Decisions If statements in C.
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Nested if-else Statements.  Should be indented to make the logic clear.  Nested statement executed only when the branch it is in is executed. For example,
Flow of Control Usually the order of statement execution through a method is linear: one after another flow of control: the order statements are executed.
Java Math Class. What is the Math Class? The Math Class is another class that is prepared by Java for us to use We use this class for mathematical operations.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Selection Control Structures Chapter 5: Selection Asserting Java © Rick Mercer.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
If Statements & Relational Operators Programming.
1 Fall 2008ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Chapter 4 The If…Then Statement
Lecture 2: Static Methods, if statements, homework uploader.
Decision Structures and Boolean Logic
Computer Science Selection Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
Flow of Control Part 1: Selection
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Week 8: Decisions Bryan Burlingame 21 October 2015.
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.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
Random Numbers. Are a series of numbers that have no pattern to them Ex) 7, 31, 4, 9, 8, 99… Random Numbers are used in… - Computer Games - Lotteries(6-49)
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Decision Statements, Short- Circuit Evaluation, Errors.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
CPS120: Introduction to Computer Science Decision Making in Programs.
5.02B Decision Making Structure (part 2). Compound Boolean Expressions.
Conditional Control Structures Chapter 5 Review. The If Statement The if statement is a conditional control structure, also called a decision structure,
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
© 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  Conditional control structure, also called a decision structure  Executes.
Chapter 5 The if Statement
Chapter 4 The If…Then Statement
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions and If
Topics The if Statement The if-else Statement Comparing Strings
Topics The if Statement The if-else Statement Comparing Strings
Relational Operators.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Presentation transcript:

© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition is true  The condition is a Boolean expression  For example, the statement if (x == 5) { y = 20; } assigns the value 20 to y only if x is equal to 5.

© 2007 Lawrenceville Press Slide 2 Chapter 5 Relational Operators OperatorMeaning == equal < less than <= less than or equal > greater than >= greater than or equal != not equal

© 2007 Lawrenceville Press Slide 3 Chapter 5 The if-else Statement Contains an else clause that is executed when the if condition evaluates to false. For example, the statement if (x == 5) { y = 20; } else { y = 10; } assigns the value 20 to y if x is equal to 5 or the value 10 if x is not equal to 5.

© 2007 Lawrenceville Press Slide 4 Chapter 5 Nested if-else Statements  Should be indented to make the logic clear.  Nested statement executed only when the branch it is in is executed. For example, the statement if (x == 5) { y = 20; } else { if (x > 5) { y = 10; } else { y = 0; } } evaluates the nested if-else only when x is not equal to 5.

© 2007 Lawrenceville Press Slide 5 Chapter 5 The if-else if Statement  Used to decide among three or more actions.  Conditions must be properly ordered for the statement to evaluate as expected. For example, the statement if (x < 5) { y = 20; } else if (x < 10) { y = 40; } else if (x < 15) { y = 80; } would give very different results if the conditions were ordered differently.

© 2007 Lawrenceville Press Slide 6 Chapter 5 The switch Statement  Used to decide among three or more actions.  Uses an expression that evaluates to an integer.  The break statement moves program execution to the next statement after the switch.  The default code is optional and is executed when none of the previous cases are met: switch (numLegs) { case 2: System.out.println("human"); break; case 4: System.out.println("beast"); break; case 8: System.out.println("insect"); break; default: System.out.println("???"); break; }

© 2007 Lawrenceville Press Slide 7 Chapter 5 The Math Class  Part of the java.lang package  The random() methods generates a double between 0 and 1.0. For example, double rNum; rNum = Math.random();  A random integer in a range is generated by using the expression: (highNum – lowNum + 1) * Math.random() + lowNum

© 2007 Lawrenceville Press Slide 8 Chapter 5 Compound Boolean Expressions  More than one Boolean expression in a single condition.  Formed using the logical And ( && ), logical Or ( || ), or logical Not ( ! ) operators.

© 2007 Lawrenceville Press Slide 9 Chapter 5 And Truth Table And Exp1Exp2Result True False TrueFalse

© 2007 Lawrenceville Press Slide 10 Chapter 5 Or Truth Table Or Exp1Exp2Result True FalseTrue FalseTrue False

© 2007 Lawrenceville Press Slide 11 Chapter 5 Not Truth Table Not ExpResult TrueFalse True

© 2007 Lawrenceville Press Slide 12 Chapter 5 The Math Class  Part of the java.lang package  Methods include: abs(num) returns the absolute value of num pow(num1, num2) returns num1 raised to the num2 power sqrt(num) returns the square root of num, where num is a positive number

© 2007 Lawrenceville Press Slide 13 Chapter 5 Flowchart Symbols decision

© 2007 Lawrenceville Press Slide 14 Chapter 5 The RPS Flowchart