1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

Slides:



Advertisements
Similar presentations
Introduction to Computing Science and Programming I
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Computer Science 1620 Loops.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
1 Functions Samuel Marateck © A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 7: Program flow control.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Geography 465 Assignments, Conditionals, and Loops.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
CONTROLLING PROGRAM FLOW
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Python Conditionals chapter 5
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Control flow Ruth Anderson UW CSE 160 Spring
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Control flow Ruth Anderson UW CSE 160 Winter
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Five-Minute Review 1.What are classes and objects? What is a class hierarchy? 2.What is an expression? A term? 3.What is a variable declaration? 4.What.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Chapter 4 – C Program Control
Making Choices with if Statements
Python: Control Structures
Chapter 5 - Control Structures: Part 2
JavaScript: Control Statements.
Ruth Anderson UW CSE 160 Spring 2018
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
Chapter 8 JavaScript: Control Statements, Part 2
Chapter (3) - Looping Questions.
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
3 Control Statements:.
Building Java Programs
3. Decision Structures Rocky K. C. Chang 19 September 2018
Computer Science Core Concepts
Chapter 2 Programming Basics.
Building Java Programs
Relational Operators.
Building Java Programs
Building Java Programs
CHAPTER 5: Control Flow Tools (if statement)
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 8 JavaScript: Control Statements, Part 2
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
COMPUTING.
Presentation transcript:

1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010

2 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010

3 The type of a variable is determined by what is defined by the type of the literal assigned to it. Example: value = 12 So value is an integer identifier. If you then assign ‘asd’ to value, value = ‘asd’ What type a variable is value now?

4 value becomes a string variable.

5 We now introduce a new type of literal, the boolean literal. It can have two values, True and False. Note that these two literals must be capitalized.

6 In computer science, an expression consist of a variable, literal or both. So examples of expressions are: x, 3.3, x+2, and ‘asd’.

7 A boolean expression must have a true or false value. Can you give an example of a boolean expression ?

8 Can you give an example of a boolean expression ? True and False are the simplest boolean expressions.

9 Normally, boolean expressions have a comparison operator. The following slide is a table of some of these operators.

10 OperatorExplanation ==Equal to !=Not equal to >Greater than >=Greater than or equal to <Less than <=Less than or equal to

11 Note that the operators consisting of two characters cannot have an embedded blank. So > = would cause an error. It should be written >=.

12 Next we show how to use a boolean expression in an if statement.

13 if x == 3: print(x) print(‘nyu’) print(‘end’)

14 The boolean expression in if x == 3: is x==3. If it’s true, then the block of statements following the if is executed. Here the block is: print(x) print(‘nyu’) Note that the block must be indented.

15 The Python environment automatically indents statements in the block. To end the block you must manually unindent.

16 if x == 3: print(x) print(‘nyu’) print(‘end’) After the block is executed, the statements after the block, here print(‘end’) executed. If the boolean expression is false, the block is skipped and the statements after the block, here print(‘end’), are executed.

17 Now we explore the if-else construct:

18 if x == 3: print(x) print(‘nyu’) else: print(x**2) print(‘end’)

19 If x==3 is true the block print(x) print(‘nyu’) is executed, then print(‘end’) is executed. If x==3 is false, the block after the else:, print(x**2) is executed. Then print(‘end’) is executed.

20 In if True : print(‘doggy’) the literal True is the boolean expression and it is true, so print(‘doggy’) is executed.

21 The for loop In for j in range(10): print(j) the range of j is 0 ≤ j ≤ 9 so print(j) is executed ten times, once for each value of j in this range. Note that j is an integer and is called the loop index.

22 To add the integers from 1 to 10, set a variable sum to zero and add to it in the loop sum = 0 for j in range(11): sum = sum + j print(sum) Since the print is indented, it is executed with sum = sum + j For each value of j where now 0 ≤ j ≤ 10. The last two statements is the block comprising the scope of the loop.

23 A table showing how sum is incremented. jsumsum + j

24 A table showing how sum is incremented. jsumsum + j

25 A table showing how sum is incremented. jsumsum + j

26 A table showing how sum is incremented. jsumsum + j

27 A table showing how sum is incremented. jsumsum + j etc

28 A table showing how sum is incremented. jsumsum + j etc

29 Since when j = 0, sum is not incremented, we can rewrite the loop as: sum = 0 for j in range(1, 11): sum = sum + j print(sum) Now the range of j is 1 ≤ j ≤ 10 and the increment by default is 1.

30 If you want the increment to be 2, write: for j in range(1, 11, 2):

31 If you want j to decrease, starting at 10, and going to 1, write: for j in range(10, 0, -1): The -1 indicates a negative increment of 1. the range of j is 10>= j >= 1.

32 In general for a positive for loop increment, the second number in range must be greater than the first for the loop to be executed at least once. So in for j in range(2,2) the loop will be executed zero times. The second 2 means that the upper limit of the loop index is 1.

33 In general for a negative for loop increment, the second number in range must be less than the first for the loop to be executed at least once. So in for j in range(2,2, -1) the loop will be executed zero times. The second 2 means that the limit of the loop index is 3.

34 If you want the sum to be printed only at the end of the loop’s execution, unindent the print, for j in range(1, 11): sum = sum + j print(sum)

35 In general, in for j in range(n), the range of j is 0 ≤ j ≤ n-1.

36 If you want to build a string consisting of increasing numbers, e.g., , try s = ‘’ for j in range(0,20): s = s + j print(s) Because s is a string, the ‘+’ here means concatenation not addition.

37 While wont the following program compile? How do you correct it? s = ‘’ for j in range(0,20): s = s + j print(s)

38 Since s is a string, j must be converted to a string by writing str(j) s = ‘’ for j in range(0,20): s = s + str(j) print(s)

39 A table showing how the string s is incremented. jss + str(j) 0‘’‘0’

40 A table showing how the string s is incremented. jss + str(j) 0‘’‘0’ 1 ’01’

41 A table showing how the string s is incremented. jss + str(j) 0‘’‘0’ 1 ’01’ 2 ’012’

42 A table showing how the string s is incremented. jss + str(j) 0‘’‘0’ 1 ’01’ 2 ’012’ 3 ’0123’

43 A table showing how the string s is incremented. jss + str(j) 0‘’‘0’ 1 ’01’ 2 ’012’ 3 ’0123’ 4 ’01234’

44 A table showing how the string s is incremented. jss + str(j) 0‘’‘0’ 1 ’01’ 2 ’012’ 3 ’0123’ 4 ’01234’ 5 ’012345’

45 A table showing how the string s is incremented. jss + str(j) 0‘’‘0’ 1 ’01’ 2 ’012’ 3 ’0123’ 4 ’01234’ 5 ’012345’ 6 ’ ’ etc

46 How do you get the computer to print only digits, e.g.,

47 Let’s pause here and ask, what is 4//10?

48 Let’s pause here and ask, what is 4//10? The answer is 0. So the remainder, 4%10, is 4. What is 14//10?

49 What is 14//10? The answer is 1. So the remainder, 14%10, is 4. How should we rewrite the program to get ?

50 s = ‘’ for j in range(0,20): n = j%10 s = s + str(n) print(s)

51 Using characters in a for Given a string s = ‘asd1’, in for c in s: print(c) during execution, the Python virtual machine iterates though the string s and prints each character in the string.

52 You don’t have to use c and s. So given the string t = ‘asd1’, you could write for b in t: print(b) and get the same results. How do you write a program that counts the characters in a string? inu

53 s = ‘asd1’, n = 0 for c in s: n = n + 1 print(‘number of characters is ‘, n)

54 What happens if instead you write: s = ‘asd1’, for c in s: n = 0 n = n + 1 print(‘number of characters is ‘, n)

55 Since n= 0 is in the loop, the counter n is set to zero for each character.

56 Nested loops How do you produce the following pattern in which the number of columns and rows is dictated by the loop indices. XXXXXX

57 How do you write a program that produces: XXXXXX and then skip to a new line?

58 for j in range(6): print(x, end = ‘’) print() # skips to a new line

59 How do we repeat this pattern 3 times?

60 We nest for j in range(6): print(x, end = ‘’) print() # skips to a new line in another loop

61 for k in range(3): for j in range(6): print(x, end = ‘’) print() # skips to a new line Note that the print() is executed after the for j loop, so it’s executed three times not six times. The loop index of the outer loop k must be different than the loop index of the inner loop, j.