Nested Loops CS303E: Elements of Computers and Programming.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Macro simple idea of textual substitution useful when you need a group of instructions or directives frequently.
C Introduction Lesson CS1313 Spring C Introduction Lesson Outline 1.C Introduction Lesson Outline 2. hello_world.c 3.C Character Set 4.C is Case.
C Introduction Lesson CS1313 Spring C Introduction Lesson Outline 1.C Introduction Lesson Outline 2. hello_world.c 3.C Character Set 4.C is Case.
Introduction to C Programming
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?
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.
CS150 Introduction to Computer Science 1
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
More loops Linag, Chpt 3, pp The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition? loop-body.
Writing Tcl Scripts Outline Goal Reading Syntax Data Types
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 8 Arrays and Strings
Files in Python Input techniques. Input from a file The type of data you will get from a file is always string or a list of strings. There are two ways.
Introduction to C Programming
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 4.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Programming for Linguists An Introduction to Python 24/11/2011.
Chapter 8 Arrays and Strings
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
More While Loop Examples CS303E: Elements of Computers and Programming.
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Practice with Lists and Strings CS303E: Elements of Computers and Programming.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
For loops in programming Assumes you have seen assignment statements and print statements.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
More Strings CS303E: Elements of Computers and Programming.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Escape sequence Certain characters, preceded by a backslash ( \ ), are known as escape sequences They are used to display certain characters, or as display.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students, continue; and break; statements.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Chapter 02 (Part II) Introduction to C++ Programming.
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
CSE123 - Lecture 4 Structured Programming- Loops.
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Lecture 4b Repeating With Loops
Chapter 4 – C Program Control
Chapter 2 Introduction to C++ Programming
EKT120 COMPUTER PROGRAMMING
Line Continuation, Output Formatting, and Decision Structures
Lecture 7: Repeating a Known Number of Times
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.
Line Continuation, Output Formatting, and Decision Structures
Python Primer 2: Functions and Control Flow
Outline Altering flow of control Boolean expressions
Introduction to C++ Programming
Class Examples.
Text Analyzer BIS1523 – Lecture 14.
CHAPTER 6: Control Flow Tools (for and while loops)
Unit 3: Variables in Java
EECE.2160 ECE Application Programming
Introduction to Computer Science
Presentation transcript:

Nested Loops CS303E: Elements of Computers and Programming

Nested Loops Nested loops occur when a loop has one or more other loops in its body Nested loops occur when a loop has one or more other loops in its body Outside loop variable is constant while the inside loop executes to completion Outside loop variable is constant while the inside loop executes to completion –The outside loop variable is incremented and inside loop begins again

Example for i in range(5): for j in range(3): print “i=“+str(i)+ print “i=“+str(i)+ ” j=“+str(j) Output: i=0 j=0 i=0 j=0 i=0 j=1 i=0 j=1 i=0 j=2 i=0 j=2 i=1 j=0 i=1 j=0 i=1 j=1 i=1 j=1 i=1 j=2 i=1 j=2 i=2 j=0 i=2 j=0 i=2 j=1 i=2 j=1 i=2 j=2 i=2 j=2 i=3 j=0 i=3 j=0 i=3 j=1 i=3 j=1 i=3 j=2 i=3 j=2 i=4 j=0 i=4 j=0 i=4 j=1 i=4 j=1 i=4 j=2 i=4 j=2

Example Write a program that reads 10 strings from the user and prints the number of times the letter “a” appears in each string. Write a program that reads 10 strings from the user and prints the number of times the letter “a” appears in each string.

Question: What is the value of sum after the following code is evaluated? sum=0 sum=0 for i in range(3): for i in range(3): for j in range(6): for j in range(6): sum=sum+1 sum=sum+1 A. 3C. 18 B. 9D. 729

Exercise Write a program that prints the following triangle, in which the first row contains 1 *, row 2 contains 2 *’s, … and row 10 contains 10 *’s *******************************************************

Print: Eliminating Extra Space print always adds a blank space at the end: print always adds a blank space at the end: –A space after a comma OR –A newline when there is no comma To avoid this, use sys.stdout.write() To avoid this, use sys.stdout.write() –sys indicates the sys library –stdout indicates standard output or where the output usually goes---in this case, the interpreter

sys.stdout.write(): Example import sys sys.stdout.write(“hello”)sys.stdout.write(“world”)Output: helloworld helloworld

Question for you: What does stdout represent? A. standard error B. standard output C. standard deviation

Exercise Write a program that reads in a string from the user and then: –Capitalizes all the words –Uses a loop to print every other character in the string starting with the first character without spaces between the letters

Reminders No discussion section meetings this week. No discussion section meetings this week. Happy spring break! Happy spring break! Work on project – due the week after spring break. Work on project – due the week after spring break.