Control Flow: Branching booleans and selection statements CS GMU

Size: px
Start display at page:

Download "Control Flow: Branching booleans and selection statements CS GMU"

Transcription

1 Control Flow: Branching booleans and selection statements CS GMU

2 Topics booleans selection statements: if if-else if-elif(s) if-elif(s)-else flow charts 2

3 Booleans

4 Booleans - examples Booleans are either True or False. We create them in many ways. 3<4 True 17%2==0 False 2<5 or 10>20 True not True False "hello"=="hello" False

5 Booleans definition bool is another Python type. It has exactly two values: True, False. many operators and functions will result in a boolean value they can be used to make decisions: if an expression results in True, do one thing (run one block of code); if False, do another thing (go to a different place in code)

6 Booleans operators three main operations that we do with booleans: and, or, not expr1 and expr2: are both exprs True? expr1 or expr2: is at least one expr True? not expr: switch between True/False

7 Booleans truth tables We can exhaustively describe all combinations of inputs for and, or, and not. x y x and y x y x or y x not x False False False False False False False True False True False False True True True False True False False True False True True True True True True True

8 Booleans more operators numbers and other types of values can be used to generate booleans with various operators: operator meaning arguments a < b less than numbers a <= b less than or equal numbers a > b greater than numbers a >= b greater than or equal numbers a == b equality check anything! a!= b inequality check anything!

9 Poll 2.1 (boolean expressions) 1.Given this code, answer each with the result of the expression. x=2 y=4 a) (x<y) and b b=true b) (not b)==false c) 3>y and x<=y d) "test" == "Test" 2. Only knowing that x, y, and z are integers, what is the result of the following expression? (not (x<y and y<z)) == (x>=y or y>=z))

10 Chained Comparisons Python is a bit unique in allowing chained comparisons: x<y<=z is equivalent to (x<y) and (y<=z) short circuiting: from left to right, if any relation in the chain is False, we know the overall answer is False, so no further relations are checked. (above: if x<y is False, don't check y<=z)

11 Impostors! Python will happily and silently (and unfortunately) let us use non-booleans where boolean values are expected zero numbers are treated as False non-zero numbers are all treated as True other non-booleans have boolean interpretations too Suggestion: try to only use actual boolean expressions where booleans are needed!

12 TRAP Note the difference: x = 5 x == 5 assignment statement boolean expression Python can tell if you put an assignment where an expression should go, but will not complain when an expression is where an assignment should go.

13 more boolean expressions Assume that x=2, y=4, b=true. Simplify each expression. x<y not (3<y) (x<y) and b not (not b) x<y<b "test"=="test" 3>y "test"=="test" 3>y and x<=y not b == y<x 3>y or x<=y x+x == y False == False False == (not b)

14 more boolean expressions Assume that x=2, y=4, b=true. Simplify each expression. True x<y False not (3<y) True (x<y) and b True not (not b) (False!) x<y<b True "test"=="test" False 3>y False "test"=="test" False 3>y and x<=y True not b == y<x True 3>y or x<=y True x+x == y True False == False True False == (not b)

15 Branching

16

17 selection statements we can select different blocks of code to run based on a boolean expression's value: dist = int(input("dist?")) if dist >= 26.2: print("marathon, wow!") print("good run.") if payment>cost: change = payment cost print("change: "+str(change)) else: print("not enough money!") val=int(input("temperature: ")) if val > 80: print("hot today.") elif val >= 65: print("i can manage.") else: print("where's my jacket?")

18 if-statement an if-statement guards a block of code with a boolean expression. if the expression is True, then run the block if the expression is False, then skip the block decision: do I perform or skip this indented block of statements? if expr: stmts

19 if-else statement an if-else statement guards two blocks of code with a boolean expression. if expr is True, then run only the first block if expr is False, then run only the second block decision: which of two blocks of code do I run? if expr: stmts1 else: stmts2

20 if-elif statement an if-elif statement guards two or more blocks of code with multiple boolean expressions. check each boolean expr in order until you find the first True one. Only run corresponding block of code may have a single else block at the end Decision: which of many things to run? no else block: might run none of them if expr1: stmts1 elif expr2: stmts2 elif expr3: stmts3 else: stmtsn

21 Selection statement example 1 val = int(input("number to check: ")) if val==10: print("ten is great!") print("thanks!") we only print "ten is great!" when val currently stores 10; other Ymes, we skip that indented code. "thanks!" always prints, because it follows the if-statement (it isn't indented)

22 Selection statement example 2 val = int(input("number to check: ")) if val==10: print("a: ten is great!") else: print("b: I dislike that number.") print("thanks!") we always print either message A or B, but not both. "thanks!" always prints, because it follows the selecyon statement

23 Selection statement example 3 val = int(input("number to check: ")) if val<10: print("a: small number") elif val==10: print("b: perfect size!") else: print("c: too big.") print("thanks!") we always print exactly one of messages A or B or C, but not mulyple of them, and not zero of them. we could omit the else branch, and for big inputs only "thanks!" would print. "thanks!" always prints, because it follows the selecyon stmt

24 Selection statement example 4 val = int(input("number to check: ")) if val<10: print("a: small number") elif val==10: print("b: perfect size!") elif val<100: print("c: a bit too big.") print("thanks!") we print at most one of messages A or B or C "thanks!" always prints, because it follows the selecyon stmt

25 note: else blocks the else block is an optional addition to a selection statement, whether it has zero, one, or many elif branches. it always defines the "default" behavior: when none of the boolean expressions were True, this else block is what should be run.

26 Poll 2.2 (selection statements) 1. What is the final value of x? x = 5 if x>0: x += 3 if x>6: x += 10 if x>100: x = What is the final value of x? x = 15 if x%7>1: x = x else: x = x + 6 if x%3==0: x = x * 2 3. What is the final value of x? x = 20 if x > 25: x += 100 if x > 15: x += 200 if x > 5: x += 400

27 flow charts branches in flow charts have multiple paths that split and rejoin they can become selection statements: each separate path is an if/elif/else block one separate path may itself contain further subdivisions/splits that rejoin to each other (nested selection statements)

28 selections, flow charts - example guess = int(input("your guess: ")) secret = 42 True get number guess from user too high? False if guess > secret: print("too big.") elif guess < secret: print("too small.") else: print ("correct!") print "too big" True print "too small" too low? False print "correct!" print ("thanks for guessing!") thanks for guessing!

29 practice: flow chart to code #1 True c False if c : s1 s2 s1 s2

30 practice: flow chart to code #2 True c1 False if c1 : s1 True s2 c2 False s1 elif c2: s2 s3 s3

31 practice: flow chart to code #3 True c1 False if c1 : s1 True c2 False s1 elif c2: s2 s3 s2 else: s4 s4 s3

32 practice: flow chart to code #4 s1 s2 s3 True True True s0 c1 c2 c3 False False False s0 if c1: s1 if c2: s2 if c3: s3 s4 what possible paths are there through this code? would this code behave differently if we used elif's for c2 and c3? s4

33 practice: flow chart to code #5 s1 True c1 False s4 if c1: s1 if c2: True c2 False True c3 False s3 s2 s2 s5 s6 else: s4 s3 True s7 c4 False if c3: else: s5 s6 s8 if c4: s7 s9 s9 s8

34 Poll What is the final value of x? x = 10 if x==20: x = 100 elif x>5: if x<50: x += 1 else: x += 4 elif x>=0: x = 3 if x%2==1: x += 1000 (selection statements) 2. Match each usage to a description. #1 if else #2 if elif #3 if elif elif else #4 if if if #5 if a) three separate choices to do or not do separate blocks of code b) select whether to do something or not c) select one or another action or alternatively do nothing d) select one of many blocks of code e) select one of two things to do

35 selection statements recap if: choose whether to run a block of code or not, based upon a boolean expression if-else: choose which of two things to do if-elif(s): choose one of many blocks of code, by finding first True boolean expr. Might choose none of them (all were False) if-elif(s)-else: choose exactly one of many blocks of code, by finding first True boolean expr (or running else block when all were False) flow charts: there are direct mappings between flow chart shapes and code.

Conditionals and Recursion. Python Part 4

Conditionals and Recursion. Python Part 4 Conditionals and Recursion Python Part 4 Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print (remainder) 1 Boolean expressions An expression that

More information

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen 1 Truth Last lecture we learned about the int, float, and string types. Another very important object type in Python is the boolean type. The two reserved keywords True and False are values with type boolean.

More information

Lecture 8: Conditionals & Control Flow (Sections ) CS 1110 Introduction to Computing Using Python

Lecture 8: Conditionals & Control Flow (Sections ) CS 1110 Introduction to Computing Using Python http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 8: Conditionals & Control Flow (Sections 5.1-5.7) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner,

More information

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala Loops and Conditionals HORT 59000 Lecture 11 Instructor: Kranthi Varala Relational Operators These operators compare the value of two expressions and returns a Boolean value. Beware of comparing across

More information

CSE 115. Introduction to Computer Science I

CSE 115. Introduction to Computer Science I CSE 115 Introduction to Computer Science I Progress In UBInfinite? A. Haven't started B. Earned 3 stars in "Calling Functions" C. Earned 3 stars in "Defining Functions" D. Earned 3 stars in "Conditionals"

More information

logical operators and else-if statements

logical operators and else-if statements logical operators and else-if statements Lecture 5 Step 0: TODAY open http://localhost:3000/close -- if this errors that's OK / expected Step 1: Open VSCode and its Integrated Terminal Step 2: npm run

More information

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

Flow of Control. Flow of control The order in which statements are executed. Transfer of control 1 Programming in C Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control

More information

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed Programming in C 1 Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control

More information

Lecture 02 Making Decisions: Conditional Execution

Lecture 02 Making Decisions: Conditional Execution Lecture 02 Making Decisions: Conditional Execution 1 Flow of Control Flow of control = order in which statements are executed By default, a program's statements are executed sequentially, from top to bottom.

More information

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block THE IF STATEMENT The if statement is used to check a condition: if the condition is true, we run a block of statements (called the if-block), elsewe process another block of statements (called the else-block).

More information

boolean Expressions Relational and Equality Operators and if-then-else Conditional Control Statement

boolean Expressions Relational and Equality Operators and if-then-else Conditional Control Statement boolean Expressions Relational and Equality Operators and if-then-else Conditional Control Statement Go to poll.unc.edu Sign-in via this website then go to pollev.com/compunc VSCode: Open Project -> View

More information

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017 Control Structures Lecture 4 COP 3014 Fall 2017 September 18, 2017 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions or function calls

More information

Flow Control. So Far: Writing simple statements that get executed one after another.

Flow Control. So Far: Writing simple statements that get executed one after another. Flow Control So Far: Writing simple statements that get executed one after another. Flow Control So Far: Writing simple statements that get executed one after another. Flow control allows the programmer

More information

CS 115 Lecture 8. Selection: the if statement. Neil Moore

CS 115 Lecture 8. Selection: the if statement. Neil Moore CS 115 Lecture 8 Selection: the if statement Neil Moore Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu 24 September 2015 Selection Sometime we want to execute

More information

Practice with if-then-else. COMP101 Lecture 7 Spring 2018

Practice with if-then-else. COMP101 Lecture 7 Spring 2018 Practice with if-then-else COMP101 Lecture 7 Spring 2018 Announcements PS01 - Due Friday at 11:59pm 3 parts, each in increasing difficulty. You will submit individually for each. Tutoring - Tomorrow from

More information

Control Structures 1 / 17

Control Structures 1 / 17 Control Structures 1 / 17 Structured Programming Any algorithm can be expressed by: Sequence - one statement after another Selection - conditional execution (not conditional jumping) Repetition - loops

More information

Flow Control: Branches and loops

Flow Control: Branches and loops Flow Control: Branches and loops In this context flow control refers to controlling the flow of the execution of your program that is, which instructions will get carried out and in what order. In the

More information

Chapter 8. Statement-Level Control Structures

Chapter 8. Statement-Level Control Structures Chapter 8 Statement-Level Control Structures Levels of Control Flow Within expressions Among program units Among program statements Copyright 2012 Addison-Wesley. All rights reserved. 1-2 Control Structure

More information

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators CS 61A Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions 1 Control Control structures direct the flow of logic in a program. For example, conditionals (if-elif-else) allow a program

More information

Python for Informatics

Python for Informatics Python for Informatics Exploring Information Version 0.0.6 Charles Severance Chapter 3 Conditional execution 3.1 Boolean expressions A boolean expression is an expression that is either true or false.

More information

Text Input and Conditionals

Text Input and Conditionals Text Input and Conditionals Text Input Many programs allow the user to enter information, like a username and password. Python makes taking input from the user seamless with a single line of code: input()

More information

NESTED IF STATEMENTS AND STRING/INTEGER CONVERSION

NESTED IF STATEMENTS AND STRING/INTEGER CONVERSION LESSON 15 NESTED IF STATEMENTS AND STRING/INTEGER CONVERSION OBJECTIVE Learn to work with multiple criteria if statements in decision making programs as well as how to specify strings versus integers in

More information

Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science Python Programming: An Introduction to Computer Science Chapter 7 Decision Structures Python Programming, 2/e 1 Simple Decisions So far, we ve viewed programs as sequences of instructions that are followed

More information

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators CS 61A Control and Environments Fall 2017 Discussion 1: August 30, 2017 1 Control Control structures direct the flow of logic in a program. For example, conditionals (if-elif-else) allow a program to skip

More information

Computer Science 217

Computer Science 217 Computer Science 17 Midterm Exam March 5, 014 Exam Number 1 First Name: Last Name: ID: Class Time (Circle One): 1:00pm :00pm Instructions: Neatly print your names and ID number in the spaces provided above.

More information

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Python review. 1 Python basics. References. CS 234 Naomi Nishimura Python review CS 234 Naomi Nishimura The sections below indicate Python material, the degree to which it will be used in the course, and various resources you can use to review the material. You are not

More information

Scripting Languages. Python basics

Scripting Languages. Python basics Scripting Languages Python basics Interpreter Session: python Direct conversation with python (>>>) Python 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on linux Type "help", "copyright",

More information

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives CS 6A Scheme Fall 208 Discussion 8: October 24, 208 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

More information

Motivation for typed languages

Motivation for typed languages Motivation for typed languages Untyped Languages: perform any operation on any data. Example: Assembly movi 5 r0 // Move integer 5 (some representation) to r0 addf 3.6 r0 // Treat bit representation in

More information

C ONTROL AND H IGHER O RDER F UNCTIONS

C ONTROL AND H IGHER O RDER F UNCTIONS Name: Date: Period: Name 2: Name 3: Name 4: 20 points C ONTROL AND H IGHER O RDER F UNCTIONS (Review questions from readings and labs) 1 Instructions: Complete all 9S CIENCE discussion C OMPUTER 61A questions.

More information

CSE 115. Introduction to Computer Science I

CSE 115. Introduction to Computer Science I CSE 115 Introduction to Computer Science I Announcements Lab activites/lab exams submit regularly to autograder.cse.buffalo.edu Announcements Lab activites/lab exams submit regularly to autograder.cse.buffalo.edu

More information

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017 SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme

More information

ME30 Lab3 Decisions. February 20, 2019

ME30 Lab3 Decisions. February 20, 2019 ME30 Lab3 Decisions February 20, 2019 0.0.1 ME 30 Lab 4 - Conditional Program Execution ME 30 ReDev Team 2018-07-06 Description and Summary: This lab introduces the programming concept of decision-making,

More information

Circuit analysis summary

Circuit analysis summary Boolean Algebra Circuit analysis summary After finding the circuit inputs and outputs, you can come up with either an expression or a truth table to describe what the circuit does. You can easily convert

More information

The Practice of Computing Using PYTHON. Chapter 2. Control. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

The Practice of Computing Using PYTHON. Chapter 2. Control. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Practice of Computing Using PYTHON William Punch Richard Enbody Chapter 2 Control 1 Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Control: A Quick Overview 2 Selection

More information

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control Chapter 2 Control, Quick Overview Control Selection Selection Selection is how programs make choices, and it is the process of making choices that provides a lot of the power of computing 1 Python if statement

More information

Conditionals & Control Flow

Conditionals & Control Flow CS 1110: Introduction to Computing Using Python Lecture 8 Conditionals & Control Flow [Andersen, Gries, Lee, Marschner, Van Loan, White] Announcements: Assignment 1 Due tonight at 11:59pm. Suggested early

More information

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems. Plan for the rest of the semester: Programming We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems. We saw earlier that computers

More information

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

More information

Chapter 8. Statement-Level Control Structures

Chapter 8. Statement-Level Control Structures Chapter 8 Statement-Level Control Structures Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions 1-2 Levels of Control Flow Within

More information

(Python) Chapter 3: Repetition

(Python) Chapter 3: Repetition (Python) Chapter 3: Repetition 3.1 while loop Motivation Using our current set of tools, repeating a simple statement many times is tedious. The only item we can currently repeat easily is printing the

More information

Crude Video Game Simulator Algorithm

Crude Video Game Simulator Algorithm Crude Video Game Simulator Algorithm The following program will simulate free games at an arcade. The player will enter his/her score for 5 levels of play. The sum of these is their game score. If this

More information

CS 61A Control and Environments Spring 2018 Discussion 1: January 24, Control. If statements. Boolean Operators

CS 61A Control and Environments Spring 2018 Discussion 1: January 24, Control. If statements. Boolean Operators CS 61A Control and Environments Spring 2018 Discussion 1: January 24, 2018 1 Control Control structures direct the flow of logic in a program. For example, conditionals (if-elif-else) allow a program to

More information

boolean & if-then-else

boolean & if-then-else boolean & if-then-else Lecture 03 Step 1: Open VSCode and its Integrated Terminal Step 2: npm run pull Step 3: npm run start Step 4: Open another tab to pollev.com/comp110 Assignments Out Problem Set 0

More information

CONTROL AND ENVIRONMENTS 1

CONTROL AND ENVIRONMENTS 1 CONTROL AND ENVIRONMENTS 1 COMPUTER SCIENCE 61A September 1, 2016 1 Control Control structures direct the flow of logic in a program. For example, conditionals (ifelif-else) allow a program to skip sections

More information

Quiz. Introduction: Python. In this project, you ll make a quiz game to challenge your friends. Activity Checklist.

Quiz. Introduction: Python. In this project, you ll make a quiz game to challenge your friends. Activity Checklist. Python 1 Quiz All Code Clubs must be registered. Registered clubs appear on the map at codeclub.org.uk - if your club is not on the map then visit jumpto.cc/18cplpy to find out what to do. Introduction:

More information

Test #2 October 8, 2015

Test #2 October 8, 2015 CPSC 1040 Name: Test #2 October 8, 2015 Closed notes, closed laptop, calculators OK. Please use a pencil. 100 points, 5 point bonus. Maximum score 105. Weight of each section in parentheses. If you need

More information

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017 Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017 K. Zhang BMGT 404 The modulus operator It works on integers

More information

Decision Structures CSC1310. Python Programming, 2/e 1

Decision Structures CSC1310. Python Programming, 2/e 1 Decision Structures CSC1310 Python Programming, 2/e 1 Simple Decisions Decision structures, which are statements that allow a program to execute different sequences of instructions for different cases,

More information

Comp 151. Control structures.

Comp 151. Control structures. Comp 151 Control structures. admin quiz this week believe it or not only 2 weeks from exam. one a week each week after that. idle debugger Debugger: program that will let you look at the program as it

More information

INTERMEDIATE LEVEL PYTHON PROGRAMMING SELECTION AND CONDITIONALS V1.0

INTERMEDIATE LEVEL PYTHON PROGRAMMING SELECTION AND CONDITIONALS V1.0 INTERMEDIATE LEVEL PYTHON PROGRAMMING SELECTION AND CONDITIONALS V1.0 OCTOBER 2014 Python Selection and Conditionals 1 SELECTION AND CONDITIONALS WHAT YOU MIGHT KNOW ALREADY You will probably be familiar

More information

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful

More information

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Chapter 8. Statement-Level Control Structures ISBN

Chapter 8. Statement-Level Control Structures ISBN Chapter 8 Statement-Level Control Structures ISBN 0-321-49362-1 Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions Copyright 2012

More information

Chapter 8. Statement-Level Control Structures

Chapter 8. Statement-Level Control Structures Chapter 8 Statement-Level Control Structures Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions Copyright 2009 Addison-Wesley.

More information

Conditionals: Making Choices

Conditionals: Making Choices Announcements ry to get help from me and tutors Reading assignment for this week: Chapters 5 and 6 of Downey Conditionals: Making Choices When you see a page on the web, be sure to reload it to see the

More information

CS100 Spring 2012 Midterm 1 Practice

CS100 Spring 2012 Midterm 1 Practice CS100 Spring 2012 Midterm 1 Practice This practice midterm gives you a guide to the subject matter and format the first midterm of the semester. Assignment: Do the practice midterm and submit it via Moodle.

More information

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015 SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Introduction to Programming

Introduction to Programming Introduction to Programming Python Lab 7: if Statement PythonLab7 lecture slides.ppt 14 November 2017 Ping Brennan (p.brennan@bbk.ac.uk) 1 Getting Started Create a new folder in your disk space with the

More information

Making Decisions In Python

Making Decisions In Python Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action. Decision Making Is All About Choices My next vacation? Images:

More information

cs1114 REVIEW of details test closed laptop period

cs1114 REVIEW of details test closed laptop period python details DOES NOT COVER FUNCTIONS!!! This is a sample of some of the things that you are responsible for do not believe that if you know only the things on this test that they will get an A on any

More information

Most of the class will focus on if/else statements and the logical statements ("conditionals") that are used to build them. Then I'll go over a few

Most of the class will focus on if/else statements and the logical statements (conditionals) that are used to build them. Then I'll go over a few With notes! 1 Most of the class will focus on if/else statements and the logical statements ("conditionals") that are used to build them. Then I'll go over a few useful functions (some built into standard

More information

Conditional Expressions

Conditional Expressions Conditional Expressions Boolean Expressions: An expression that evaluates to either TRUE or FALSE. The most common types of boolean expressions are those that use relational operators. The general syntax

More information

Chapter 4 The If Then Statement

Chapter 4 The If Then Statement The If Then Statement Conditional control structure, also called a decision structure Executes a set of statements when a condition is true The condition is a Boolean expression For example, the statement

More information

The following expression causes a divide by zero error:

The following expression causes a divide by zero error: Chapter 2 - Test Questions These test questions are true-false, fill in the blank, multiple choice, and free form questions that may require code. The multiple choice questions may have more than one correct

More information

CS100: CPADS. Decisions. David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania

CS100: CPADS. Decisions. David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania CS100: CPADS Decisions David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania James Moscola Decisions Just like a human, programs need to make decisions - Should turtle turn

More information

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal An Easy Intro to Python & Programming Don t just buy a new video game, make one. Don t just download the latest app, help design it. Don t just play on your phone, program it. No one is born a computer

More information

Boolean algebra. June 17, Howard Huang 1

Boolean algebra. June 17, Howard Huang 1 Boolean algebra Yesterday we talked about how analog voltages can represent the logical values true and false. We introduced the basic Boolean operations AND, OR and NOT, which can be implemented in hardware

More information

Chapter 8 Statement-Level Control Structures

Chapter 8 Statement-Level Control Structures Chapter 8 Statement-Level Control Structures In Chapter 7, the flow of control within expressions, which is governed by operator associativity and precedence rules, was discussed. This chapter discusses

More information

Conditional Expressions and Decision Statements

Conditional Expressions and Decision Statements Conditional Expressions and Decision Statements June 1, 2015 Brian A. Malloy Slide 1 of 23 1. We have introduced 5 operators for addition, subtraction, multiplication, division, and exponentiation: +,

More information

Chapter 2. Flow of Control. Copyright 2016 Pearson, Inc. All rights reserved.

Chapter 2. Flow of Control. Copyright 2016 Pearson, Inc. All rights reserved. Chapter 2 Flow of Control Copyright 2016 Pearson, Inc. All rights reserved. Learning Objectives Boolean Expressions Building, Evaluating & Precedence Rules Branching Mechanisms if-else switch Nesting if-else

More information

Comp 151. Control structures.

Comp 151. Control structures. Comp 151 Control structures. admin For these slides read chapter 7 Yes out of order. Simple Decisions So far, we ve viewed programs as sequences of instructions that are followed one after the other. While

More information

CS1 Lecture 5 Jan. 25, 2019

CS1 Lecture 5 Jan. 25, 2019 CS1 Lecture 5 Jan. 25, 2019 HW1 due Monday, 9:00am. Notes: Do not write all the code at once before starting to test. Take tiny steps. Write a few lines test... add a line or two test... add another line

More information

Chapter 5 : Informatics practices. Conditional & Looping Constructs. Class XI ( As per CBSE Board)

Chapter 5 : Informatics practices. Conditional & Looping Constructs. Class XI ( As per CBSE Board) Chapter 5 : Informatics practices Class XI ( As per CBSE Board) Conditional & Looping Constructs Control Statements Control statements are used to control the flow of execution depending upon the specified

More information

MATLAB Operators, control flow and scripting. Edited by Péter Vass

MATLAB Operators, control flow and scripting. Edited by Péter Vass MATLAB Operators, control flow and scripting Edited by Péter Vass Operators An operator is a symbol which is used for specifying some kind of operation to be executed. An operator is always the member

More information

CONTROL AND HIGHER ORDER FUNCTIONS 1

CONTROL AND HIGHER ORDER FUNCTIONS 1 CONTROL AND HIGHER ORDER FUNCTIONS 1 COMPUTER SCIENCE 61A September 3, 2015 1 Control Control structures direct the flow of logic in a program. For example, conditionals (ifelif-else) allow a program to

More information

Programming in Python 3

Programming in Python 3 Programming in Python 3 Programming transforms your computer from a home appliance to a power tool Al Sweigart, The invent with Python Blog Programming Introduction Write programs that solve a problem

More information

61A Lecture 3. Friday, September 5

61A Lecture 3. Friday, September 5 61A Lecture 3 Friday, September 5 Announcements There's plenty of room in live lecture if you want to come (but videos are still better) Please don't make noise outside of the previous lecture! Homework

More information

Lecture 04 More Iteration, Nested Loops. Meet UTA Jarrett s dog Greta, lying in her nest

Lecture 04 More Iteration, Nested Loops. Meet UTA Jarrett s dog Greta, lying in her nest Lecture 4 More Iteration, Nested Loops Meet UTA Jarrett s dog Greta, lying in her nest Indefinite Loops Rest Chase tail Got tail! Almost got it... Based in part on notes from the CS-for-All curriculum

More information

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics: Computers and FORTRAN Language Fortran 95/2003 Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes Topics: - Program Design - Logical Operators - Logical Variables - Control Statements Any FORTRAN program

More information

CS1 Lecture 5 Jan. 26, 2018

CS1 Lecture 5 Jan. 26, 2018 CS1 Lecture 5 Jan. 26, 2018 HW1 due Monday, 9:00am. Notes: Do not write all the code at once (for Q1 and 2) before starting to test. Take tiny steps. Write a few lines test... add a line or two test...

More information

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration STATS 507 Data Analysis in Python Lecture 2: Functions, Conditionals, Recursion and Iteration Functions in Python We ve already seen examples of functions: e.g., type()and print() Function calls take the

More information

5. Control Statements

5. Control Statements 5. Control Statements This section of the course will introduce you to the major control statements in C++. These control statements are used to specify the branching in an algorithm/recipe. Control statements

More information

Case by Case. Chapter 3

Case by Case. Chapter 3 Chapter 3 Case by Case In the previous chapter, we used the conditional expression if... then... else to define functions whose results depend on their arguments. For some of them we had to nest the conditional

More information

Python Games. Session 1 By Declan Fox

Python Games. Session 1 By Declan Fox Python Games Session 1 By Declan Fox Rules General Information Wi-Fi Name: CoderDojo Password: coderdojowireless Website: http://cdathenry.wordpress.com/ Plans for this year Command line interface at first

More information

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops PRG PROGRAMMING ESSENTIALS 1 Lecture 2 Program flow, Conditionals, Loops https://cw.fel.cvut.cz/wiki/courses/be5b33prg/start Michal Reinštein Czech Technical University in Prague, Faculty of Electrical

More information

Getting Started Values, Expressions, and Statements CS GMU

Getting Started Values, Expressions, and Statements CS GMU Getting Started Values, Expressions, and Statements CS 112 @ GMU Topics where does code go? values and expressions variables and assignment 2 where does code go? we can use the interactive Python interpreter

More information

Logical and Bitwise Expressions

Logical and Bitwise Expressions Logical and Bitwise Expressions The truth value will set you free. 1 Expression Constant Variable Unary Operator Binary Operator (expression) Function Invocation Assignment: = Prefix: +, -, ++, --,!, ~

More information

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016 CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Spring 2016 Type-checking (Static) type-checking can reject a program before it runs to prevent the possibility of some errors A feature

More information

Strings and Testing string methods, formatting testing approaches CS GMU

Strings and Testing string methods, formatting testing approaches CS GMU Strings and Testing string methods, formatting testing approaches CS 112 @ GMU Topics string methods string formatting testing, unit testing 2 Some String Methods (See LIB 4.7.1) usage: stringexpr. methodname

More information

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington Selection s CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1 Book reference Book: The practice of Computing Using Python 2-nd edition Second hand book

More information

CS 2316 Exam 1 Spring 2014

CS 2316 Exam 1 Spring 2014 CS 2316 Exam 1 Spring 2014 Name : Grading TA: Integrity: By taking this exam, you pledge that this is your work and you have neither given nor received inappropriate help during the taking of this exam

More information

How Do Robots Find Their Way?

How Do Robots Find Their Way? How Do Robots Find Their Way? Conditionals and Repetition http://en.wikipedia.org/wiki/file:cyclope_robot.jpg http://www.youtube.com/watch?v=_l9rklaskwu Learning Objectives Learn basic programming concepts

More information

CS Boolean Statements and Decision Structures. Week 6

CS Boolean Statements and Decision Structures. Week 6 CS 17700 Boolean Statements and Decision Structures Week 6 1 Announcements Midterm 1 is on Feb 19 th, 8:00-9:00 PM in PHYS 114 and PHYS 112 Let us know in advance about conflicts or other valid makeup

More information

Intro. Scheme Basics. scm> 5 5. scm>

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

Conditionals. C-START Python PD Workshop

Conditionals. C-START Python PD Workshop Boolean Data A boolean is a type of data which can be one of two values: True or False Boolean Data A boolean is a type of data which can be one of two values: True or False mybool = True print(mybool)

More information

Loops In Python. In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.

Loops In Python. In this section of notes you will learn how to rerun parts of your program without having to duplicate the code. Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code. The Need For Repetition (Loops) Writing out a simple counting program (1

More information

CS177 Recitation. Functions, Booleans, Decision Structures, and Loop Structures

CS177 Recitation. Functions, Booleans, Decision Structures, and Loop Structures CS177 Recitation Functions, Booleans, Decision Structures, and Loop Structures Functions Collection of instructions that perform a task as: o Printing your name and course. o Calculating the average of

More information

Intro. Comparisons. > x > y if and only if x is bigger than y. < x < y if and only if x is smaller than y.

Intro. Comparisons. > x > y if and only if x is bigger than y. < x < y if and only if x is smaller than y. Intro Recall that and are the two booleans in Python. It turns out they're pretty important. For instance, what if we want to do something, but only if a particular condition is? What if we want to repeat

More information