Introduction to Programming

Slides:



Advertisements
Similar presentations
Control Structures.
Advertisements

22 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
17 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
8 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
22 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
COMP Loop Statements Yi Hong May 21, 2015.
12 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
19 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
26 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
4 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming Python Lab 8: Loops 26 February PythonLab8 lecture slides.ppt Ping Brennan
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
11 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Copyright © 2013 by John Wiley & Sons. All rights reserved. LOOPS CHAPTER Slides by James Tam Department of Computer Science, University of Calgary April.
UCT Department of Computer Science Computer Science 1015F Iteration
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
CSC115 Introduction to Computer Programming
Control Structure Senior Lecturer
Introduction to Programming
Introduction to Programming
Introduction to Programming
MSIS 655 Advanced Business Applications Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Topics Introduction to Repetition Structures
Repetition Control Structure
` Structured Programming & Flowchart
Introduction to Programming
Looping Topic 4.
3. Decision Structures Rocky K. C. Chang 19 September 2018
Introduction to Programming
Introduction to Programming
Introduction to Repetition Structures
Introduction to Programming
Introduction to Programming
CS2011 Introduction to Programming I Selections (I)
CS150 Introduction to Computer Science 1
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Flow Control I Branching and Looping.
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

Introduction to Programming Department of Computer Science and Information Systems Steve Maybank sjmaybank@dcs.bbk.ac.uk Spring 2017 Week 8: loops 3 March 2017 Birkbeck College, U. London

Lab 7, Quiz Grading Score Grade 90-100 A 80-89 B 70-79 C 60-69 D <60 E Obtain an integer valued score from the keyboard and print out the corresponding letter grade. 3 March 2017 PFE R3.18 2

Solution to Quiz Grading score = int(input("Enter the score: ")) grade = "" if (score >= 90) : grade = "A" elif (score >= 80) : grade = "B" elif (score >= 70) : grade = "C" elif (score >= 60) : grade = "D" else : grade = "E" 3 March 2017 PFE R3.18 3

Diagram for QuizGrading E >= D >= C >= B =A 50 70 80 90 100 60 3 March 2017 PFE R3.18 4

Lab 7, Leap Year Obtain an integer from the keyboard. Print True if it specifies a leap year, otherwise print False. Usually years that are divisible by 4 are leap years. However, years that are divisible by 100 are not leap years, unless the year is also divisible by 400. 3 March 2017 PFE P3.27 5

Decision Tree False True divisible by 4? not a leap year False 3 March 2017 PFE P3.27 6

Paths through the Decision Tree a: divisible by 4 b: divisible by 100 c: divisible by 400 First path: not a Second path: a and (not b) Third path: a and b and (not c) Fourth path: a and b and c 3 March 2017 PFE P3.27 7

Boolean Test for a Leap Year (a and (not b)) or (a and b and c) Equivalent solution: a and ((not b) or (b and c)) Proof of equivalence: case a = False (both are False) case a = True (both reduce to ((not b) or (b and c))) In this example only, (b and c) == c thus an equivalent solution is a and ((not b) or c) 3 March 2017 PFE P3.27 8

Solution to Leap Year year = int(input("Enter the year:")) a = (year%4 == 0) # brackets are not essential b = (year%100 == 0) c = (year%400 == 0) if a and ((not b) or c) : print("Leap year") else : print("Not a leap year") 3 March 2017 PFE P3.27 9

Syntax for the while Loop while condition : statements # If the value of the condition equals # True, then the statements are executed Example: i = 0 while i < 10 : print(i) i = i+1 3 March 2017 PFE Section 4.1 10

Flowchart for the while Loop test condition False True execute statements Event controlled loop 3 March 2017 PFE Section 4.1 11

Example: compound interest RATE = 5.0 INITIAL_BALANCE = 10000.0 TARGET = 2*INITIAL_BALANCE balance = INITIAL_BALANCE year = 0 while (balance < TARGET) : year = year+1 interest = balance*RATE/100 balance = balance+interest print("The investment doubled after", year, "years.") 3 March 2017 PFE Section 4.1 12

Test Cases Use very simple test data to check that the while loop is correct. Eg. Set TARGET = INITIAL_BALANCE Eg. if RATE = 100.1%, TARGET = 2*INITIAL_BALANCE then the balance is slightly more than doubled at the end of the first year. In both cases check the value of year on exiting the loop. 3 March 2017 PFE Section 4.1 13

while Loop Examples Loop Output Explanation i = 0 total = 0 while total < 10 : i = i+1 total = total+i print(i, total) 1 1 2 3 3 6 4 10  When total is 10, the loop condition is False and the loop ends. i = 0 total = total-i 1 -1 2 -3 3 -6 4 -10 …  Infinite loop while total < 0 :  (No output)  The statement total < 0 is False when it is checked for the first time. The loop is never executed 3 March 2017 PFE Section 4.1 14

Two Infinite Loops i = 0 total = 0 while total >= 0 : i = i+1 total = total+i print(i, total) year = 20 while year > 0 : interest = balance*RATE/100 balance = balance+interest year = year+1 3 March 2017 PFE Section 4.1 15

The for Loop For Strings stateName = "Virginia" for letter in stateName : print(letter) # The successive values of letter are "V", "i", "r", etc. # Output # V # i # … 3 March 2017 PFE Section 4.6 16

Count Controlled for Loops for i in range(1, 10) : # i = 1, 2, 3, …, 9 print(i) for i in range(1, 10, 2) : # i = 1, 3, 5, …, 9 for i in range(10) : # i = 0, 1, 2, …, 9 # The range function generates a sequence of integers # over which the loop iterates 3 March 2017 PFE Section 4.6 17

Example of a for Loop RATE = 5.0 INITIAL_BALANCE = 10000.0 numYears = int(input("Enter number of years:")) balance = INITIAL_BALANCE for year in range(1, numYears+1) : interest = balance*RATE/100 balance = balance+interest print("%4d %10.2f" % (year, balance)) 3 March 2017 PFE Section 4.6 18

Output Enter number of years: 10 1 10500.00 2 11025.00 3 11576.25 4 12155.06 5 12762.82 6 13400.96 7 14071.00 8 14774.55 9 15513.28 10 16288.95 3 March 2017 PFE Section 4.6 19

for Loop Examples Loop values of i comment for i in range(6) : 0, 1, 2, 3, 4, 5 The loop is executed 6 times for i in range(10, 16) : 10, 11, 12, 13, 14, 15 The ending value is never included in the sequence for i in range(0, 9, 2) : 0, 2, 4, 6, 8 The third argument is the step value for i in range(5, 0, -1) : 5, 4, 3, 2, 1 Use a negative step value to count down 3 March 2017 PFE Section 4.6 20

Example of a for Loop Read twelve temperature values (one for each month) and display the number of the month with the highest temperature Example: if the temperatures in degree C are 18.2, 22.6, 26.4, 31.1, 36.6, 42.2, 45.7, 44.5, 40.2, 33.1, 24.2, 17.6 then the program should display 7 3 March 2017 PFE Section 4.6 21

PFE Ch. 4 Review Questions Examples Write a loop that computes the sum of all squares between 1 and 100, inclusive Write a loop that computes the sum of all the odd digits in a non-negative integer n Use a single for loop to display a rectangle of asterisks with a given height and a given width 3 March 2017 PFE Ch. 4 Review Questions 22