• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Rules for if/else in a do/while loop

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me go ahead and show the code.


Value of T=    Value of K=
t was easy to figure out , I got 18 which is correct.  , but I am having issues with the value of k . I know now it is 21, but I dont understand how the bracket rule works with if else.

I understand with for loops if you dont use brackets, it only looks at the next line, if you use brackets it will see the whole block.
So my question here is after the "else"  which ones are being looked at.. regardless if , ""if"" is true, and "else" is false, or " if" is false and "else " is true. how many times would k++, k=k+a, b++ being hit?  

b++ has to be getting hit at some point in time else the loop would be infinite.   I have come up with different answers but i cant seem to hit 21 for k
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I got 21 on my first attempt on paper. What values did you get?

Henry
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Always use braces with if statements.
 
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

damon greenfield wrote:but I dont understand how the bracket rule works with if else.


1. If there are no brackets, only 1 statement being counted as an 'if' or 'else' blocks.
2. If in the code is if-else and no brackets, then in if statement must be 1 statement only, otherwise code wouldn't compile.
3. If there are form like:

Then else being grouped to nearest 'if', that means to inner if.
 
Daniel Dagenhart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the first time through I have k =9 b=8 , onto 2nd loop i have k=16 b=9. onto 3rd loop , this is where I am having the issue, the if statement now comes true because b>9 , so if the IF statement is true, then its not doing k++ and when i do k=k+a , i come up with 23 ...
Thats why I need help with how to read this with out there being brackets. I searched the internet, but I cant find any threads talking about my issue, only about one statement after else, not multiple ones.

 
Daniel Dagenhart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Always use braces with if statements.



This was a question on my mid term I had earlier today.  The professor purposely leaves brackets out some times to try to trick us.. As I said, I understand the rule for for loops, but I cant seem to figure out this do while with an if else inside of it.
 
Liutauras Vilda
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't look into it deeply, but keep in mind that lines 2 and 3 (in your case 10 and 11) get executed until condition in 'while' gets false:
Having said that, it means regardless of expression evaluation in 'if' and 'else' on lines 7 and 8
 
Liutauras Vilda
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This exercise isn't mainly about understanding how mathematical operations work, but more about knowledge how to trace variables.

Do you know the technique with pencil and piece of paper by drawing a table and writing down the values during each iteration?
 
Daniel Dagenhart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

damon greenfield wrote:but I dont understand how the bracket rule works with if else.


1. If there are no brackets, only 1 statement being counted as an 'if' or 'else' blocks.
2. If in the code is if-else and no brackets, then in if statement must be 1 statement only, otherwise code wouldn't compile.
3. If there are form like:

Then else being grouped to nearest 'if', that means to inner if.



there are three things after the else statement..  If, the IF is true, then the ELSE will be false, K++ would be skipped, but its the two lines after that I am not sure about.  I mean b++ has to be getting hit at some point in time else the loop would never end.
 
Liutauras Vilda
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Bear said, always use braces, regardless how the code looks like during the time you have been given. You can rewrite it for yourself.
If during the exam you get such exercise - rewrite in the form it should be looking like.

Can you solve it now?
 
Daniel Dagenhart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:This exercise isn't mainly about understanding how mathematical operations work, but more about knowledge how to trace variables.

Do you know the technique with pencil and piece of paper by drawing a table and writing down the values during each iteration?



Yes our professor had taught us the long way to do loops, and the short way to figure out loops. Not all loops can be done the short way especially if you have a variable going up exponentially. But that is a different topic, this is about how many lines are being recognized after the ELSE command when ELSE is false, or when it is true..
There are 3 lines .. k++, K=K+a, and b++ ...

If this was a for  loop with { }  then everything in between those would be seen, no brackets, only the next line is seen.  I am confused with the if/else inside of a do/while
 
Liutauras Vilda
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many times do-while loop body will get iterated?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

damon greenfield wrote:onto 3rd loop , this is where I am having the issue, the if statement now comes true because b>9 , so if the IF statement is true, then its not doing k++ and when i do k=k+a , i come up with 23 ...



Can you explain how you got 23 after the third loop? B=9 and A=12... so, after k=k+2-b (line 5), and k=k+a (line 10), shouldn't you get k=9 (after line 5), and then k=21 (after line 10)? ... as you correctly surmised, k++ (line 9) doesn't run with the third iteration.

Henry
 
Daniel Dagenhart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

damon greenfield wrote:

Liutauras Vilda wrote:This exercise isn't mainly about understanding how mathematical operations work, but more about knowledge how to trace variables.

Do you know the technique with pencil and piece of paper by drawing a table and writing down the values during each iteration?



Yes our professor had taught us the long way to do loops, and the short way to figure out loops. Not all loops can be done the short way especially if you have a variable going up exponentially. But that is a different topic, this is about how many lines are being recognized after the ELSE command when ELSE is false, or when it is true..
There are 3 lines .. k++, K=K+a, and b++ ...

If this was a for  loop with { }  then everything in between those would be seen, no brackets, only the next line is seen.  I am confused with the if/else inside of a do/while



Yes !! THank you !!  You answered my question with showing the brackets..  my issue was if k=k+a;   b++; were being hit whether or not based on the IF being true/false, or the Else being true/false.  I got 21 this time.
 
Daniel Dagenhart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:How many times do-while loop body will get iterated?


it goes through 3 times. How many times a loop goes through wasnt my problem, it was the conditions for k=k+a;  b++; to being hit.  I thought it was only being hit when the statement was false, I didnt realize it was being hit all 3 times through the loop.
 
Daniel Dagenhart
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

damon greenfield wrote:onto 3rd loop , this is where I am having the issue, the if statement now comes true because b>9 , so if the IF statement is true, then its not doing k++ and when i do k=k+a , i come up with 23 ...



Can you explain how you got 23 after the third loop? B=9 and A=12... so, after k=k+2-b (line 5), and k=k+a (line 10), shouldn't you get k=9 (after line 5), and then k=21 (after line 10)? ... as you correctly surmised, k++ (line 9) doesn't run with the third iteration.

Henry


yeah I some how had 11 instead of 9 before adding the final 12 to make it 21.  I am not sure how i got it now, because I already erased the work, and reworked the problem.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic