Welcome to Dream.In.Code
Become a Java Expert!

Join 150,381 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,192 people online right now. Registration is fast and FREE... Join Now!




do while without body

 
Reply to this topicStart new topic

do while without body

bhandari
4 Feb, 2008 - 08:58 AM
Post #1

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
Hi All,

Kindly look into the code samples below:

CODE
int I =2;
do                              
while ( I > 1 );


CODE
int I =2;
do
    System.out.print("I is " + I);
while ( I > 1 );


The first one gives compilation error while the second one doesn't. Any explaination?
User is offlineProfile CardPM
+Quote Post

DillonSalsman
RE: Do While Without Body
4 Feb, 2008 - 09:12 AM
Post #2

D.I.C Head
Group Icon

Joined: 30 Oct, 2007
Posts: 72


Dream Kudos: 50
My Contributions
QUOTE(bhandari @ 4 Feb, 2008 - 09:58 AM) *

Hi All,

Kindly look into the code samples below:

CODE
int I =2;
do                              
while ( I > 1 );


CODE
int I =2;
do
    System.out.print("I is " + I);
while ( I > 1 );


The first one gives compilation error while the second one doesn't. Any explaination?

Not absolutely positive but I think the computer expects you to give it something to do if you says do{}while()
Kinda like if I called you over in the middle of the street to talk to you and then just plugged my headphones in my ear.
I wonder if computers think like that...hmmm

This post has been edited by DillonSalsman: 4 Feb, 2008 - 09:12 AM
User is offlineProfile CardPM
+Quote Post

iNaStY v3
RE: Do While Without Body
4 Feb, 2008 - 09:25 AM
Post #3

D.I.C Head
Group Icon

Joined: 3 Feb, 2008
Posts: 63


Dream Kudos: 50
My Contributions
Yes I'm pretty sure you're right. You have to tell the computer to do something when you say do{}while(), otherwise the do loop is incomplete.

The Do-Statement syntax looks like this:

do
Statement
while ( Expression );



Hope this helps (I looked it up in my book lol)

This post has been edited by iNaStY v3: 4 Feb, 2008 - 09:27 AM
User is offlineProfile CardPM
+Quote Post

Programmist
RE: Do While Without Body
4 Feb, 2008 - 03:10 PM
Post #4

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,257



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
QUOTE(bhandari @ 4 Feb, 2008 - 10:58 AM) *

Hi All,

Kindly look into the code samples below:

CODE
int I =2;
do                              
while ( I > 1 );


CODE
int I =2;
do
    System.out.print("I is " + I);
while ( I > 1 );


The first one gives compilation error while the second one doesn't. Any explaination?


Not sure why you'd want to do that, but add a semicolon and it will compile.

CODE
int I =2;
do                              
;  // <---- semicolon
while ( I > 1 );

User is online!Profile CardPM
+Quote Post

B-Boy209
RE: Do While Without Body
4 Feb, 2008 - 03:35 PM
Post #5

D.I.C Head
**

Joined: 9 Dec, 2007
Posts: 61


My Contributions
QUOTE(bhandari @ 4 Feb, 2008 - 09:58 AM) *

Hi All,

Kindly look into the code samples below:

CODE
int I =2;
do                              
while ( I > 1 );


CODE
int I =2;
do
    System.out.print("I is " + I);
while ( I > 1 );


The first one gives compilation error while the second one doesn't. Any explaination?

the reason why the first one doesnt work is because like somebody mentioned you are trying to tell it to "do" something "while" something so if you put nothing it wont compile because there is nothing there for it to do while something...so by putting
CODE

int i=2;
do
; <<<<<<<<this right here is telling it to "do" nothing but if there was no semi-colon that would mean that there wasnt nothing there at all...get it
while (i>2);


This post has been edited by B-Boy209: 4 Feb, 2008 - 03:37 PM
User is offlineProfile CardPM
+Quote Post

bhandari
RE: Do While Without Body
4 Feb, 2008 - 10:35 PM
Post #6

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
Thanks to you all.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:21PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month