Eric Foster ee593db846 typed out java loops | 6 anni fa | |
---|---|---|
README.md | 6 anni fa |
some simpler exercises for IFs and loops.
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”.
Write an IF statement that checks “player1.isAlive()” and if that’s false, calls “displayGameOver(player1)”
if !(player1.isAlive()){
displayGameOver(player1);
}
Write an IF statement that checks the “temperature(room)” and if that check is less than 70, calls “heatOn()” else calls “coolOn()”
temperature(room) < 70 ? heatOn() : coolOn();
Write an IF statement that checks “outsideTemp()” is less than 50 AND “insideTemp()” is less than 62, calls “startAFire(fireplace1)”
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()”
if (fuelLevel < 0.08){
refuel();
}
For each loop you write, print “Hello” on each loop iteration.
Write a FOR loop that counts from 1 to 10.
for(int i=1; i<=10; i++){
Console.println("Hello");
}
Write a FOR loop that makes 10 iterations, start at 21
for(int i=21; i<31; i++){
Console.println("Hello");
}
Write a FOR loop that counts down from 100 to 0
for(int i=100; i>=0; i--){
Console.println("Hello");
}
Write a FOR loop from 0 to 32 by 2s
for(int i=0; i<=32; i+=2){
Console.println("Hello");
}
Write a FOR loop from 1 to less than 5001 by 11s.
for(int i=1; i<5001; i+=11){
Console.println("Hello");
}
Write a nested FOR loop(s), where one counts from 0 to less than 20 and the inner one counts from 0 to 4.
for(int i=0; i<20; i++){
for(int j=0; i<=4; i++){
Console.println("Hello");
}
}
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”
for(int i=5; i<=105; i++){
if(i>51){
Console.println("Hello Zipcode");
} else {
Console.println("Hello");
}
}
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!”
while(!(gps.currentLocation().equals("Home"))){
driveSomeMore();
} Console.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()”.
highestScore = 0; currentScore = game.nextScore(); while(currentScore > highestScore){
highestScore = currentScore;
currentScore = game.nextScore();
}
Rewrite the previous WHILE loop as a REPEAT..UNTIL loop. Notice how the “currentScore” variable usage is different.
highestScore = 0; currentScore = game.nextScore(); repeat {
highestScore = currentScore;
currentScore = game.nextScore();
} until (currentScore < highestScore);
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()”
while(server.isRunning()){
waitFor(5);
} if(!(server.isRunning())){
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”
int i; i = 7; while(i < 50){
i+=7;
}
Given an array voteTallies[], write a FOR loop that prints out each value in the array.
for(int i = 0; i <= voteTallies.length - 1; i++){
System.out.println("Value " + (i+1) + " = " + 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.
int idx = 0; while(idx <= voteTallies.length -1){
System.out.println("Value " + (i+1) + " = " + voteTallies[i]);
idx++;
}
Ponder this: can all FOR loops be rewritten as WHILE loops?
For loops are used when the loop amount is known beforehand. These can all be translated into while loops because we can set the index, exit criteria, and index increment to break the while loop at the known number of loops.
Ponder this: can all WHILE loops be rewritten as FOR loops?
While loops are used when the loop amount is unknown beforehand. These cannot all be translated into for loops because for loops require the number of loops to be known, so if the while loop run amount is unknown we cannot define our for loop.
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)”
yardNeedsMowed = true; while(isSummer()){
if(yardsNeedMowed){
yellAtJunior(chorse.mowLawn());
}
} sendJuniorToSchool(onTime);