上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Computational Thinking and Approach Lecture 5 Dr.Jialiang LU Jialiang.lu@situ.edu.cn
Computational Thinking and Approach Lecture 5 Dr. Jialiang LU Jialiang.lu@sjtu.edu.cn 1
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Control Structure ITERATION 2
ITERATION Control Structure 2
@ 上降充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Out ines 。for:definite Loop while:indefinite loop ·Interactive loop ● Sentinel loop 。File loop 。Nested loop ·Dictionary 3
3 Outlines • for: definite Loop • while: indefinite loop • Interactive loop • Sentinel loop • File loop • Nested loop • Dictionary
上降充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY For Loops:A Quick Review The for statement allows us to iterate through a sequence of values. for <var>in <sequence>: <body> -for i in range(n): The loop index variable var takes on each successive value in the sequence,and the statements in the body of the loop are executed once for each value. 4
4 For Loops: A Quick Review • The for statement allows us to iterate through a sequence of values. • for <var> in <sequence>: <body> – for i in range(n): • The loop index variable var takes on each successive value in the sequence, and the statements in the body of the loop are executed once for each value
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY For Loops:A Quick Review ● Suppose we want to write a program that can compute the average of a series of numbers entered by the user. e To make the program general,it should work with any size set of numbers. We don't need to keep track of each number entered,we only need know the running sum and how many numbers have been added. 5
5 For Loops: A Quick Review • Suppose we want to write a program that can compute the average of a series of numbers entered by the user. • To make the program general, it should work with any size set of numbers. • We don‟t need to keep track of each number entered, we only need know the running sum and how many numbers have been added