1 CS 177 Week 5 Recitation Booleans and Control Flow.

Slides:



Advertisements
Similar presentations
Objectives Knows and understand about another kind of flowchart’s symbols (looping) Have ability to create structured solution on simple problem(s) Have.
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.
Chapter 3 section 2. Please form your groups The 1 st column represents all possibilities for the statement that can be either True or False. The 2 nd.
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Python Programming Language
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Boolean Algebra Computer Science AND (today is Monday) AND (it is raining) (today is Monday) AND (it is not raining) (today is Friday) AND (it is.
Introduction Chapter 4: Control structures. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 Objectives What are control structures Relational.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Geography 465 Assignments, Conditionals, and Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Sentinel Logic Assumes while loops and input statements.
Python Control of Flow.
Truth Tables for Negation, Conjunction, and Disjunction.
Python quick start guide
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Chapter 1 The Logic of Compound Statements. Section 1.1 Logical Form and Logical Equivalence.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1.
MATH 102 Contemporary Math S. Rook
COMP 116: Introduction to Scientific Programming Lecture 28: Midterm #2 Review.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
Logical Form and Logical Equivalence Lecture 2 Section 1.1 Fri, Jan 19, 2007.
If statements while loop for loop
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
CMSC 150 CONDITIONAL EXECUTION CS 150: Mon 16 Jan 2012.
Decisions in Python Bools and simple if statements.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Boolean Algebra. Logical Statements A proposition that may or may not be true:  Today is Monday  Today is Sunday  It is raining.
How do I show that two compound propositions are logically equivalent?
6.1 Logic Logic is not only the foundation of mathematics, but also is important in numerous fields including law, medicine, and science. Although the.
Logical Form and Logical Equivalence Lecture 1 Section 1.1 Wed, Jan 12, 2005.
Boolean Algebra Monday/Wednesday 7th Week. Logical Statements Today is Friday AND it is sunny. Today is Friday AND it is rainy. Today is Monday OR it.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 3 Object Interaction.  To construct interesting applications it is not enough to build individual objects  Objects must be combined so they.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
Xiaojuan Cai Computational Thinking 1 Lecture 8 Loop Structure Xiaojuan Cai (蔡小娟) Fall, 2015.
Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox.
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Logical Thinking CS 104 9/12/11. Agenda Today  College Board survey reminder  Note: Simple “how to” guide on Scratch posted on eLearning  Review HW.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Expressions Methods if else Statements Loops Potpourri.
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
 Type Called bool  Bool has only two possible values: True and False.
Lesson 9: "if-else-if" and Conditional Logic
Boolean logic Taken from notes by Dr. Neil Moore
Types, Truth, and Expressions (Part 2)
CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum
Introduction to Programming Using Python PART 2
CSc 110, Spring 2018 Lecture 13: Cumulative Sum and Boolean Logic
Boolean logic Taken from notes by Dr. Neil Moore
Nate Brunelle Today: Conditional Decision Statements
LOOP Basics.
Presentation transcript:

1 CS 177 Week 5 Recitation Booleans and Control Flow

2 Announcements

3 Contents Booleans Decision structures While loop Project 2 preview

Booleans Boolean (logical) expressions:  An expression that can assume only the true or false value  We use logical expression in everyday language: If today is raining, then bring an umbrella when you go out.  “today is raining” is a logical expression: its value can be either true or false.  Other examples: assume x=4 assume str=“abc” x>3 type(str)==int (true) (false)

Boolean operators:  And, or, not a and b a and true x > 0 and x <=2 y > 0 and y >= 3(overlapped) Booleans PQP and Q TTT TFF FTF FFF

Boolean operators:  And, or, not a or b a or true x 2 x > 5 or x < 10 (always true) Booleans PQP or Q TTT TFT FTT FFF

Boolean operators:  And, or, not not a not (not a) not x > 3 DeMorgan’s law not (a or b) == (not a) and (not b) not (a and b) == (not a) or (not b) Booleans Pnot P TF FT

(P and (not Q)) or ((not P) and Q)  It has a name: XOR  Can you do this? Booleans PQP xor Q TT TF FT FF

Does the result look familiar? How about this: Let T=1, F=0 Yes, it is the sum of binary numbers Booleans PQP xor Q

If statement  An if statement takes a logical expression and evaluates it.  If it is true, the statements in if block are executed, otherwise, they are not executed. Simple decision Decision Structures x = 5 if x>1: print(“print something”) The string is printed x = 0 if x>1: print(“print something”) Does nothing!

Two-way decision Decision Structures a = 45 if a < 100: print(“a is small”) else: print(“a is large”) >>> a is small a = 153 if a < 100: print(“a is small”) else: print(“a is large”) >>> a is large

Two-way decision Decision Structures a < 100? “a is large”“a is small” noyes

Multi-way decision Decision Structures a = 1.5 if a > 2: print(“a>2”) else: if a > 1: print(“1<a<=2”) else: print(“a<=1”) >>> 1<a<=2 a = 1.5 if a > 2: print(“a>2”) elif a > 1: print(“1<a<=2”) else: print(“a<=1”) >>> 1<a<=2

Decision Structures Multi-way decision 14 a >2 ? “a>2” noyes a >1 ? “1<a<=2”“a<1” noyes

“How long do I have to shower?” You have two replies you could make:  a) 10 minutes  b) Until you are clean a) When programming, the first answer would be portrayed as a for-loop because we focus on exactly how long the shower will continue: for minutes in range (0,9): shower b) The second answer would be portrayed as a while-loop because the length of the shower is undetermined; we instead focus on the condition of cleanliness: while you are not clean: shower For vs While Loop

count = 0 while count < 9: print(“The count is:”,count) count = count + 1 print(“while loop ended”) for count in range(0,9): print(“The count is:”, count) print(“for loop ended”)

Rules of While Loops Command name: while Boolean condition (in the previous example: count < 9) determines the termination of loop A colon (“:”) And a block (the indented lines of code) 17

Interactive loop Using while loop, we can write interactive loops 18 count = 0 str = “Yes” while str == “Yes”: print(“The count is:”,count) count = count + 1 str = input(“continue? Yes or No:”) print(“while loop ended”)

Project 2 Preview Population Growth Simulation The Geometric growth model No limitation on the population  For loop  Show the population in characters Limitation on the population  While loop  The limitation is the stopping criterion  Show the population in graphs 19

Project 2 Preview The structure of a project should be like this: 20 Main function Function 1 Function

An Example from the Book def main(): sing("Fred") print() sing("Lucy") def sing(person): happy() happy() print("Happy birthday, dear", person + ".“) happy() def happy(): print("Happy birthday to you!") 21

An Example from the Book Functions link to each other by their parameters and return values What does f2(f1()) mean?  Pass the function f1 to f2? No  Pass the return value of f1 to f2? Yes v=f1() f2(v) 22 f1f2 f1f2 parameterreturn valueparameterreturn value

Questions? 23