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

Similar documents
INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Lecture 7 Tao Wang 1

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

Getting Started with Python

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

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

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

Theory of control structures

Introduction. C provides two styles of flow control:

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

Information Science 1

Chapter 5 : Informatics practices. Conditional & Looping Constructs. Class XI ( As per CBSE Board)

BIL101E: Introduction to Computers and Information systems Lecture 8

Control structure: Repetition - Part 3

Computational Expression

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Why Is Repetition Needed?

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

204111: Computer and Programming

(Python) Chapter 3: Repetition

Repetition Structures

While Loops A while loop executes a statement as long as a condition is true while condition: statement(s) Statement may be simple or compound Typical

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Chapter 1: Problem Solving Skills Introduction to Programming GENG 200

Information Science 1

LECTURE 5 Control Structures Part 2

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Comp 151. Control structures.

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Chapter 5: Control Structures

In this chapter you will learn:

Chapter 2: Functions and Control Structures

Decision Making in C

Structured Program Development

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

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

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

Structured Program Development in C

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Score score < score < score < 65 Score < 50

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Introduction to C/C++ Lecture 3 - Program Flow Control

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Search and Sorting Algorithms

CS110D: PROGRAMMING LANGUAGE I

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

4. Write the output that would be printed from each of the following code fragments. (8pts) x = 9 y = x / 2 print('y ==', y) +1 4.

Fundamentals of Programming (Python) Getting Started with Programming

Python Programming: An Introduction to Computer Science

REPETITION CONTROL STRUCTURE LOGO

Computer Science 217

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

Starting chapter 5. l First open file, and say purpose read or write inputfile = open('mydata.txt', 'r') outputfile = open('myresults.

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

LOOPS. Repetition using the while statement

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Fundamentals of Programming Session 9

#4: While Loop Reading: Chapter3

Programming for Experimental Research. Flow Control

Python Activity 5: Boolean Expressions and Selection Statements

Selection Statement ( if )

Repetition CSC 121 Fall 2014 Howard Rosenthal

Advanced Computer Programming

Accelerating Information Technology Innovation

Introduction to Programming

Looping. Arizona State University 1

Fundamentals of Programming Session 7

Software often repeats a set of instructions over and over again. This repetition continues until some condition is met.

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Chapter 8. Statement-Level Control Structures

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Fundamental of Programming (C)

Fundamentals of Programming Session 13

The while Loop 4/6/16 4

CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements

Control structures in C. Going beyond sequential

Fundamentals of Programming

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Control structure: Repetition - Part 2

CE151-4-AU UNIVERSITY OF ESSEX. Sample Examination Questions 2011 INTRODUCTION TO PROGRAMMING

Quiz 1: Functions and Procedures

Loops / Repetition Statements

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

CompSci 125 Lecture 11

CIS 3260 Intro to Programming in C#

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Repetition, Looping. While Loop

INTRODUCTION TO COMPUTER SCIENCE - LAB

Chapter 5: Control Structures II (Repetition)

Chapter 4: Control structures. Repetition

Outline. 1 If Statement. 2 While Statement. 3 For Statement. 4 Nesting. 5 Applications. 6 Other Conditional and Loop Constructs 2 / 19

Decision Structures CSC1310. Python Programming, 2/e 1

ECE 122. Engineering Problem Solving with Java

Second Term ( ) Department of Computer Science Foundation Year Program Umm Al Qura University, Makkah

Transcription:

Fundamentals of Programming (Python) Control Structures Sina Sajadmanesh Sharif University of Technology Some slides have been adapted from Python: How to Program 1 st Edition

Outline 1. Control Structures 2. Boolean Expressions 3. Conditionals 4. While Loop 5. For Loop 2

Control Structures Sequential order Statements are executed in the order they are written Transfer of control A program executes a statement other than the following one Do using control structures 3 control structures Sequential structure Selection structure (Conditionals) Repetition structure (Loops) 3

Boolean Expressions Operation x == y x!= y x > y x < y x >= y x <= y Operation x and y x or y not x Meaning Tests if x is equal to y... x is not equal to y... x is greater than y... x is less than y... x is greater than or equal to y... x is less than or equal to y Meaning Tests if both x and y are True... either x or y are True... x is False 4

Conditionals if structure It is a single entry, single exit structure Allows a program to perform an action only if a statement is true Otherwise the action is skipped Condition false true Action(s) 5

Conditionals Example Find min and max of two numbers x = input("enter first number: ") min = float(x) y = input("enter second number: ") max = float(y) if x > y: min, max = max, min print("min:", min) print("max:", max) 6

Conditionals if/else structure Double selection statement Allows the programmer to perform an action when a condition is true An alternate action is preformed when the action is false false Action(s) of False Case Condition true Action(s) of True Case 7

Conditionals Example Find min and max of two numbers x = input("enter first number: ") x = float(x) y = input("enter second number: ") y = float(y) if x < y: print("min:", x) print("max:", y) else: print("min:", y) print("max:", x) 8

Conditionals if/elif/else structure Multiple selection statement This is used in place of nested if/else statements The final else statement is optional condition a false condition b false true true case a action(s) case b action(s) It is used as a default action should all other statements be false... condition z true case z action(s) false default action(s) 9

Conditionals Example Find min and max of two numbers (check for equality) x = input("enter first number: ") x = float(x) y = input("enter second number: ") y = float(y) if x < y: print("min:", x) print("max:", y) elif x > y: print("min:", y) print("max:", x) else: print("min=max=", x) 10

Repetition Structures Repetition Structures (Loops) Allow a program to repeat an action while a statement is true Deterministic Repetition Implemented using for loop Non-Deterministic Repetition Implemented using while loop 11

Repetition Structures while loop The action is contained within the body of the loop Can be one or more than one action Condition should evaluate to false at some point Creates a infinite loop and program hangs Condition False True Action(s) 12

While Loop Example Class average grade (end with sentinel value) total = 0.0 counter = 0 grade = float(input("enter next grade: ")) while grade >= 0: total += grade counter += 1 grade = float(input("enter next grade: ")) average = total / counter print("average Grade: %.2f" % average) 13

While Loop The break statement Used to make a loop stop looping The loop is exited and no more loop code is executed total = 0.0 counter = 0 while True: grade = float(input("enter next grade: ")) if grade < 0: break total += grade counter += 1 average = total / counter print("average Grade: %.2f" % average) 14

Repetition Structures Counter-Controlled Repetition The counter A named variable to control the loop Initial value That which the counter starts at Increment Modifying the counter to make the loop eventually terminate Condition The test that the counter must pass in order to continue looping 15

Repetition Structures for loop Used to implement countercontrolled repetition structure for counter in range(initial, max, step): # Action(s) counter initial counter < max False True Action(s) counter counter + step 16

For Loop Example Class average grade (with known number of grades) total = 0.0 number = int(input("enter number of grades: ")) for counter in range(0, number, 1): grade = float(input("enter grade %d: " % (counter + 1))) total += grade average = total / number print("average Grade: %.2f" % average) 17

For Loop The continue statement Used to continue the looping process All following actions in the loop are not executed But the loop will continue to run Example Even numbers between 0 and 50 not dividable by 3 for counter in range(0, 51, 2): if counter % 3 == 0: continue print(counter, end=", ") 18