Jump to content

[Solved]do...loop in java (?)

Go to solution Solved by Ciccioo,

VB only has

do while... loop / do until... loop / do... loop while / do... loop until

source

 

i don't think there is any language which has a loop without a condition

Hi guys

 

So this is kinda a homework question. I'll have to explain all the different loops and "decision makers" in Java.

 

Is there such a thing as a do...loop?

 

(it's written uppon the things I'll need to explain)

 

I know about "do...while" and "while".

 

I could only imagine it being like a do..while loop but without the arguments (which would make it useless(?)).

 

Thanks for any answers

Greetings Marius

btw I use arch

Link to comment
Share on other sites

Link to post
Share on other sites

- snip -

do... loop? how should it behave? you said it has no condition? in which language did you hear of it?

as you said, it should be a 'decision maker', but there's no point in it if you don't give it a decision to make

Link to comment
Share on other sites

Link to post
Share on other sites

I don't exactly know, unfortunatley

Our teacher wrote this on the list of things we must cover.

It *should* exist in Java, but I don't think it does. I've seen something similar in VB tho.

btw I use arch

Link to comment
Share on other sites

Link to post
Share on other sites

VB only has

do while... loop / do until... loop / do... loop while / do... loop until

source

 

i don't think there is any language which has a loop without a condition

Link to comment
Share on other sites

Link to post
Share on other sites

"If.. then... Else.." and "Case" statements would fall under "Decision Makers", I'm sure there are others out there, those two instances spring to mind.

Social Links: LTT Forum    Website    Instagram    Twitter    YouTube    LinkedIn    Steam 

 

The Build

CPU Intel i7 4770k 4.4ghz Motherboard Asus Sabertooth Z87 RAM 34gb (4 x 8gb) Corsair Vengeance 1600mhz GPU Evga GTX780 Classified Case Lian Li O11 Dynamic Storage Samsung 840 256gb SSD PSU Corsair HX750 Display Achieva Shimian QH300-IPSMS 1600p Cooling Corsair H100i Keyboard Corsair K90 Mouse Corsair M65 Sound 3.1 Surround Sound Fans 2x Corsair SP120 PWM, 3x Corsair AF140 Q.E, and 1x Corsair AF120 Q.E.

Link to comment
Share on other sites

Link to post
Share on other sites

Solution: I'll do some further research and

if(i don't find anything){

i'll just state that this doesn't exist (in java)

}

btw I use arch

Link to comment
Share on other sites

Link to post
Share on other sites

The only "do while loop" which exists in Java is this one.
The only other loops I can think of in Java are the for loop and for each loop (for loop without counter which just goes through all elements).

 

Hope this helps  :) .

 

Edit: you always need a condition in your loop so that it can exit, otherwise you will have an infinite loop which just crashes your application. So yes, a loop without condition would be pretty useless  :D .

Link to comment
Share on other sites

Link to post
Share on other sites

 

Edit: you always need a condition in your loop so that it can exit, otherwise you will have an infinite loop which just crashes your application. So yes, a loop without condition would be pretty useless  :D .

There are plenty of times you want an infinite loop and use a break to get out instead.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Oh actually could that be a do...loop?

Just repeating an infinite number of times and you exit it using an if statement inside the loop (with break)

That would be inefficient tho wouldn't it?

btw I use arch

Link to comment
Share on other sites

Link to post
Share on other sites

Oh actually could that be a do...loop?

Just repeating an infinite number of times and you exit it using an if statement inside the loop (with break)

That would be inefficient tho wouldn't it?

from the syntax perspective it's still a 'do while... loop' because the condition is present, it just happens to be 'true'

from the logical perspective it's still a 'do... loop until' because the exit condition is present, it just isn't written in the loop statement

in fact, every 'infinite loop' done like that can be converted to a normal loop, it's just a matter to which solution happens to be the most elegant and clear to implement

 

edit: and no, it's not a matter of efficiency, it would be fine

Link to comment
Share on other sites

Link to post
Share on other sites

VB only has

do while... loop / do until... loop / do... loop while / do... loop until

source

 

i don't think there is any language which has a loop without a condition

C++ also has do{}while(statement);

And a 'without condition' part is simply while(true). so it always loops, and I assume it has a break; somewhere in the loop itself.

Link to comment
Share on other sites

Link to post
Share on other sites

C++ also has do{}while(statement);

And a 'without condition' part is simply while(true). so it always loops, and I assume it has a break; somewhere in the loop itself.

every language i heard of has a postconditional loop statement, that wasn't the point i was trying to make

 

about the while(true), i explained in my previous post why it still counts as a loop WITH a condition

Link to comment
Share on other sites

Link to post
Share on other sites

every language i heard of has a postconditional loop statement, that wasn't the point i was trying to make

 

about the while(true), i explained in my previous post why it still counts as a loop WITH a condition

I would argue that conditions can change, and hard-coding a condition like that to a loop is not really a condition anymore. It's The solution for endless loops, even more, I found a cool trick to make it clearly stand out:

#define FOREVER while(true)...FOREVER {}
Link to comment
Share on other sites

Link to post
Share on other sites

 

I would argue that conditions can change, and hard-coding a condition like that to a loop is not really a condition anymore. It's The solution for endless loops, even more, I found a cool trick to make it clearly stand out:

#define FOREVER while(true)...FOREVER {}

In most languages (I think every one that supports this syntax)

for(;

is the standard infinite loop, no condition involved.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

In most languages (I think every one that supports this syntax)

for(;

is the standard infinite loop, no condition involved.

Here's why I prefer while(true):

http://stackoverflow.com/a/1401208/2188694

+ same topic, different answer, if you want to be doubly sure :D

while (!false && true)
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×