Like I said in the title I have a loop in an RPG I'm making about High School. This is the main loop that sets up your day to act out individual sequences in chronological order. My question is how could I make it so that I check whether the boolean
"beat" or the boolean
"lost" (referring to the status of the game) has been tripped to true after every method in the loop but still keeping the methods together in a loop. Is nested if statements inside my while loop the only way?
while (!g.getBeat() || g.getLost())
{
g.wakeUp();
g.goToSchool();
g.beforeLunch();
g.lunchActivity();
g.afterLunch();
g.afterSchool();
g.home();
g.sleep();
}
As seen on line 4 the while loop has two conditions, one using the AND operator and the other using the OR operator. Note: The AND condition must be fulfilled for the loop to run. However, if either of the conditions on the OR side of the operator returns true , the loop will run.
It is possible to use multiple variables and conditions in a for loop like in the example given below.
While loops check for the stopping condition first, and may not execute the body of the loop at all if the condition is initially false.
After writing WHILE, the condition must be written, the loop will execute until the condition will be true. Otherwise, the loop will not execute. 8. What does the next statement in loops do?
If the while condition is true, the statements that represent the loop are executed. When the condition represents false, the loop condition will stop and the programm continues beyond the loop. Let’s take an example and check how to use them while loop condition in Python
This statement is used to construct loops. The condition is prepared to check if it’s True or false. If the while condition is true, the statements that represent the loop are executed. When the condition represents false, the loop condition will stop and the programm continues beyond the loop.
Clarification: WHILE LOOP is repeated until a condition no longer holds. The condition is first tested and if it is found to be true then the loop iteration starts. With the end of iteration, the condition is again tested and the process continues until the condition is not false. 7. Which of the following is correct syntax for WHILE LOOP?
You would have to do it manually. To help you write a little less code, make a method that checks both conditions:
private boolean stopTheLoop() {
return g.getBeat() || g.getLost();
}
Now convert your loop to infinite with checks after each method:
while (true) {
g.wakeUp();
if (stopTheLoop()) break;
g.goToSchool();
if (stopTheLoop()) break;
g.beforeLunch();
if (stopTheLoop()) break;
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With