Conditional Control Structures Chapter 5 Review. The If Statement The if statement is a conditional control structure, also called a decision structure,

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

Control Structures.
Chapter 4: Control Structures I (Selection)
© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
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.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
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.
true (any other value but zero) false (zero) expression Statement 2
Branch Statements (Decision). Flow of Control  The order in which a program performs actions.  A branching statement chooses one of two or more possible.
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
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
Decision Structures and Boolean Logic
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Flow of Control Part 1: Selection
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Branches and Program Design
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
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
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.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Chapter 6 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
Random Functions Selection Structure Comparison Operators Logical Operator
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
© 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  Conditional control structure, also called a decision structure  Executes.
Lecture 3 Selection Statements
Chapter 5 The if Statement
Chapter 4 The If…Then Statement
Chapter 3: Decisions and Loops
Chapter 4: Control Structures I
Chapter 6 Conditionals.
Topics The if Statement The if-else Statement Comparing Strings
Control Structures.
Chapter 4: Control Structures I
Chapter 4 Conditionals.
Topics The if Statement The if-else Statement Comparing Strings
Visual Basic – Decision Statements
Random number generators
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Controlling Program Flow
Chapter 6 Conditionals.
Presentation transcript:

Conditional Control Structures Chapter 5 Review

The If Statement The if statement is a conditional control structure, also called a decision structure, which executes a set of statements when a condition is true. It takes the form: if ( ) { ; }

Relational operators Relational operators can be used to form Boolean expressions. They are: OperatorMeaning ==Equal <less than <=less than or equal >more than >=more than or equal !=not equal

The If Statement example In this example, the user’s guess is compared to the constant SECRET_NUM. If they are the same, “You guessed it” is outputted. if (guess==SECRET_NUM) { System.out.println(“You guessed it”); }

The If-Else Statement The if statement can include an optional else clause that is executed when the if condition is false. if ( ) { ; } else { ; }

The If-Else Statement example if (guess==SECRET_NUM) { System.out.println(“You guessed it”); } else { System.out.println(“Try again”); }

Nested If Statements Statements placed within the same type of statements are called nested. if (guess==SECRET_NUM) { System.out.println(“You guessed it”); } else { if (guess<SECRET_NUM) { System.out.println(“Too low”); } else { System.out.println(“Too high”); }

The If-Else if Statement The if-else if statement is used to decide among 3 or more actions. if ( ) { ; } else if ( ) { ; } else { ; }

The If-Else if Statement example The if-else if statement is used to decide among 3 or more actions. if (guess==SECRET_NUM) { System.out.println(“You guessed it”); } else if (guess<SECRET_NUM) { System.out.println(“Too low”); } else { System.out.println(“Too high”); }

The switch Statement The switch statement is a conditional control that uses the result of an expression to determine which statements to execute. switch ( ) { case x: ; break; default: ; break; }

The switch Statement cont’d break; The break statement is necessary to move program control to the next statement after the switch statement. default: The default code is optional and is executed when non of the previous cases are met.

The switch Statement example switch (score) { case 0: System.out.println(“Too bad”); break; case 0: System.out.println(“Good”); break; case 0: System.out.println(“Great”); break; }

Generating Random Numbers Java uses the Linear Congruential Method to generate a sequence of random numbers. Because the sequence eventually repeats, random number in a computer application are really pseudorandom. Java includes the Random class in the java.util package for generating random numbers.

Random Numbers in a range To generate a random number in a range the following formula is used: (high – low + 1) * Math.random() + low Note that high represents the largest number & low the lowest number in the range. To randomly generate a number between 1-10: (10 – 1 + 1) * Math.random() + 1

Compound Boolean Expressions Two or more Boolean expressions can be joined with logical operators to form a compound Boolean expression. Logical operators include && (logical And), || (logical Or), and ! (logical Not). && evaluates true only when both operands are true || evaluates true when either operand is true

Truth Tables How a compound Boolean expression evaluates with && and || operators can be shown with a truth table AND - && OR - || Exp1Exp2Results True False TrueFalse Exp1Exp2Results True FalseTrue FalseTrue False

Logical Operator examples if (guess>=1 && guess<=10) { System.out.println(“Valid entry”); } if (guess =10) { System.out.println(“Invalid entry”); }

The Math Class The Math class, part of the java.lang package, contains many useful methods for performing math functions. Calling a Math method requires including the class name. For example, Math.abs(-3)

Useful Methods of the Math Class MethodDescription abs()Returns the absolute value of the argument. pow()Returns the value of the first argument raised to the power of the second argument. sqrt()Returns the square root of the argument.