Say I have a loop like this:
for (int i = 0; i < someint; i++)
{
if (i == somecondition)
{
DoSomething();
continue;
}
doSomeOtherStuff();
}
Versus...
for (int i = 0; i < someint; i++)
{
if (i == somecondition)
{
DoSomething();
}
else
{
doSomeOtherStuff();
}
}
Is there any reason to use one over the other? Or is it just personal preference?
The main language I'm asking this for is Java, but I guess it also applies to most others.
Technically, no, but I find the second one prettier for this particular case.
I prefer the second construct...
for (int i = 0; i < someint; i++)
{
if (i == somecondition)
{
DoSomething();
//lets say lot of code
//..
//...
//...
//...
continue;
}
else
{
doSomeOtherStuff();
}
}
Lets say you had lot of code before the continue
. It is immediately apparent just by looking
else
{
doSomeOtherStuff();
}
the it is not executed unconditionally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With