
Programming Examples for Looping and Repetition
Explore various programming examples illustrating loops and repetition constructs, including while loops, input validation, do-while loops, and for loops in languages such as pseudocode and Java.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Ps Loops and Repetition
Initial Problem int counter = 1 counter while (counter < 1000) { PRINT (counter + I will not ) counter++ } Ps
Initial Problem int counter = 1; while (counter <= 1000) { System.out.println (counter + I will not ); counter++; }
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
Input ValidationExample public static void main (String[] args) { int userGuess = 0, secretNum = 5; Scanner scan = new Scanner (System.in); System.out.println("Enter a number between 1 and 10: "); userGuess = scan.nextInt(); while(userGuess < 1 || userGuess > 10) { System.out.println ("Not between 1 and 10. Try again!"); userGuess = scan.nextInt(); } if (userGuess == secretNum) { System.out.println(userGuess + " is the secret number!"); } else { System.out.println(userGuess + " isn t the secret number!"); } }
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) Ps ENDDO
Java - do-while Loop Example public static void main (String[] args) { int number, lastDigit, reverse = 0; Scanner scan = new Scanner (System.in); System.out.print ("Enter a positive integer: "); number = scan.nextInt(); do { lastDigit = number % 10; reverse = (reverse * 10) + lastDigit; number = number / 10; } while (number > 0); // NOTE THE SEMICOLON!!!!!!!!!!! System.out.println ("The reversed is " + reverse); } }
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
Java - for LoopExample public static void Main(String[] args) { int choice = 0; Scanner scan = new Scanner (System.in); System.out.println("Enter a number to see the table."); choice = scan.nextInt(); for(int multiplier = 1; multiplier <= 12; multiplier += 1) { System.out.println (multiplier + " * " + choice + " = " + (multiplier * choice)); } }
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 ENDouterFOR PRINTLINE () Ps
Java Nested for Loop Example public static void main (String[] args) { int maxRows = 10; for (int row = 1; row <= maxRows; row++) { for (int star = 1; star <= row; star++) { System.out.print ("*"); } System.out.println(); } }