some simpler exercises for IFs and loops.
David Thornley 3d5f447011 completed 6 年前
README.md completed 6 年前

README.md

Saturday-Exercises

some simpler exercises for IFs and loops.

Happy 1st Saturday, Newbies!

Yes, I am writing these on my phone, from I-70west in Ohio. No, I am not currently driving.

Some extra (easier) exercises.

Use them to practice writing both pseudo code and Java. Write each one in both.

Strive to feel good about the practice as you complete each one.

I suggest you write them into a notebook or onto paper. Once, you written them all, go thru them and type them into this file. You can use “atom” for that.

Then, if you’d like comments on your work, do a Pull Request and make the title of the PR “comments please”.

IFs

Write an IF statement that checks “player1.isAlive()” and if that’s false, calls “displayGameOver(player1)”

pseudo:

  • test the state of player1.isAlive()
    • if true
      • do nothing
    • if false
      • call displayGameOver(player1) Java

if(player1.isAlive() != true) { displayGameOver(player1); }

Write an IF statement that checks the “temperature(room)” and if that check is less than 70, calls “heatOn()” else calls “coolOn()”

pseudo:

  • test the result of temperature(room)
    • if < 70
      • call heatOn(room)
    • else
      • call heatOn(room)

Java:

if(temperature(room) < 70) { heatOn(room); } else { heatOff(room);}

Write an IF statement that checks “outsideTemp()” is less than 50 AND “insideTemp()” is less than 62, calls “startAFire(fireplace1)”

pseudo:

  • test if outsideTemp is < 50 AND inside tempTemp is < 62
    • if true
    • call startAFire(fireplace1)
    • if false
    • do nothing

Java:

if(outsideTemp() < 50 && insideTemp() < 62) { startAFire(fireplace1); }

Write an IF statement that checks “fuelLevel” and if that check is less than 0.08, calls “refuel()”

pseudo:

  • test if fuelLevel is < 0.08
    • if true
    • call refuel();
    • if false
    • do nothing

Java:

if(fuelLevel < 0.08) { refuel(); }

Loops

For each loop you write, print “Hello” on each loop iteration.

pseudo:

  • Create loop that prints "Hello" each time it iterates

Java:

for (int i = 0; i <=10; i++) { System.out.println("Hello"); }

Write a FOR loop that counts from 1 to 10.

pseudo:

  • Create a loop that starts at 1
  • prints the counter on each iteration
  • end when counter is <= 10

Java:

for (int i = 1; i <= 10; i++) { System.out.println(i); }

Write a FOR loop that makes 10 iterations, start at 21

pseudo:

  • create for loop that starts at i = 21
    • each iteration i++
    • end loop when i >= 30
    • print the counter each iteration

Java

for(int i = 21; i <= 30; i++) { System.out.println(i); }

Write a FOR loop that counts down from 100 to 0

pseudo:

  • create for loop that starts at 100
    • iterate until at 0
    • counter -- each iteration to reach 0
    • print counter each iteration

Java:

for(int i = 100; i <= 0; i--) { System.out.println(i); }

Write a FOR loop from 0 to 32 by 2s

pseudo:

  • create for loop starting at 0
    • stop iterations once counter reaches 32
    • increase counter by 2 after each iteration

Java:

for(int i = 0; i >= 32; i +=2) {}

Write a FOR loop from 1 to less than 5001 by 11s.

pseudo:

  • create for loop starting at 1
    • stop iterations once counter is greater than or equal to 5000
    • increase counter by 11 after each iteration

Java:

for(int i = 1; i < 5001; i += 11) {}

Write a nested FOR loop(s), where one counts from 0 to less than 20 and the inner one counts from 0 to 4.

pseudo:

  • create for loop starting at 0
    • end loop once counter is > 19
    • increase count by 1 after each iteration
  • inside for loop create for loop 2
    • start for loop 2 at 0
    • end for loop 2 when counter is greater than 4
      • increase counter by 1 after each iteration

Java:

for(int i = 0; i < 20 ; i++){ for(int j = 0; j <= 4; j++){

} }

Write a FOR loop that counts from 5 to 105. Put an IF statement inside the loop that checks the loop index counter and if it’s greater than 51, prints “Hello Zipcode” instead on “hello”

pseudo:

  • create for loop that starts at 5
    • end the loop when counter is > 105
    • increase counter by one each iteration
    • create if statement inside for loop
    • if the for loop counter is greater than 51
      • print "Hello Zipcode"

Java:

for(int i = 5; i <= 105; i ++) { if(i > 51) {

System.out.println("Hello Zipcode");

} }

Write a WHILE loop that checks “gps.currentLocation()” and if that is not equal to “Home” then and it calls “driveSomeMore()”. After the loop is done, print “Honey, I’m Home!”

pseudo:

  • create while loop set to gps.currentLocation() not equal to Home
    • call driveSomeMore() inside while loop
  • under while loop print “Honey, I’m Home!”

Java:

while( gps.currentLocation != "Home") { driveSomeMore(); }

System.out.println("Honey, I'm Home!");

First set “highestScore” to 0. Then set “currentScore” to “game.nextScore()”. Then write a WHILE loop that checks “currentScore” is greater than “highestScore” and if it is, sets “highestScore” to “currentScore” and then sets “currentScore” to “game.nextScore()”.

pseudo:

  • set highestScore to 0
  • set currentScore = game.nextScore()
  • create while loop that runs until currentScore is greater than highestScore
    • once while loop ends set highestScore to currentScore and sets currentScore to game.nextScore()

Java:

highestScore = 0; currentScore = game.nextScore();

while(highestScore > currentScore) {

currentScore = game.nextScore(); }

highestScore = currentScore; currentScore = game.nextScore();

Rewrite the previous WHILE loop as a REPEAT..UNTIL loop. Notice how the “currentScore” variable usage is different.

pseudo:

  • do some code
    • compare IF currentScore < highestScore
    • currentScore = game.nextScore();
    • else highestScore = currentScore;
    • while highestScore > currentScore

Java: do { if (currentScore < highestScore) {

currentScore = game.nextScore();

} else {

highestScore = currentScore

} } while (highestScore > currentScore);

Write a WHILE loop that checks “server.isRunning()” and if true calls “waitFor(5)” After the loop, write an IF and check “server.isRunning()” is false, and if so, call “sendEmergencyText(“Help!”, admin.iPhoneNumber)” and also calls “tryServerRestart()”

pseudo:

  • while loop that runs when server.isRunning() is true
    • if server.isRunning() is true
    • then call waitFor(5)
  • under while loop
    • if server.isRunning() is false
    • then call sendEmergencyText("Help!", admin.iPhoneNumber) and call tryServerRestart();

Java:

while (server.isRunning == true) { waitFor(5); }

if (server.isRunning != true) { sendEmergencyText("Help!", admin.iPhoneNumber); tryServerRestart(); }

Declare an “int” i. Set i to 7. Write a WHILE loop that checks “i” is less than 50, and if it is, add 7 to “i”

pseudo:

  • declare variable i = 7
  • while i is less than 50
    • add seven to the value of i;

Java:

int i = 7; while(i < 50) { i += 7; }

Given an array voteTallies[], write a FOR loop that prints out each value in the array.

pseudo:

  • for loop starts counter at 0
    • stops when counter gets to the length of the voteTallies array minus one
    • print index value of voteTallies where counter is the index position
    • add one to counter after each loop

Java:

for( int i = 0; i <= voteTallies.length() - 1; i ++) { System.out.println(voteTallies[i]); }

Given an array voteTallies[], write a WHILE loop that prints out each value in the array. You should declare and use an index “idx” to keep track of where you are.

pseudo:

  • declare idx variable set to 0
  • while idx is less than or equal to (voteTallies.length()-1)
    • print value at voteTallies[idx]; and add one to idx value

Java:

int idx = 0;

while(idx < (voteTallies.length() - 1)) { System.out.println(voteTallies[idx]); idx ++ ; }

Ponder this: can all FOR loops be rewritten as WHILE loops?

  • yes because you can fit all of the parameters of a for loop into the code of a while loop

Ponder this: can all WHILE loops be rewritten as FOR loops?

  • no because a while loop validates the truthiness as a requirement to operate and that might not translate into the start/stop scenario that a for loop requires to run

Set “yardNeedsMowed” to true. Write WHILE loop that checks for “isSummer()”. inside the loop, write an IF that checks “yardNeedsMowed” and if true calls “yellAtJunior(chores.mowLawn())” After loop, call “sendJuniorToSchool(onTime)”

pseudo:

  • declare boolean yardNeedsMowed set to true
  • while isSummer() is true
    • if yardNeedsMowed is true
    • call yellAtJunior(chores.mowLawn())
  • after while loop
    • call sendJuniorToSchool(onTime)

Java:

boolean yardNeedsMowed = true;

while (isSummer()) {

if (yardNeedsMowed == true) {
  yellAtJunior(chores.mowLawn());
}

}

sendJuniorToSchool(onTime);