1 / 11

Module 4

Module 4. Loops and Repetition. Initial Problem. int counter = 1 while (counter < 1000) { PRINT (counter + “I will not…” ) counter++ }. counter. Ps. Initial Problem. int counter = 1; while (counter <= 10) { cout << counter << " I will not.."<< endl ; counter++; } }.

amora
Download Presentation

Module 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Module 4 Loops and Repetition CSE 1321 Module 4

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

  3. Initial Problem int counter = 1; while (counter <= 10) { cout << counter << " I will not.."<<endl; 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 userGuessENDWHILEIF (userGuess == secretNum) THEN PRINT “That’s right! ”, userGuess, “ is the secret!”ELSE PRINT userGuess, “ is not the secret!”ENDIF Ps CSE 1321 Module 4

  5. Input ValidationExample int main() { int userGuess = 0, secretNum = 5; cout << "Enter a number between 1 and 10: "; cin >> userGuess; while(userGuess < 1 || userGuess > 10) { cout<<"Not between 1 and 10. Try again!"; cin>>userGuess; } if (userGuess == secretNum) { cout<<userGuess << " is the secret number!"; } else { cout<<userGuess << " isn’t the secret number!"; } } CSE 1321 Module 4

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

  7. C++ - do-while Loop Example int main() { int number, lastDigit, reverse = 0; cout<< "Enter a positive integer: "; cin>>number; do { lastDigit = number % 10; reverse = (reverse * 10) + lastDigit; number = number / 10; } while (number > 0); // NOTE THE SEMICOLON!!!!!!!!!!! cout<<"The reversed is " << reverse; } 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 CSE 1321 Module 4

  9. C++ - for LoopExample int main() { int choice = 0; cout<<"Enter a number to see the table."; cin>>choice; for(int multiplier = 1; multiplier <= 12; multiplier += 1) { cout<<multiplier <<" * " <<choice << " = " << (multiplier * choice)<<endl; } } 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 CSE 1321 Module 4

  11. C++ – Nested for Loop Example int main() { int maxRows = 10; for (int row = 1; row <= maxRows; row++) { for (int star = 1; star <= row; star++) { cout << "*"; } cout <<"\n"; } } CSE 1321 Module 4

More Related