Comp 151. Control structures.

Similar documents
Comp 151. Control structures.

Text Input and Conditionals

Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors

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

Conditionals and Recursion. Python Part 4

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

MITOCW watch?v=0jljzrnhwoi

Lecture Transcript While and Do While Statements in C++

Quiz 1: Functions and Procedures

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

Introduction to Computer Programming for Non-Majors

CMSC 201 Fall 2018 Lab 04 While Loops

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

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

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

Lists, loops and decisions

Python - Loops and Iteration

Introduction. C provides two styles of flow control:

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

Control Structures in Java if-else and switch

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

Loop structures and booleans

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

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

Flow Control: Branches and loops

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Fundamentals of Programming (Python) Control Structures. Sina Sajadmanesh Sharif University of Technology Fall 2017

Type Checking and Type Equality

A453 Task 1: Analysis: Problem: Solution:

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Introduction to Python (All the Basic Stuff)

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

Python Programming: An Introduction to Computer Science

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

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Logic & program control part 3: Compound selection structures

Introduction to Computer Programming for Non-Majors

CS1 Lecture 5 Jan. 25, 2019

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Information Science 1

Conditional Expressions and Decision Statements

8.3 Common Loop Patterns

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Unit 7. 'while' Loops

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

CS1 Lecture 5 Jan. 26, 2018

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

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

The following expression causes a divide by zero error:

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

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

CompSci 101 Introduction to Computer Science

Control Structures in Java if-else and switch

Logical Operators and if/else statement. If Statement. If/Else (4.3)

Selection Statement ( if )

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

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

Unit E Step-by-Step: Programming with Python

Python Programming: An Introduction To Computer Science

Control structures in C. Going beyond sequential

Decision Structures CSC1310. Python Programming, 2/e 1

How Do Robots Find Their Way?

(Python) Chapter 3: Repetition

204111: Computer and Programming

Theory of control structures

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

61A Lecture 3. Friday, September 5

CS Boolean Statements and Decision Structures. Week 6

5. Control Statements

Algorithms and Conditionals

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

Announcements. HW0 is posted on schedule, due next Friday at 9pm (pretty easy)

Information Science 1

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

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

Python for Informatics

cs1114 REVIEW of details test closed laptop period

CSE 113 A. Announcements - Lab

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

CSE 115. Introduction to Computer Science I

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

More Complex Versions of the if Statement. Class 13

Chapter 2 - Control Structures

Lab1. Introduction to Python. Lab 4: Selection Statement. Eng. Mai Z. Alyazji

3 Programming. Jalal Kawash Mandatory: Chapter 5 Section 5.5 Jalal s resources: How to movies and example programs available at:

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Control structure: Repetition - Part 2

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

COMP 110 Introduction to Programming. What did we discuss?

Making Decisions In Python

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.

Part III Appendices 165

CS1 Lecture 22 Mar. 6, 2019

Python Activity 5: Boolean Expressions and Selection Statements

Transcription:

Comp 151 Control structures.

admin quiz this week believe it or not only 2 weeks from exam. one a week each week after that.

idle debugger Debugger: program that will let you look at the program as it is running and slow the program down to the point that it is useful. Now lets take a look at what the debugger can show you 3

Objectives To understand the programming pattern simple decision and its implementation using a Python if statement. To understand the programming pattern two-way decision and its implementation using a Python ifelse statement. Python Programming, 2/e 4

Objectives (cont.) To understand the programming pattern multi-way decision and its implementation using a Python ifelif-else statement. To understand the idea of exception handling and be able to write simple exception handling code that catches standard Python run-time errors. Python Programming, 2/e 5

Objectives (cont.) To understand the concept of Boolean expressions and the bool data type. To be able to read, write, and implement algorithms that employ decision structures, including those that employ sequences of decisions and nested decision structures. Python Programming, 2/e 6

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 7

Simple Decisions Control structures allow us to alter this sequential program flow. In this chapter, we ll learn about decision structures, which are 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 8

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

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 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

multi line code block lets try one in pycharm.

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 )

Lets try a little

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.

Read chapter 7 Assignment