Selection Structures Tonga Institute of Higher Education.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Selection Structures Tonga Institute of Higher Education.
Conditional Statements Introduction to Computing Science and Programming I.
Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either.
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.
(c) 2003 E.S.Boese1 Conditionals Chapter 10 - Student.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Loops – While, Do, For Repetition Statements Introduction to Arrays
If Statement if (amount
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
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
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
New Mexico Computer Science For All Booleans and Logic Maureen Psaila-Dombrowski.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Decision Structures and Boolean Logic
Logic & Logical Operators Logical Operators are used to connect two or more relational expressions into one OR to reverse the logic of an expression. OperatorMeaning.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Flow of Control Part 1: Selection
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
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.
1 2. Program Construction in Java. 2.4 Selection (decisions)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 4 – Logical Operators & Branching.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Control statements Mostafa Abdallah
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
Decision Statements, Short- Circuit Evaluation, Errors.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Silberschatz and Galvin  C Programming Language Decision making in C Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
 Type Called bool  Bool has only two possible values: True and False.
Lecture 3 Selection Statements
Chapter 3 Selection Statements
Java Programming Fifth Edition
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Lecture 3- Decision Structures
Topics The if Statement The if-else Statement Comparing Strings
JavaScript: Control Statements.
Topics The if Statement The if-else Statement Comparing Strings
Conditions and Ifs BIS1523 – Lecture 8.
Computer Science Core Concepts
Relational Operators.
Presentation transcript:

Selection Structures Tonga Institute of Higher Education

Introduction Programs must handle different conditions when the program is running. Selection structures allow programs to handle different conditions.  If…Else Statement  Switch Statement

Source Code Execution Order Generally, source code is run in order But we don’t always run in order This is run first This is run second This is run third

Selection Structures/Statements Selection structures allow programs to run different code based on runtime conditions.  If…Else Statement  Switch Statement

If Statement / Structure This allows a program to run different code based on a condition. They require: 1. The condition to test for. 2. The code to run if the condition is true. 3. The code to run if the condition is false. (Optional) Example: 1. Condition: If my name is Dave Shin. 2. If True: Print “You are the teacher!” 3. If False: Print “You are a student!”

Example of an If Statement Example: 1. Condition: If my name is Dave Shin. 2. If True: Print “You are the teacher!” 3. If False: Print “You are a student!” Keywords Condition – Must be in parenthesis! Code to run if True Code to run if False

Examples: IF Statements - 1 If the condition is TRUE, then execute 1 line of code. If the condition is TRUE, then execute 1 line of code. If the condition is FALSE, then execute 1 line of code.

Examples: IF Statements - 2 If the condition is TRUE, then execute multiple lines of code. If the condition is FALSE, then execute multiple lines of code. If the condition is TRUE, then execute 1 line of code. If the condition is FALSE, then execute 1 line of code.

Nested IF Statements Check for different conditions.

Comparison Operators These work well with primitive data types These do not work well with objects The return of a comparison operator is a boolean (true/false) Equal: ==  Remember to use 2 equal signs! (Using only 1 assigns values) Not Equal: != Less Than: < Less Than or Equal To: <= Greater Than: > Greater Than or Equal To: >=

Comprehension Check If Statements

Comparing Objects - 1 If (x == y) Using == with objects works differently than expected.  Only compares memory addresses to see if they are referencing the same object. Normally, you don’t want this. Prints “Boo!”

Comparing Objects - 2 Normally, you want to compare the data inside of the objects. Use the Object.equals(Object anObject) method to compare the data inside of objects. Example: if (aString.equals(“hello”)) Example: if (name.equals(“Dave”))

Comprehension Check If Statements with String Comparisons

Logical Operators Logical operators return a true or false value as a result of performing an operation on two booleans. 1. AND - & or && 2. OR - | or || 3. NOT - ! Use these to evaluate multiple conditions. Example: We own a restaurant. We want to buy chicken and pigs from large suppliers. Our criteria for finding a supplier could be: (ChickenQuantity > 1000 && PigQuantity > 1000) (ChickenQuantity > 5000 || PigQuantity > 5000)

Logical Operators - AND & or &&  Both conditions must be true to return true. Otherwise, false is returned.  & - Always evaluates both conditions  && - Only evaluates second condition if necessary Condition 1OperatorCondition 2Result TrueAndTrue AndFalse AndTrueFalse AndFalse

Logical Operators - OR | or ||  At least one condition must be true to return true. Otherwise, false is returned.  I - Always evaluates both conditions  II - Only evaluates second condition if necessary Condition 1OperatorCondition 2Result TrueOrTrue OrFalseTrue FalseOrTrue FalseOrFalse

Logical Operators - NOT !  Operates on a single expression  Returns the opposite of the expression. ConditionOperatorResult TrueNotFalse NotTrue

Practice! String name1 = “Dave” String name2 = “Cathy” String name3 = “Jenny” 1. !(name1.equals(“Bob”) && name2.equals(“Cathy”) || name3.equals(“Jenny”)) = ? 2. !(!(name1.equals( “Dave”) || name2.equals(“Bob”))) = ? 3. !(name1.equals(“Dave”)) && !(name2.equals(“Bob”)) = ?

Comprehension Check If Statements with Boolean Logic

Switch Statement / Structure This allows a program to run different code based on a condition. Must evaluate to a Byte, Short, Int or Char data type