• 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

incrementing 'i' ??

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

Answer : It will print 3
According to me, it will print 2 because in a do-while loop the condition is executed atleast once.
Please let me know where am I wrong
Sonir
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sonir shah:

Answer : It will print 3
According to me, it will print 2 because in a do-while loop the condition is executed atleast once.
Please let me know where am I wrong
Sonir


Hi Sonir,
I think you are bit confused her. Do loop executes the statements in loop atleast once, even if while condition is false. In the above program, when do executes the i will be incremented to 2, next while condition is checked, because it is true, the loop continues, at this time the i will be incremented to 3. After this the while statement will be false. And loop end there. results in answer 3
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
during the first execution of the loop i is incremented (i=2 now) and the condition evaluates to true so we go for another round. During the second execution i is incremented again (i=3 now) and the condition evaluates to false, so the loop is over...
i=3 at the end...
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this one is really really tricky.
the condition while (b = !b);
it looks like b = true at first. and atleast i thought that this one is an infinite loop. just escaped my mind that b is changing as well at each assignment in each loop.
sonir, you have got some great questions. where are you getting such questions from ?

Originally posted by Valentin Crettaz:
during the first execution of the loop i is incremented (i=2 now) and the condition evaluates to true so we go for another round. During the second execution i is incremented again (i=3 now) and the condition evaluates to false, so the loop is over...
i=3 at the end...

 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "==" and "=" have cause more programming bugs than many countries on Earth have money.
I remember that Pascal had a colon-equals operator that forced a distinction:
"=" test for equality
":=" assignment.
Since one of the main goals of java was to simplify the language so you avoided common bugs, I'm really really surprised and dissapointed that they didn't adopt two distinct symbols like "==" and ":="; Maybe someday I'll be able to add my own macros to java source code and define these symbols myself ;p
Rob
[ January 17, 2002: Message edited by: Rob Ross ]
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually rob if u think about it, they did simplify it a bit more than c++.
at least a boolean expression is only now true or false, unlike c++ and basic where 0 is false and 1 is true also.
this prevents many such errors as you said!
like:
if (x==5) and
if (x=5)
java will give compile error, and c++ and basic not!
 
reply
    Bookmark Topic Watch Topic
  • New Topic