Download presentation
Presentation is loading. Please wait.
1
Module 4 Loops and Repetition 4/4/2019 CSE 1321 Module 4
2
Ps Initial Problem int counter = 1 while (counter < 1000) {
PRINT (counter + “I will not…”) counter++ } counter Ps
3
Code counter = 1 while (counter <= 1000):
print (counter, " I will not...") counter = counter + 1
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.