26 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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.
Introduction to Programming
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.
Fundamentals of Python: From First Programs Through Data Structures
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
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
More While Loop Examples CS303E: Elements of Computers and Programming.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
17 November 2015Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
3 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
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.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
ARITHMETIC OPERATORS COMPARISON/RELATIO NAL OPERATORS LOGIC OPERATORS ()Parenthesis>Greater than &&And ^Exponentiation=>=
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.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
19 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
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
UCT Department of Computer Science Computer Science 1015F Iteration
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Printing Lines of Asterisks
Introduction to Programming
Introduction to Programming
CSC115 Introduction to Computer 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
Looping Topic 4.
3. Decision Structures Rocky K. C. Chang 19 September 2018
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
Introduction to Programming
Introduction to Programming
Presentation transcript:

26 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems Spring 2016 Week 8: loops

Lab 7, Quiz Grading 26 February 2016PFE R3.182 ScoreGrade A 80-89B 70-79C 60-69D <60E Obtain an integer valued score from the keyboard and print out the corresponding letter grade.

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" 26 February 2016PFE R3.183

Diagram for Question 2 26 February 2016PFE R =A >= D >= C >= B E

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 February 2016PFE P3.275

Decision Tree 26 February 2016PFE P3.276 divisible by 4? TrueFalse not a leap year divisible by 100 ? leap year False True divisible by 400 ? not a leap year False True leap year

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 26 February 2016PFE P3.277

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) 26 February 2016PFE P3.278

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") 26 February 2016PFE P3.279

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 26 February 2016PFE Section 4.110

Flowchart for the while Loop 26 February 2016PFE Section test condition True False execute statements Event controlled loop

Example: compound interest RATE = 5.0 INITIAL_BALANCE = 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.") 26 February 2016PFE Section 4.112

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. 26 February 2016PFE Section 4.113

while Loop Examples 26 February 2016PFE Section LoopOutput Explanation i = 0 total = 0 while total < 10 : i = i+1 total = total+i print(i, total) When total is 10, the loop condition is False and the loop ends. i = 0 total = 0 while total < 10 : i = i+1 total = total-i print(i, total) … Infinite loop i = 0 total = 0 while total < 0 : i = i+1 total = total-i print(i, total) (No output) The statement total < 0 is False when it is checked for the first time. The loop is never executed

Two Infinite Loops i = 0 total = 0 while total >= 0 : i = i+1 total = total+i print(i, total) 26 February 2016PFE Section year = 20 while year > 0 : interest = balance*RATE/100 balance = balance+interest year = year+1

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 # … 26 February 2016PFE Section 4.616

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 print(i) for i in range(10) : # i = 0, 1, 2, …, 9 print(i) # The range function generates a sequence of integers # over which the loop iterates 26 February 2016PFE Section 4.617

Example of a for Loop RATE = 5.0 INITIAL_BALANCE = 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)) 26 February 2016PFE Section 4.618

Output Enter number of years: February 2016PFE Section 4.619

for Loop Examples 26 February 2016PFE Section Loopvalues of icomment for i in range(6) :0, 1, 2, 3, 4, 5The 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, 8The third argument is the step value for i in range(5, 0, -1) :5, 4, 3, 2, 1Use a negative step value to count down

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 26 February 2016PFE Section 4.621

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 26 February 2016PFE Ch. 4 Review Questions22