Student Q and As from 5.1 – 5.7, 5.11 Students of CS104, Fall 2013 (and me)

Slides:



Advertisements
Similar presentations
Selection Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.
Advertisements

ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
16-May-15 Sudden Python Drinking from the Fire Hose.
Conditional Statements Introduction to Computing Science and Programming I.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
If Statement if (amount
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
The If/Else Statement, Boolean Flags, and Menus Page 180
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.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Q and A for Section 4.4 CS 106, Fall Q1 Q: The syntax for an if statement (or conditional statement) is: if _______________ : _____________ A: if.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Flow of Control Part 1: Selection
Variables and Expressions CMSC 201 Chang (rev )
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Python Conditionals chapter 5
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Decision Structures, String Comparison, Nested Structures
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.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
Basic Conditions. Challenge: ● Ask the user his/her name ● If it’s “Wally,” jeer him ● Pause video and try on your own.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Lesson thirteen Conditional Statement "if- else" ©
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.
These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
CS 115 Lecture 8 Conditionals, if statements Taken from notes by Dr. Neil Moore.
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 _______________ : _____________.
7 - Programming 7J, K, L, M, N, O – Handling Data.
Python Basics.
CS 115 Lecture Conditionals and if statements
Selections Java.
Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016
CMPT 201 if-else statement
Selection Statements.
Adding Intelligence Check for the presence or absence of a condition in the environment Take the appropriate actions Involves making a choice (to do or.
Nate Brunelle Today: Conditional Decision Statements
Conditionals.
Presentation transcript:

Student Q and As from 5.1 – 5.7, 5.11 Students of CS104, Fall 2013 (and me)

Modulus operator Q: What does 10 % 3 produce? A: 1 (10 / 3 is 3 remainder 1) Q: What does 112 % 10 produce? A: 2 Q: What does 112 % 100 produce? A: 12 Q: What does x % 2 == 0 mean? A: That comparison is True if x is even.

Modulus Usefulness Q: When would this modulus operator (%) be useful? A: Suppose you have defined a function turnLeftDegrees(deg) and someone calls it, sending in a value of 363. How far should you turn? What if it is called with 723? If you take do this, you won’t be spinning your wheels… deg = deg % 360 # don’t do extra circles

Practice Q: What are the results of these expressions?: glick = 33 % 7 blick = 33 / 7 flick = 330 % 70 plick = 330 % 100 A: 5, 4, 50, 30

Relational operators Q: What 6 relational operators are there, for comparing two numbers, say, x and y? A: x < y x > y x <= y x >= y x == y x != y

Relational operators Q: What 6 relational operators are there, for comparing two numbers, say, x and y? A: x < y x > y x <= y x >= y x == y x != y

Result of a relational operator? Q: Operators produce results, right? produces 7. 3 / 4 produces 0. x – y produces the value x with y subtracted. What values can relational operators produce? (e.g., x == y, x <= y, etc.) A: Boolean values: either True or False. (Named after George Boole)

not operator Q: When is this useful? A: Sometimes it is just really nice to use not instead of not using it. Or, if you have a function that returns a boolean value. Suppose we have a function isEven(): if not isEven(num): print str(num) + “is odd” You could write if isEven(num) == False: etc.

Format of an if statement Q: What is the general format of an if statement? A: if : e.g.: if x > y: print x, “is greater than”, y

Format of an if-else statement Q: What is the general format of an if-elif statement? A: if : else:

Format of an if-elif statement Q: What is the general format of an if-elif statement? A: if : elif : (elif part can be repeated, and optional else clause on end)

Example of use Suppose you have a function called turnDegrees(deg) which takes a positive or negative value in deg. You want to turnLeft() if deg is positive, turnRight() if negative, and do nothing if 0. How would you write this code? if deg > 0: # this code in turnDegrees() turnLeft(deg) elif deg < 0: turnRight(-1 * deg)

Example of if elif elif … else Write code to print out the letter grade for a student. > 90 is A, 80 – 89 is B, etc. Value is held in variable grade. if grade > 90: print ‘A’ elif grade > 80: print ‘B’ elif grade > 70: print ‘C’ elif grade > 60: print ‘D’ else: print ‘F’

How about this? Q: What does this code do? # get value from user into variable grade if grade >= 90 : print "A” if grade >= 80 : print "B” else: print "F” A: if grade >= 90, prints out A and B (on 2 lines); prints out B if grade between 80 and 90; prints F for grades lower than 80

Nested conditional Q: Write code that prints the word “odd” or “even” depending on the value of variable anInt, and also prints “greater than 10” if the odd number has a value greater than 10. A: if anInt % 2 == 0 : print "even” else: print "odd” if anInt > 10: print "greater than 10”

Keyboard input Built-in function raw_input(somestr) prints out somestr and waits for user to type stuff in and hit. It returns what was typed – a string. If you want the result to be an int, you have to convert it. Same for float, etc. That’s all there is to it.