Module 4 Loops and Repetition 4/4/2019 CSE 1321 Module 4.

Slides:



Advertisements
Similar presentations
Microsoft® Small Basic
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Structured programming
Program Design and Development
CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Chapter 5 Loops.
Python Repetition. We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how.
ANALYSIS AND ALGORITHM DESIGN - IV. Repeat statements/looping/counting/iterations It is often necessary to repeat certain parts of a program a number.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Counting Loops.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 7 Problem Solving with Loops
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
Microsoft® Small Basic Conditions and Loops Estimated time to complete this lesson: 2 hours.
Input, Output and Variables GCSE Computer Science – Python.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
Pseudocode Key Revision Points.
while Repetition Structure
ECS10 10/10
Chapter 6 More Conditionals and Loops
Week 4 – Repetition Structures / Loops
Lesson 3 - Repetition.
CHAPTER 2 & 3: Pseudocode and Developing and Algorithm
Logical Operators, Boolean Data Types
The while Looping Structure
Control Structure Senior Lecturer
Pseudocode.
Understanding the Three Basic Structures
Chapter (3) - Looping Questions.
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
The while Looping Structure
The ‘while’ loop ‘round and ‘round we go.
Looping Topic 4.
Some Common Issues: Iteration
Chapter 5: Control Structure
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Python programming exercise
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
Lecture 1 Review of 1301/1321 CSE /26/2018.
Python Basics with Jupyter Notebook
Module 4 Loops and Repetition 4/15/2019 CSE 1321 Module 4.
Introduction to Computer Science
The while Looping Structure
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Starter Look at the hand-out.
Lecture 1 Review of 1301/1321 CSE /26/2018.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Module 3 Selection Structures 12/7/2019 CSE 1321 Module 3.
Presentation transcript:

Module 4 Loops and Repetition 4/4/2019 CSE 1321 Module 4

Ps Initial Problem int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…”) counter++ } counter Ps

Code counter = 1 while (counter <= 1000): print (counter, " I will not...") counter = counter + 1

Pseudocode – Input Validation Example userGuess ← 0, secretNumber ← 5 PRINT “Enter a number between 1 and 10” READ userGuess WHILE (userGuess < 1 OR userGuess > 10) PRINT “That is not between 1 and 10. Try again.” READ userGuess ENDWHILE IF (userGuess == secretNum) THEN PRINT “That’s right! ”, userGuess, “ is the secret!” ELSE PRINT userGuess, “ is not the secret!” ENDIF Ps 4/4/2019 CSE 1321 Module 4

Python - Input Validation Example secretNumber = 5 userGuess = int(input("Enter a number between 1 and 10: ")) while(userGuess < 1 or userGuess >10): userGuess = int(input("Not between 1 and 10! Try again!")) if(userGuess == secretNumber): print("That's right! ", userGuess, " is the secret number!") else: print(userGuess, " is not the secret number!") 4/4/2019 CSE 1321 Module 4

Pseudocode – do-while Loop Example CREATE number ← 0, lastDigit ← 0, reverse ← 0 PRINT “Enter a positive number.” READ number DO lastDigit ← number % 10 reverse ← (reverse * 10) + lastDigit number ← number / 10 WHILE (number > 0) ENDDO Ps 4/4/2019 CSE 1321 Module 4

Python – do-while Loop Example number = 0 lastDigit = 0 reverse = 0 number = int(input("Enter a positive integer: ")) helperVariable = True while (helperVariable == True): lastDigit = number % 10 reverse = (reverse * 10) + lastDigit number = (int)(number / 10) print (number, ":", reverse) if (number >= 1): else: helperVariable = False print("The number reversed is: ", reverse) Note: Python does not have an explicit do-while loop, However, one way is to use a helper variable 4/4/2019 CSE 1321 Module 4

Pseudocode – for Loop Example CREATE userChoice ← 0 PRINT “Choose a number to see the multiplication table.” READ userChoice FOR (multiplier ← 1, multiplier < 12, multiplier ← multiplier + 1) PRINT userChoice, “ * “, multiplier, “ = “, (multiplier * userChoice) ENDFOR Ps 4/4/2019 CSE 1321 Module 4

Python – for Loop Example choice = int(input("Enter the number for the table: ")) for multiplier in range(1, 13): print(multiplier, " * ", choice, " = ", multiplier * choice) We can add a parameter to define the incrementation, if it is other than 1. For instance, the code below will show the choice multiplied only by even numbers for multiplier in range(2, 13, 2): print(multiplier, " * ", choice, " = ", multiplier * choice) 4/4/2019 CSE 1321 Module 4

Pseudocode – Nested for Loop Example CREATE maxRows ← 10, row, star FOR (row ← 1, row < maxRows, row ← row + 1) FOR (star ← 1, star <= row, star ← star + 1) PRINT “*” ENDinnerFOR PRINTLINE () ENDouterFOR Ps 4/4/2019 CSE 1321 Module 4

Python – Nested for Loop Example # Demonstrates the use of nested for loops to print stars rows = 10 for row in range(1,11): for star in range(1, row): print("*", end = "") print() 4/4/2019 CSE 1321 Module 4