Chapter 5 The if Statement

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

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,
© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements.
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.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Logical Operators and Conditional statements
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Chapter 4 The If…Then Statement
Lecture 2: Static Methods, if statements, homework uploader.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Copyright 2003 Scott/Jones Publishing Making Decisions.
1 Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Flow of Control / Conditional Statements The.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
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.
Control statements Mostafa Abdallah
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)
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.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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 3 Control Statements
Chapter 4 Assignment Statement
Chapter 4 The If…Then Statement
COMP 14 Introduction to Programming
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions and If
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Making Decisions.
Control Structures.
Chapter 4 Conditionals.
Making Decisions in a Program
Control Structures: Selection Statement
Pages:51-59 Section: Control1 : decisions
Visual Basic – Decision Statements
Microsoft Visual Basic 2005: Reloaded Second Edition
Relational Operators.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Control Structures: Selection Statement
Comparing Data & the ‘switch’ Statement
Pages:51-59 Section: Control1 : decisions
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Selection Control Structure
Controlling Program Flow
Presentation transcript:

Chapter 5 The if Statement 5/13/2018 9:32 PM 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. Refer to page 105 in the text. © 2007 Lawrenceville Press

Chapter 5 Relational Operators 5/13/2018 9:32 PM Chapter 5 Relational Operators Operator Meaning == equal < less than <= less than or equal > greater than >= greater than or equal != not equal Refer to page 105 in the text. Relational operators are used to form Boolean expressions. Boolean expressions evaluate to true or false. © 2007 Lawrenceville Press

Chapter 5 The if-else Statement 5/13/2018 9:32 PM 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. Refer to pages 106 and 107 in the text. © 2007 Lawrenceville Press

Chapter 5 Nested if-else Statements 5/13/2018 9:32 PM 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. Refer to page 107 in the text. © 2007 Lawrenceville Press

Chapter 5 The if-else if Statement 5/13/2018 9:32 PM 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. Refer to page 108 in the text. © 2007 Lawrenceville Press

Chapter 5 The switch Statement 5/13/2018 9:32 PM 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; } Refer to page 109 in the text. © 2007 Lawrenceville Press

Part of the java.lang package 5/13/2018 9:32 PM 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 Refer to pages 110 and 111 in the text. The Random class actually generates pseudorandom (like random) numbers. Java implements the Linear Congruential Method to ensure that generated numbers can be considered random for most applications. © 2007 Lawrenceville Press

Chapter 5 Compound Boolean Expressions 5/13/2018 9:32 PM 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. Refer to page 112 in the text. Compound Boolean expressions use more than one Boolean expression to determine if a condition is true or false. © 2007 Lawrenceville Press

Chapter 5 And Truth Table 5/13/2018 9:32 PM Chapter 5 And Truth Table And Exp1 Exp2 Result True False Refer to page 112 in the text. © 2007 Lawrenceville Press

Or Exp1 Exp2 Result True False Chapter 5 Or Truth Table 5/13/2018 9:32 PM Chapter 5 Or Truth Table Or Exp1 Exp2 Result True False Refer to page 112 in the text. © 2007 Lawrenceville Press

Chapter 5 Not Truth Table 5/13/2018 9:32 PM Chapter 5 Not Truth Table Not Exp Result True False Refer to page 112 in the text. © 2007 Lawrenceville Press

Part of the java.lang package Methods include: 5/13/2018 9:32 PM 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 Refer to page 113 in the text. © 2007 Lawrenceville Press

Chapter 5 Flowchart Symbols 5/13/2018 9:32 PM Chapter 5 Flowchart Symbols decision Refer to page 115 in the text. The diamond flowchart symbol indicates a decision. © 2007 Lawrenceville Press

Chapter 5 The RPS Flowchart 5/13/2018 9:32 PM Chapter 5 The RPS Flowchart Refer to page 115 in the text. The RPS flowchart illustrates the application solution. Solution steps include: 1. prompting the user for a number between 1 and 3 2. generating a random number between 1 and 3 3. comparing the generated number to the number typed by the user 4. determining a winner and displaying an appropriate message © 2007 Lawrenceville Press