Selection Structures Tonga Institute of Higher Education.

Slides:



Advertisements
Similar presentations
MONTEGO BAY HIGH SCHOOL INFORMATION TECHNOLOGY THE EXCEL IF FUNCTION.
Advertisements

Computer Science & Engineering 2111 IF and Boolean Functions 1 CSE 2111 Lecture-IF and Boolean Functions.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Conditional Statements Introduction to Computing Science and Programming I.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
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.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
New Mexico Computer Science For All Booleans and Logic Maureen Psaila-Dombrowski.
CS0004: Introduction to Programming Relational Operators, Logical Operators, and 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.
Decision Structures and Boolean Logic
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Selection Structures Tonga Institute of Higher Education.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Flow of Control Part 1: Selection
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Lesson 5: Working with Formulas and Functions Logical and 3D Formulas.
Introduction to Computer Science – Chapter 4 CSc 2010 Spring 2011 Marco Valero.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Basic Conditions. Challenge: ● Ask the user his/her name ● If it’s “Wally,” jeer him ● Pause video and try on your own.
Georgia Institute of Technology Conditionals – part 2 Barb Ericson Georgia Institute of Technology August 2005.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
Controlling Program Flow with Decision Structures.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
Lesson thirteen Conditional Statement "if- else" ©
5.02B Decision Making Structure (part 2). Compound Boolean Expressions.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Selection Part 2 Using Logical Operators, how strings are compared and random numbers.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Learning Javascript From Mr Saem
Silberschatz and Galvin  C Programming Language Decision making in C Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
 Type Called bool  Bool has only two possible values: True and False.
Computer Science 1000 LOGO II. Boolean Expressions like Excel and Scratch, LOGO supports three Boolean operators less than (
Control Flow (Python) Dr. José M. Reyes Álamo.
Chapter 6 More Conditionals and Loops
Topics The if Statement The if-else Statement Comparing Strings
Expressions and Control Flow in JavaScript
Topics The if Statement The if-else Statement Comparing Strings
Pages:51-59 Section: Control1 : decisions
Logical Operations In Matlab.
Selection Statements.
Computer Science Core Concepts
Topics The if Statement The if-else Statement Comparing Strings
CS2011 Introduction to Programming I Selections (I)
Relational Operators.
CHAPTER 5: Control Flow Tools (if statement)
Pages:51-59 Section: Control1 : decisions
Presentation transcript:

Selection Structures Tonga Institute of Higher Education

Introduction Programs must be able to adapt to conditions encountered when the program is running. Selection structures allow programs to adapt to different conditions.  IF…Else Statement  Select Statement

Source Code Execution Order Generally, source code is run in order But we don’t always need to 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 Statement  Select Statement

If Statement / Structure This allows a program to run different code based on a condition. They require 3 things: 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. Example: 1. Condition: If my name is Dave. 2. If True: Print “Your name is Dave!” 3. If False: Print “Your name is not Dave!”

If Statement Overview Example: 1. Condition: If my name is Dave. 2. If True: Print “Your name is Dave!” 3. If False: Print “Your name is not Dave!” Keywords are blue Code to run if True Code to run if False Keywords are blue

IF Statements - 1 You can execute many lines of code

IF Statements - 2 You don’t need an Else statement

Nested If Statements Check for different conditions.

Comparison Operators The return of a comparison operator is a boolean (true/false) Equal: = Not Equal: <> Less Than: < Less Than or Equal To: <= Greater Than: > Greater Than or Equal To: >=

Demonstration If Statements

Logical Operators Logical operators return a true or false value as a result of performing an operation on two booleans. 1. And 2. Or 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 And PigQuantity > 1000 Or ChickenQuantity > 5000 Or PigQuantity > 5000

Logical Operators - And And  Both conditions must be true to return true. Otherwise, false is returned. 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. Condition 1OperatorCondition 2Result TrueOrTrue OrFalseTrue FalseOrTrue FalseOrFalse

Practice! Dim Name1 as String = “Dave” Dim Name2 as String = “Cathy” Dim Name3 as String = “Jenny” 1. (Name1 = “Bob” And Name2 = “Cathy”) Or Name3 = “Jenny” 2. Name1 = “Dave” Or Name2 <> “Bob” 3. (Name1 <> “Dave” And Name2 = “Bob”) Or Name3 = “Jenny”

Demonstration If Statements with Boolean Logic

Select Statement / Structure This allows a program to run different code based on a condition. Improves readability of very long nested if statements

Demonstration Select Statements