Programming Examples for Looping and Repetition

slide1 n.w
1 / 11
Embed
Share

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.

  • Programming Examples
  • Looping
  • Repetition
  • Pseudocode
  • Java

Uploaded on | 0 Views


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


  1. Ps Loops and Repetition

  2. Initial Problem int counter = 1 counter while (counter < 1000) { PRINT (counter + I will not ) counter++ } Ps

  3. Initial Problem int counter = 1; while (counter <= 1000) { System.out.println (counter + I will not ); counter++; }

  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

  5. 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!"); } }

  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) Ps ENDDO

  7. 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); } }

  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

  9. 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)); } }

  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 ENDouterFOR PRINTLINE () Ps

  11. 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(); } }

More Related Content