Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to check a condition after every method in a loop? If so, how?

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();
}
like image 503
Metacomet10 Avatar asked Feb 22 '15 06:02

Metacomet10


People also ask

Can you have an OR condition in a while loop?

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.

Can we pass two conditions in for loop?

It is possible to use multiple variables and conditions in a for loop like in the example given below.

Does a for loop check condition first?

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.

What happens after you write the while condition in a loop?

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?

What happens when a loop condition is false in Python?

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

What is the use of while condition in C++?

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.

What happens at the end of a while 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?


1 Answers

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;
    ...
}
like image 123
Sergey Kalinichenko Avatar answered Sep 23 '22 19:09

Sergey Kalinichenko