CSE 231 Lab 2.

Slides:



Advertisements
Similar presentations
Introduction to Computing Science and Programming I
Advertisements

Structured programming
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
The switch Statement, DecimalFormat, and Introduction to Looping
The University of Texas – Pan American
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python Control Flow statements There are three control flow statements in Python - if, for and while.
If statements while loop for loop
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Course A201: Introduction to Programming 09/16/2010.
CS 100 Introduction to Computing Seminar October 7, 2015.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
GCSE Computing: Programming GCSE Programming Remembering Python.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
Python: Selection Damian Gordon. Python: Selection We’ll consider two ways to do selection: The IF statement The CASE statement.
STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
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.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
INTRO2CS Tirgul 3 1. What we will cover today?  Boolean and logical expressions  Truth values  Conditional statements  while loop  for loop  range.
Controlling Program Structures. Big Picture We are learning how to use structures to control the flow of our programs Last week we looked at If statements.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
More about Iteration Victor Norman CS104. Reading Quiz.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
G. Pullaiah College of Engineering and Technology
Python Review 1.
Topic: Iterative Statements – Part 1 -> for loop
REPETITION CONTROL STRUCTURE
Loops Upsorn Praphamontripong CS 1110 Introduction to Programming
Lesson 4 - Challenges.
The switch Statement, and Introduction to Looping
Python: Control Structures
Think What will be the output?
Programming 101 Programming for non-programmers.
Lesson 3 - Repetition.
Repetition: the for loop
Iterations Programming Condition Controlled Loops (WHILE Loop)
While Loops (Iteration 2)
Writing Functions( ) (Part 5)
Introduction to pseudocode
Validations and Error Handling
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Types, Truth, and Expressions (Part 2)
More Looping Structures
Topics Introduction to Repetition Structures
Flow Control Presented by: Md Fashiar Rahman.
3. Decision Structures Rocky K. C. Chang 19 September 2018
Suggested Layout ** Designed to be printed on white A3 paper.
Repetition In today’s lesson we will look at:
Conditional and iterative statements
15-110: Principles of Computing
A LESSON IN LOOPING What is a loop?
CHAPTER 6: Control Flow Tools (for and while loops)
Python Basics with Jupyter Notebook
Nate Brunelle Today: Conditional Decision Statements
Repetition: the for loop
Programming with Python, for beginners
CSE 231 Lab 4.
Types, Truth, and Expressions
More Looping Structures
Chapter 5 R programming Instructor: Li, Han.
CMPT 120 Lecture 4 – Unit 1 – Chatbots
How to allow the program to know when to stop a loop.
GCSE Computing.
Introduction to Python
Presentation transcript:

CSE 231 Lab 2

Topics to cover key condition : Statement Statement Decisions: Decision is how programs make choices if , elif, and else Repetition: Repeat until a condition is met while loop for loop (optional) condition ends all headers key condition : Keyword Statement Indentation “suite” of statements Statement

Passed #What gets printed? Decisions with if grade = 3.5 print(“Passed”) #What gets printed? Passed

#nothing #What gets printed? Decisions with if grade = 0.5 print(“Passed”) #What gets printed? #nothing

Decisions with if/else grade = 3.5 if grade >= 1.0: print(“Passed :) ”) else: print(“Not Passed :’( ”) #What gets printed? Passed :)

Decisions with if/else grade = 0.5 if grade >= 1.0: print(“Passed :) ”) else: print(“Not Passed :’( ”) #What gets printed? Not Passed :’(

Decisions with if/elif/else grade = 0.5 if grade >= 3.0: print(“Excellent”) elif grade >= 2.0: print(“Good”) else: print(“Not so good”) #What gets printed? Not so good

Decisions with if/elif/else #What gets printed? grade = 3.5 if grade >= 3.0: print(“Excellent”) elif grade >= 2.0: print(“Good”) else: print(“Not so good”) Excellent ...but why not “Good” as well, since 3.5 >= 2.0?

Decisions with if/elif/else grade = 2.5 if grade >= 3.0: print(“Excellent”) elif grade >= 2.0: print(“Good”) else: print(“Not so good”) #What gets printed? Good

Repetition with while answer = input(“Please say Hi:”) while answer != “Hi”: print(“You do not want to say Hi ?”) answer = input(“Let’s try again… Say Hi:”) print(“Cool, let’s continue then.”) >Please say Hi: no >You do not want to say Hi ? >Let’s try again… Say Hi: no >You do not want to say Hi ? >Let’s try again… Say Hi: Hi >Cool, let’s continue then.

1 2 why not 3? #What gets printed? Repetition with for n = 3 for i in range(n): print(i) #What gets printed? 1 2 why not 3?

2 3 4 why start at 2? #What gets printed? Repetition with for for i in range(2,5): print(i) #What gets printed? 2 3 4 why start at 2?

1 3 5 7 why only odds? #What gets printed? Repetition with for for i in range(1,9,2): print(i) #What gets printed? 1 3 5 7 why only odds?

Should I use while or for use this when you know how many times you want to iterate something e.g., to print “hello” 5 times for i in range(5): print(“hello”) while: when you want to continue looping until some condition is met when we do not know how many times we need to loop e.g., continue program until user inputs “Hi” answer = input() while answer != “Hi”:

Break vs Continue statement #What gets printed? for letter in 'Python': if letter == 'h': break print('Current Letter :', letter) Current Letter : P Current Letter : y Current Letter : t for letter in 'Python': if letter == 'h': continue print('Current Letter :', letter) Current Letter : P Current Letter : y Current Letter : t Current Letter : o Current Letter : n

Break vs continue The break statement in Python terminates the current loop and resumes execution at the next statement, The continue statement in Python returns the control to the beginning of the while loop.