Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

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.
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
CS0007: Introduction to Computer Programming
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Computer Science 1620 Loops.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Chapter 4 Making Decisions
Decisions in Python elif. A new keyword elif A contraction of “else if” Used to tie two if statements (or more) together into one structure Syntax – elif,
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Conditional Statements While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make.
Decision Structures and Boolean Logic
Chapter 3 Making Decisions
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
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)
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Chapter 4 Introduction to Control Statements (Branching Statements) Section 1 - Additional Java Operators Section 2 - If Statements Section 3 - If-Else.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Flow of Control Part 1: Selection
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Chapter 4 Introduction to Control Statements Section 1 - Additional Java Operators Section 2 - If Statements Section 3 - If-Else Statements Section 4.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Decisions in Python Bools and simple if statements.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Copyright 2003 Scott/Jones Publishing Making Decisions.
TK 1914 : C++ Programming Control Structures I (Selection)
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
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 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
CPS120 Introduction to Computer Science Iteration (Looping)
Java Programming Fifth Edition Chapter 5 Making Decisions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Decision Making and Branching
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
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.
Chapter 4 – C Program Control
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
JavaScript: Control Statements.
Selection By Ramin && Taimoor
Topics The if Statement The if-else Statement Comparing Strings
Conditions and Ifs BIS1523 – Lecture 8.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Chapter 8: More on the Repetition Structure
Selection Statements.
Chapter 2 Programming Basics.
Presentation transcript:

Selection Statements

Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements

Review – if statements var number : int get number % Convert a negative number into a positive if ( number < 0 ) then number := number * -1 end if Boolean expression Code to execute if the Boolean expression is true

Review – if-else statements var number : int get number if ( number < 0 ) then % Convert the number into a positive number number := number * -1 elsif ( number = 0 ) then % Exit the program quit end if Boolean expression Code to execute if the Boolean expression is true Code to execute if the Boolean expression is false

Nesting Nesting means putting one conditional statement inside of another statement. We can use nesting to make our code shorter and easier to read by ▫reducing the number of conditions checked, or ▫removing repeated lines of code.

Nesting There is no limit to the amount of nesting you can do, although after 4 or more nesting levels it may become difficult to understand the code. Occasionally it is better to do nesting in order to group certain computations together. In the next example, there is the case where we want to exit the program, and the other case is where we actually do processing.

` var number : int get number if ( number < 0 ) then % Convert to a positive number number := number * -1 put number elsif ( number = 0 ) then % Exit program if number is 0 quit else % Double the number number := number *2 put number end if var number : int get number if ( number = 0 ) then % Exit the program if number is 0 quit else % Process number if ( number < 0 ) then % Convert to a positive number number := number * -1 else % Double the number number := number *2 end if put number end if

Nesting Nesting can also be used to make code easier to read by splitting up the logical AND into several simpler Boolean expressions:

Unnested Boolean expression Original if ( age > 14 and grade > 9 and isInterested = true and isNiceOutside = false and isFriday = false) then % Student is old enough to understand, % interested, and not distracted put "You might be listening" end if

Nested Boolean Expression Nested if ( age > 14 and grade > 9 ) then % Student is old enough to understand if ( isInterested = true) then % Student is interested if ( isNiceOutside = false and isFriday = false) then % Student is not distracted put "You might be listening" end if

Question Why is nesting conditional statements inside of other conditional statements like using logical AND? Each nested conditional statement is entered only if the previous level of nesting is true. In order for the inner statements to be executed, every level of nesting needs to be true. This is like using AND to put them all together.

Discussion – What Happens in Each? % Process number if ( number < 0 ) then % Convert to a positive number number := number * -1 elsif ( number > 0 ) then % Double the number number := number *2 end if % Process number if ( number < 0 ) then % Convert to a positive number number := number * -1 end if if ( number > 0 ) then % Double the number number := number *2 end if

What Happens in Each In the left code segment, a negative number is converted into a positive number only. A number that starts off as positive is the only number that will get doubled. In the right code segment, both negative and positive numbers will get doubled.

Discussion When do you use separate conditional statements and when do you use if-elsif structures Use separate if statements only when you want every condition to be checked every time.

Discussion Use elsif statements for: 1) Mutually exclusive Boolean expressions. This means that if only one of the Boolean expressions can be true at one time. For example, a number cannot be both positive and negative at the same time. In this case, only one branch can be true anyway, so there is no point in using several if statements.

Discussion 2)Code that has differing levels of importance. Remember that the top-most Boolean expressions get evaluated first and the ones below it are skipped if any expression evaluates to true. Therefore, you can make certain some code gets executed with higher priority than those below it.

case statements These are less often used than conditional statements because they are less flexible. Instead of using Boolean expressions, you compare the switch against various cases. You can only use the data types int, string, and boolean in case statements. Can have many different cases

case statements Syntax case of label : % Code executed if constant value = testVariable value label : % Code executed if constant value 2 = testVariable value label : % like an else block, code that executes when all previous cases failed end case

case statements You can only compare equality against variables for case statements, and the data types that can be used are limited, that means no, etc. ▫Less flexible than if statements

var input : string put “Please enter a command” get input case input of label “x”: quit label “c”: put “ You have chosen to continue” label “d”: put“ You have chosen to self-destruct. Good-bye” label : % used like an else block put “Unknown selection was made” end case

if-elsif-else vs. Case Why would we use Case instead of if-elsif ▫Don’t they do the same thing? Well, yes and no... ▫They end up with the same results, however they get there at different speeds ▫And they look entirely different  Ex. Demo Code on Moodle

Choices as Programmers Programmers are faced with choices everyday as there is never only one way of doing something. ▫In all cases there is only two reasons every programmer considers when making this decision:  Readability  How clean and easy is code to read and understand at a glance  Efficiency (Memory Space and Time)  How fast and little memory does a certain technique take?

Case Both Case and if-elsif statements will only go into one of the possible cases. ▫The difference is that for a Case statement it does not check each case, it automatically goes to the correct one (Efficiency) ▫if-elsif statements must look at each one until the first correct one is found.