0

I am new to programming and currently learning Java from Stanford free learning course. There was this assignment that I had trouble translating my algorithm into code. I feel like I have a misconception about nested loops.

My question in this code:

while (frontIsClear()) {   //LET THIS REFER AS BOOLEAN 1
  putBeeper();             //LET THIS REFER AS STAT 1
  if (frontIsClear()) {    // LET THIS REFER AS BOOLEAN 2
    move();                //LET THIS REFER AS STAT 2
  }
}

If BOOLEAN 1 is TRUE but BOOLEAN 2 is FALSE then will STAT 1 work or will it terminate everything?
I understand STAT 2 won't work because BOOLEAN 2 is false but I just don't get this loop inside condition etc.

  • 7
    What makes you think that "nested loops" like this aren't used frequently in software engineering? Have you tried creating an example to see what happens? – Winston Ewert Feb 9 '15 at 4:47
  • I'm new to programming so I wasn't really sure. But thanks for clearing things up. @WinstonEwert – Sadij Feb 9 '15 at 13:03
  • 3
    Nested loops are perfectly normal things in software engineering. They are sometimes frowned upon because the flow of code becomes hard to follow. The example you've given isn't a nested loop however, it's just an if statement inside a while loop. If you put another while loop inside the first you would have a nested loop, but that would not generally be a good idea. – sydan Feb 9 '15 at 13:40
  • There's no special rule for nested loops. They always need to be looked at with a bit of caution because it's easy to write something very slow with mutiply-nested loops but if that's truly what you need you do it anyway. I've got 8 levels deep once in a dataset verification issue--it took 12 hours and the only reason it could run at all was the tree got very heavily pruned on the second level. – Loren Pechtel Feb 16 '15 at 4:32
4

Every loop in Java (and most, but not all programming languages) has a loop conditional which is checked every iteration. It works just like an if statement: the conditional is a boolean expression. If it evaluates to true, the loop executes. If it evaluates to false, the loop stops and control flows to the next statement after the loop.

In this code, frontIsClear() is used to control both the while loop and the if conditional. If the method returns false in either case, the body of that construct (compound statement, everything between { and }) will not execute.

0

It Depends... If your putBeeper() method has any impact on this condition; frontIsClear(), it will not call to move() method if the conditional statement becomes false.

In your given example, there are no nested loops. For example:

int i = 0;
while (frontIsClear()) {
 putBeeper();
 while (i<3) {
  move();
 }
}

So you'll move 3 steps when frontIsClear().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.