Selection 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

Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
CS0007: Introduction to Computer Programming
Conditional Statements Introduction to Computing Science and Programming I.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Python quick start guide
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
HOMEWORK REVIEW This is an if else statement layout if (condition) { code to be executed if condition is true; } else { code to be executed if condition.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
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.
Agenda Basic Logic Purpose if statement if / else statement
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 4 – Logical Operators & Branching.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
Basic Conditions. Challenge: ● Ask the user his/her name ● If it’s “Wally,” jeer him ● Pause video and try on your own.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
31/01/ Selection If selection construct.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
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.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Learning Javascript From Mr Saem
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Computer Science 1000 LOGO II. Boolean Expressions like Excel and Scratch, LOGO supports three Boolean operators less than (
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Introduction to Decision Structures and Boolean Variables
Control Flow (Python) Dr. José M. Reyes Álamo.
CS0007: Introduction to Computer Programming
INC 161 , CPE 100 Computer Programming
The switch Statement, and Introduction to Looping
Python: Control Structures
Topics The if Statement The if-else Statement Comparing Strings
Simple Control Structures
Control Structures.
Microsoft Visual Basic 2005 BASICS
Selection By Ramin && Taimoor
Topics The if Statement The if-else Statement Comparing Strings
Lesson 8: Boolean Expressions and "if" Statements
Conditions and Ifs BIS1523 – Lecture 8.
Computers & Programming Languages
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Repetition Control Structure
Visual Basic – Decision Statements
Introduction to Decision Structures and Boolean Variables
Learning Intention I will learn about programming using selection (making choices) with one condition.
Programming Concepts and Database
CS2011 Introduction to Programming I Selections (I)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CHAPTER 5: Control Flow Tools (if statement)
Repetition Statements (Loops) - 2
Boolean Logic.
Presentation transcript:

Selection Statements

Boolean Expressions Are statements that evaluate to either a true or false value; Are useful in computing and programming because computers think in terms of 1’s and 0’s, which can represent true and false.

Boolean Expressions Can be a simple expression if it is a single statement involving a relation, equality, or not statement; var isTeacherAGamer : boolean := true var isWeatherNice : boolean := isSkyBlue var isLazy : boolean := not(isHardWorker) Can be a compound expression if there are several simple expressions connected using the and and or operators willBallBounce := isBallRubber or isGroundRubber

Our Programs So Far... procedure GetSomeData() var input : string put “Please enter the data” get input put “You inputted the data: “ + input end GetSomeData Variable value and output change with the user input

Our Programs So Far... Our programs so far cannot make decisions E.g., the user can’t decide to just exit instead of printing their input Our programs follow the typical Top-to-bottom flow, we need to give the programmer control

Selection Statements We usually want our programs to run different lines of code depending on different variable values. This is where we use selection statements. Selection statements are used to control the flow of the program, so are also called control structures.

Comparison Operators Before we can use this control structure we need to understand how they give us control. They do so by comparing values. The following is a list of all comparisons in Turing Operator Meaning Example Evaluates to = Equal to teacherName = “Mr.Jackson” False >  Greater than 6 > 3 True <  Less than 5 < 11 >= Greater than or equal to 23 >= 23 <= Less than or equal to 4 <= 21 not= NOT equal to 3 not= 3

If-statements (the detour) Syntax if (<test-expression>) then %Insert code between the if and end if lines %This code is executed if and only if the Boolean %expression in the brackets evaluates to true. end if

What is a test-expression A test-expression is any expression that evaluates to true or false, usually a comparison of values E.g. %In this example, if the user entered a value of 15 or less the put %command would be executed, otherwise it would be skipped completely if (userAge < 16) then put “Sorry, no license for you” end if

Examples var understood : boolean put “Is the topic understood?” get understood if ( understood = false) then %User did not understand put "We need more time" end if

if-else statements (The fork in the road) Syntax if (<test-expression>) then % Code between the if and else lines that is to be executed if the Boolean % expression in the brackets is true else % Code inside the else and end if lines that is to be executed if the Boolean % expression in the brackets is false end if Only ONE of the branches (code blocks) will run depending on whether the test-expression evaluates to true or false.