© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Introduction to C Programming
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 9 Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
Decision Structures and Boolean Logic
Problem Solving with Decisions
If statements while loop for loop
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
For loops in programming Assumes you have seen assignment statements and print statements.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Chapter 4 – C Program Control
Chapter 6 JavaScript: Introduction to Scripting
Chapter 6: Loops.
© 2016 Pearson Education, Ltd. All rights reserved.
Topics Introduction to Repetition Structures
Problem Solving and Control Statements: Part 2
Chapter 5: Repetition Structures
Topics The if Statement The if-else Statement Comparing Strings
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Arrays, For loop While loop Do while loop
© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved.
Topics The if Statement The if-else Statement Comparing Strings
Arithmetic operations, decisions and looping
Python Primer 2: Functions and Control Flow
Chapter 8 JavaScript: Control Statements, Part 2
Additional Control Structures
Chapter 3: Introduction to Problem Solving and Control Statements
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Objectives After studying this chapter, you should be able to:
1) C program development 2) Selection structure
3 Control Statements:.
Chapter 8: More on the Repetition Structure
Chapter 6 Control Statements: Part 2
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Core Objects, Variables, Input, and Output
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
For loops Taken from notes by Dr. Neil Moore
Topics Introduction to Repetition Structures
statement. Another decision statement, , creates branches for multi-
Class code for pythonroom.com cchsp2cs
Chapter 2, Part III Arithmetic Operators and Decision Making
Chapter 8 JavaScript: Control Statements, Part 2
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. The for Loop Section 4 Chapter 3 © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Relational and Logical Operators A condition is an expression Involving relational operators (such as < and >=) Logical operators (such as and, or, and not) Evaluates to either True or False Conditions used to make decisions Control loops Choose between options © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. The for Loop Used to iterate through a sequence of values General form of a for loop Sequence can be Arithmetic progression of numbers String List File object © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. The for Loop Variable is successively assigned each value in the sequence Indented block of statements executed after each assignment Physical indentation tells interpreter where block starts and stops © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Looping Through Arithmetic Progression of Numbers Range function is used to generate an arithmetic progression © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Looping Through Arithmetic Progression of Numbers Example 1: Code displays four integers and their squares : © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Looping Through Arithmetic Progression of Numbers Example 2: Program displays a table showing the population each year until 2018. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Looping Through Arithmetic Progression of Numbers FIGURE 3.44 Flowchart for Example 2. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Step Values for the range Function Variation of the range function generates a sequence of integers Successive integers differ by a value other than 1 Examples © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Step Values for the range Function Example 3: Program requests amount deposited annual rate of interest then calculates balance after each quarter-year for four quarters. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Step Values for the range Function Example 3, cont. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Step Values for the range Function If negative step value is used and initial value is greater than terminating value, Range function generates a decreasing sequence Examples: © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. Nested for Loops Body of for loop can contain any type of Python statement Can contain another for loop. Second loop must be completely contained inside the first loop Must have a different loop variable © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. Nested for Loops Example 4: Program displays a multiplication table for the integers from 1 to 5 © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. Nested for Loops Example 5: Program uses nested for loops to display a triangle of asterisks. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Looping Through Characters of a String Example 6: Program requests a word as input and displays it backwards. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Looping Through Items of a List or Tuple Example 7: Program displays the months whose names contains the letter r. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Looping Through Items of a List or Tuple Example 8: Program replaces the name of each month with its three-letter abbreviation. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Looping Through Items of a List or Tuple Example 9: Program uses nested for loops with list of ranks, list of suits Creates list consisting of 52 cards in deck of cards © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Looping Through Items of a List or Tuple FIGURE 3.45 Flowchart of nested for loops from Example 9. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Looping Through Lines of Text File Reads each line of the file in succession Executes indented block of statement(s) for each line First line establishes connection between program and file © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Looping Through Lines of Text File Example 10: Program requests a first name and then displays names of U.S. presidents having that first name © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. The pass Statement There are times when you want loop to cycle through a sequence and not do anything The pass statement should be used. The pass statement is a do-nothing placeholder statement © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. The pass Statement Example 11: Program displays the last line of a file © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Populating a List with Contents of a Text File One way of placing the contents of a text file into a list. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Populating a List with Contents of a Text File A more efficient way (to be explained in next two chapters) © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. End Section 4 Chapter 3 © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.