CHAPTER 3 Decisions and Repetition. A few new constants Constants so far:  Strings  Numbers  integer  float Boolean constants: [On board]

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 4 - Control Statements
Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.
Lecture 12 – Loops – while loops COMPSCI 1 1 Principles of Programming.
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
Conditional Statements Introduction to Computing Science and Programming I.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Chapter 6 – Repetition Statements : Objectives After you have read and studied this chapter, you should be able to Implement repetition control in a program.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Guide to Programming with Python Chapter Three Branching, while Loops, and Program Planning: The Guess My Number Game.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Course A201: Introduction to Programming 09/16/2010.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Decision Structures, String Comparison, Nested Structures
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Control Flow (Python) Dr. José M. Reyes Álamo.
Chapter 4 Repetition Statements (loops)
Week of 12/12/16 Test Review.
Chapter 3: Decisions and Loops
The switch Statement, and Introduction to Looping
Selection and Python Syntax
ECS10 10/10
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
And now for something completely different . . .
Logical Operators and While Loops
Starter 15//2 = 7 (Quotient or Floor) (Modulus) 22%3 =1
Practice with loops! What is the output of each function below?
Repetition Control Structure
Variables and Expressions
Visual Basic – Decision Statements
Logical Operators and While Loops
Program Flow.
Python Basics with Jupyter Notebook
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
How to allow the program to know when to stop a loop.
Iteration – While Loops
Presentation transcript:

CHAPTER 3 Decisions and Repetition

A few new constants Constants so far:  Strings  Numbers  integer  float Boolean constants: [On board]

A few new operators All of these are binary operators and create a Boolean result.  We'll use them as conditions. == != < > <= >= not and or

Decisions (if statement) Our programs so far…linear Real programs need to make decisions:  Return to title screen if player's health at 0.  Turn off the speed-boost if player has had it for 5 seconds.  Increase the player's ammo if they have less than 100, otherwise store in backpack. Decision (based on a condition) Action to take if the condition is true Action to take if the condition isn't true

If statements in python Type I (optionally do something) if cond: # Block of code Type II (do one of two things) if cond: # Block of code else: # Block of code

If statements, cont. Type III (do one of n things) if cond1: # Block of code elif cond2: # Block of code elif cond3: # Block of code else: # Block of code Type IV (do 0 or 1 of n things)  Like Type III but no else.

Examples Time-guesser  Computer waits for 5-10 seconds.  Try to guess the # of seconds (within 0.1)  Computer tells you “correct”, “too low”, or “too high”

Repetition Used to repeat a section of code 0-n times.  Keep moving the player until health == 0  Generate 10 numbers between 1 and 18 (stats)  Draw 500 stars in the background. Syntax: while cond: # While-block Evaluation of a while. [Nano-lab: write a loop which will print all positive powers of 2 which are less than x (initially x is 5000) ]

Repetition, cont. break and continue Infinite Loops

Nested if & while An if-block and a while-block can contain any python code.  This includes other if & while statements. Examples:  Day-of-the-week + coin-flip "game“ (if with nested-if)  Prompting the user for a valid login. (while with nested if)  Drawing a pygame starfield (while)  Animating a bouncing circle (while)  Drawing a checkerboard (nested while)