Comp 151. Control structures.

Similar documents
Comp 151. Control structures.

Text Input and Conditionals

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CS100: CPADS. Decisions. David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania

Lecture Transcript While and Do While Statements in C++

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

cs1114 REVIEW of details test closed laptop period

Python Programming: An Introduction to Computer Science

Lists, loops and decisions

Outline. Announcements. Homework 2. Boolean expressions 10/12/2007. Announcements Homework 2 questions. Boolean expression

MITOCW watch?v=0jljzrnhwoi

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Introduction to Python (All the Basic Stuff)

EECS 183. Week 3 - Diana Gage. www-personal.umich.edu/ ~drgage

Python Programming: An Introduction to Computer Science

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

Intro. Scheme Basics. scm> 5 5. scm>

Loop structures and booleans

Flow Control. So Far: Writing simple statements that get executed one after another.

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Introduction to Computer Programming for Non-Majors

Basics of Programming with Python

Lecture. Loops && Booleans. Richard E Sarkis CSC 161: The Art of Programming

Control Structures in Java if-else and switch

Conditionals and Recursion. Python Part 4

Python - Loops and Iteration

CIS220 In Class/Lab 1: Due Sunday night at midnight. Submit all files through Canvas (25 pts)

Loops and Iteration. Chapter 5. Python for Informatics: Exploring Information

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

6.001 Notes: Section 15.1

Type Checking and Type Equality

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Information Science 1

Most of the class will focus on if/else statements and the logical statements ("conditionals") that are used to build them. Then I'll go over a few

A453 Task 1: Analysis: Problem: Solution:

Propositional Calculus: Boolean Functions and Expressions. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

Example of a Demonstration that a Problem is NP-Complete by reduction from CNF-SAT

Unit E Step-by-Step: Programming with Python

Test #2 October 8, 2015

Ruby: Introduction, Basics

CS 112: Intro to Comp Prog

Basic Syntax - First Program 1

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

C++ Programming: From Problem Analysis to Program Design, Third Edition

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

(Python) Chapter 3: Repetition

MITOCW watch?v=se4p7ivcune

Chapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like?

COMS 1003 Fall Introduction to Computer Programming in C. Bits, Boolean Logic & Discrete Math. September 13 th

Control Structures in Java if-else and switch

CMSC 201 Fall 2018 Lab 04 While Loops

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Mathematical Logic Prof. Arindama Singh Department of Mathematics Indian Institute of Technology, Madras. Lecture - 9 Normal Forms

CSE 115. Introduction to Computer Science I

8.3 Common Loop Patterns

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

5. Control Statements

CSC Software I: Utilities and Internals. Programming in Python

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Lecture 8. Conditionals & Control Flow

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Ruby: Introduction, Basics

AXIOMS OF AN IMPERATIVE LANGUAGE PARTIAL CORRECTNESS WEAK AND STRONG CONDITIONS. THE AXIOM FOR nop

CMPT 120 Control Structures in Python. Summer 2012 Instructor: Hassan Khosravi

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

CS 106 Introduction to Computer Science I

Information Science 1

The following expression causes a divide by zero error:

Introduction to Computer Programming for Non-Majors

Assessment - Unit 3 lessons 16-21

CMSC201 Computer Science I for Majors

Try typing the following in the Python shell and press return after each calculation. Write the answer the program displays next to the sums below.

COMP-202 Unit 4: Programming with Iterations

Boolean Expressions. Is Equal and Is Not Equal

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

COMP 102: Computers and Computing

Verilog Overview. The Verilog Hardware Description Language. Simulation of Digital Systems. Simulation of Digital Systems. Don Thomas, 1998, Page 1

Flow Control: Branches and loops

Boolean Expressions. Is Equal and Is Not Equal

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0

LESSON 3 CONTROL STRUCTURES

Control structure: Repetition - Part 2

(Refer Slide Time: 00:26)

The Big Python Guide

CS 115 Lecture 4. More Python; testing software. Neil Moore

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

Making Decisions In Python

Quiz. Introduction: Python. In this project, you ll make a quiz game to challenge your friends. Activity Checklist.

ECS15, Lecture 14. Reminder: how to learn Python. Today is the final day to request a regrade of the midterm. Topic 4: Programming in Python, cont.

How Do Robots Find Their Way?

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Python Programming: An Introduction To Computer Science

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

61A Lecture 3. Friday, September 5

Introduction to Python

CSC 326H1F, Fall Programming Languages. What languages do you know? Instructor: Ali Juma. A survey of counted loops: FORTRAN

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Transcription:

Comp 151 Control structures.

admin For these slides read chapter 7 Yes out of order.

Simple Decisions So far, we ve viewed programs as sequences of instructions that are followed one after the other. While this is a fundamental programming concept, it is not sufficient in itself to solve every problem. We need to be able to alter the sequential flow of a program to suit a particular situation. Python Programming, 2/e 4

Simple Decisions Control structures allow us to alter this sequential program flow. In this chapter, we ll learn about decision structures, statements that allow a program to execute different sequences of instructions for different cases, allowing the program to choose an appropriate course of action. Python Programming, 2/e 5

Motivation up till now written simple programs storing and manipulating small amounts of data. first line in program executed, then each line after that. now add ability to take more control from computer

Making decision sometimes we don't want every line of code to execute don't want to do the same thing every time real life example?

Making decision sometimes we don't want every line of code to execute don't want to do the same thing every time real life example? how about if campus is open go to class can do this in programming as well technically called 'selection' decision making Some of you did this in the PAL sections for lab1

Selection in python syntax if <something that evals to boolean> : statements here the <> brackets just indicate that whatever is between them is semantic and should be replaced again note indent of statements example value = True if value: print ( yes )

Boolean expressions laymans terms: something that evaluates to true/false value in python True False Note capitalization for those of you c++/java refugees what sorts of things give true/false values?

Common booleans (in python) equality and inequality == (equality) >, <, >= (greater than or equal) <=!=(not equal) straight negation not eg: value = True if not value: print( something )

doing something else for false sometimes you want to do something else when the condition is false if the campus is open go to class otherwise go back to bed. if today == 'Monday': print ( Back to work ) else: print( same old, same old ) else only after if.

notice the indenting? code 'blocks' As you know indenting is how python determines code that logically belongs together called a 'code block' in jargon terms can have as many lines as desired in said code block all indenting the same same code block

multiple conditions can have multiple conditions use elif keyword if today == 'monday': print ('blah') elif today == 'friday': print ('yup') elif today == 'Sunday': else: print('almost all over') print( same old same old )

Finishing conditionals We looked at some simple conditionals last time Comparisons Now lets consider compound conditions In logic we have and or and not X and Y is true iff X is true and Y is true X or Y is true if either X is true or Y is true or both are true. If X is true then not X is false. Python actually uses these words for logical operators

Logical Operators II Var1 = hello Var2 = 3 if len(var1)>4 and Var2<12: print( it was true ) else: print( condition was false )

Short Circuit Operators Lets take a look at another example Var1 = hello Var2 = 3 if Var2>12 and len(var1)>4 : print( it was true ) else: print( condition was false ) Do you really need to evaluate the len(var1)>4 part? Why or why not?

Short Circuit Operators Lets take a look at another example Var1 = hello Var2 = 3 if Var2>12 and len(var1)>4 : else: print( it was true ) print( condition was false ) Do you really need to evaluate the len(var1)>4 part? Why or why not? You don't. Because the first part is false So it doesn't matter what the second part was

Short Circuit Operators II Python (and most other languages) Takes advantage of this logical property If the expression to the left of the operator (and/or) completely determines the truth value of the statement Then the expression to the right is never evaluated Left is true for or don't don't need to evaluate right Left is false for and don't don't need to evaluate right Implications?

Short Circuit Operators II Python (and most other languages) Takes advantage of this logical property If the expression to the left of the operator (and/or) completely determines the truth value of the statement Then the expression to the right is never evaluated Left is true for or don't don't need to evaluate right Left is false for and don't don't need to evaluate right Implications? Put expensive operations or right for efficiency Potential for errors to be hidden

definite and indefinite loops two kinds of loops in programming definite: know how many times to loop when program begins looping indefinite don't know how many times, but know how world will be when done (ends on condition) real world examples of each from class? which is the range looping we did last week?

While: python indefinite loop Syntax while <condition>: codeblock as long as the condition is true, code block will be executed again and again lets do one in idle stalker 'friend' then add ifs

potential loop problems I Infinite loop you must update the loop variable somewhere in the code block for the loop. i.e. the loop needs to have the chance to get closer to ending each time through. bad: counter = 1 sum = 0 while counter <=5: sum = sum+counter

Potential loop problems II Off by one errors want to make sure you loop the right number of times not one too many or one too few code below is supposed to be calculating how much a bank balance is after 10 years of 5% interest. what are problems? balance = input( how much to start?: ) balance = float(balance) years = 0 while years <= 10: balance = balance + balance* 0.05

loop problems III problems in that loop: infinite loop questions/problems should years start at 0 or 1? should the condition be < or <= consider simple conditions lets hand execute on board first.

Random Module Introduce random.randint Edit stalker friend to use it.

Assignment Read chapter 7 And see assignment if Wednesday