Q and A for Section 4.4 CS 106, Fall 2014. Q1 Q: The syntax for an if statement (or conditional statement) is: if _______________ : _____________ A: if.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Selection Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.
Python Programming Language
James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Making Decisions In Python
Logical Operators and Conditional statements
The If/Else Statement, Boolean Flags, and Menus Page 180
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Chapter 4 Making Decisions
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
4. Python - Basic Operators
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
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.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping with.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Python Conditionals chapter 5
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Conditional Statements Consider: if the mouse location is contained in the rectangle, display message “success” Some programming constructs can choose.
Java Decision Making and booleans (Java: An Eventful Approach, Ch 4), Slides Credit: Bruce, Danyluk and Murtagh CS 120 Lecture October 2012.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
ICT Introduction to Programming Chapter 4 – Control Structures I.
James Tam Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action.
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.
Student Q and As from 5.1 – 5.7, 5.11 Students of CS104, Fall 2013 (and me)
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Q and A for Section 4.4 CS 106, Fall If statement syntax Q: The syntax for an if statement (or conditional statement) is: if _______________ : _____________.
Computer Science 1000 LOGO II. Boolean Expressions like Excel and Scratch, LOGO supports three Boolean operators less than (
Lecture 3 Selection Statements
Control Flow (Python) Dr. José M. Reyes Álamo.
Chapter 3 Selection Statements
Selection (also known as Branching) Jumail Bin Taliba by
Making Choices with if Statements
Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016
JavaScript conditional
And now for something completely different . . .
Types, Truth, and Expressions (Part 2)
Conditional Logic Presentation Name Course Name
JavaScript conditional
Nate Brunelle Today: Conditional Decision Statements
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
CHAPTER 5: Control Flow Tools (if statement)
Branching statements Kingdom of Saudi Arabia
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

Q and A for Section 4.4 CS 106, Fall 2014

Q1 Q: The syntax for an if statement (or conditional statement) is: if _______________ : _____________ A: if :

Q2 Q: Write code that prints out "Brilliant!" if the value in variable food is "spam". A: if food == "spam": print "Brilliant!"

Q3 Q: What happens if the value of food is "bubble and squeak"? A: Nothing.

Q4 Q: Write code to print "Brilliant!" if food is "spam" and how_prepared is "on toast". A: if food == "spam" and how_prepared == "on toast": print "Brilliant!" (or...) What if we don't like it on toast?

Q5 Q: Write code to print "Brilliant!" if food is "spam" and how_prepared is "on toast", and to print "Awesome!" if how_prepared is "in a casserole". A: if food == "spam": if how_prepared == "on toast": print "Brilliant!" if how_prepared == "in a casserole": print "Awesome!"

Q6 Q: An if-else statement has this format: if _____________ : ___________ else: ___________ A: ; ;

Q7 Q: Suppose you have a function is_prime(x) that returns True if x is a prime number. Write code to add x to a list primes if x is prime and to a list non_primes otherwise. A: if is_prime(x): primes.append(x) else: non_primes.append(x)

Q8 Q: Write code that prints "NaN" if the list nums is empty or, otherwise, computes and prints the average of the list of floats. A: if len(nums) == 0: print "NaN" else: print sum(nums) / len(nums)

Non-booleans used as booleans Q: In the “for the guru” on page 142, there is code if not dinner:. Can you explain that? A: Why, yes. Yes, I can. The pattern is if : So, if the expression given is not a boolean ( dinner is a string), then it is converted to a boolean, as if bool(dinner) were called. In general, if a value is empty ( “” or [] ), the value is False; else, True.

Q9 Q: Write code that prints "Let's go" if the variable response equals either 'y' or 'yes'. A: if response in ('y', 'yes'): print "Let's go" (not if response == 'y' or 'yes' !!)

Q10 Q: You have a list of Room objects, rooms. A Room object has a method anybody_home() that returns True if anyone is there. Write code to count how many rooms are not empty. A: someone_home = 0 for room in rooms: if room.anybody_home(): someone_home += 1 This is like a filter: do something only for certain elems in the sequence

Q10 Q: You are given a long string gen containing only As, Gs, Ts, and Cs. Write code to count the number of As and the number of Gs in the string. countAs = 0 countGs = 0 for ch in gen: if ch == 'A': countAs += 1 elif ch == 'G': countGs += 1

Q11 Q: You have 2 operands, op1 and op2, and an operator, op, that is one of '+', '-', '*', '/'. Write code to compute the correct result into res. A: if op == '+': res = op1 + op2 elif op == '-': res = op1 - op2 elif op == '*': res = op1 * op2 elif op == '/': res = op1 / op2

Q12 Q: What can we do to the previous code to print out "Oops!" if op contains a bad value? A: Add else: print 'Oops!'

Q13 Q: Rewrite this code using if-elif: if i < 7: do_something(i) else: if j > 9: do_something_else(j) A: if i < 7: do_something(i) elif j > 9: do_something_else(j)

Nested if vs. elif Q: When would you prefer an if nested inside an else, over using an elif. A: An if-else is a binary decision: there are two choices. An if-elif-elif-else is an n-way decision: some variable can belong to n different “groups” or “classes”. If you are comparing a variable n to something, and then in one of the results have to “classify” some other variable, use a nested if.

Q14 Q: Write code to print out a student's letter grade, based on the value in score. Pick your own scale. A: if score > 92: print 'A' elif score > 85: print 'B' elif score > 77: print 'C' elif score > 70: print 'D' else: print 'F'