Page 1 of 1

Omitting Curly Brace In Do-While Statement

Posted: Sun Dec 27, 2009 5:42 am
by Master Jake
Is it OK to write single-statemented do whiles without the curly brace (like I do for my fors, ifs, whiles, etc.).

Example:

Code: Select all

do
{
	Something();
}
while (something);
As....

Code: Select all

do
	Something();

while (something);

Re: Omitting Curly Brace In Do-While Statement

Posted: Sun Dec 27, 2009 7:07 am
by newbie1234
Yeah, I guess you could do that in Code::Blocks in Ubuntu:

Code: Select all

#include <iostream>
#include <cstdio>

class SomeClass {

    bool quit;

    public:

    SomeClass();

    void Something();

    bool getQuit();

};

SomeClass::SomeClass()
{
    quit = false;
}

void SomeClass::Something() {

    printf("strange");
}

bool SomeClass::getQuit() {

    return quit;
}

SomeClass SomeClass1;

int main()
{
    do
        SomeClass1.Something();

    while (!SomeClass1.getQuit());
}
;)

Re: Omitting Curly Brace In Do-While Statement

Posted: Sun Dec 27, 2009 8:27 am
by RyanPridgeon
It will work in other compilers and operating systems as well.

Re: Omitting Curly Brace In Do-While Statement

Posted: Sun Dec 27, 2009 10:18 am
by Falco Girgis
It's just as fine as doing:

Code: Select all

if() something;
else something;
Some programmers don't like that and always use curly braces for one-liners, but I think that's just being anal. If you asked me, I would say it's perfectly legit.

Re: Omitting Curly Brace In Do-While Statement

Posted: Sun Dec 27, 2009 2:04 pm
by K-Bal
I always use braces. I once unconsciously commented out the body of an if-statement and asked myself for some hours why nothing was working.

Re: Omitting Curly Brace In Do-While Statement

Posted: Sun Dec 27, 2009 2:41 pm
by GroundUpEngine
GyroVorbis wrote:It's just as fine as doing:

Code: Select all

if() something;
else something;
Some programmers don't like that and always use curly braces for one-liners, but I think that's just being anal. If you asked me, I would say it's perfectly legit.
Agreed

Re: Omitting Curly Brace In Do-While Statement

Posted: Sun Dec 27, 2009 6:55 pm
by XianForce
GyroVorbis wrote:It's just as fine as doing:

Code: Select all

if() something;
else something;
Some programmers don't like that and always use curly braces for one-liners, but I think that's just being anal. If you asked me, I would say it's perfectly legit.
Yeah, it's legit, but I personally put braces just because it's easier for me to see a problem.

Re: Omitting Curly Brace In Do-While Statement

Posted: Sun Dec 27, 2009 10:10 pm
by hurstshifter
XianForce wrote:
GyroVorbis wrote:It's just as fine as doing:

Code: Select all

if() something;
else something;
Some programmers don't like that and always use curly braces for one-liners, but I think that's just being anal. If you asked me, I would say it's perfectly legit.
Yeah, it's legit, but I personally put braces just because it's easier for me to see a problem.
This

Re: Omitting Curly Brace In Do-While Statement

Posted: Mon Dec 28, 2009 12:17 pm
by dandymcgee
hurstshifter wrote:
XianForce wrote:
GyroVorbis wrote:It's just as fine as doing:

Code: Select all

if() something;
else something;
Some programmers don't like that and always use curly braces for one-liners, but I think that's just being anal. If you asked me, I would say it's perfectly legit.
Yeah, it's legit, but I personally put braces just because it's easier for me to see a problem.
This
I do this as well. I often add lines of debug code to my conditionals, and that never ends well if you didn't use braces for the one-liner. It also improves readability (for me).

Re: Omitting Curly Brace In Do-While Statement

Posted: Mon Dec 28, 2009 5:55 pm
by short
hurstshifter wrote:
XianForce wrote:
GyroVorbis wrote:It's just as fine as doing:

Code: Select all

if() something;
else something;
Some programmers don't like that and always use curly braces for one-liners, but I think that's just being anal. If you asked me, I would say it's perfectly legit.
Yeah, it's legit, but I personally put braces just because it's easier for me to see a problem.
This
This. :roll:

Re: Omitting Curly Brace In Do-While Statement

Posted: Tue Dec 29, 2009 8:58 am
by Master Jake
Thanks everyone.

By the way, newbie1234, what's all this about a "must be" object orientation. The language doesn't force me to use it so why should I when I don't need to? :)

Re: Omitting Curly Brace In Do-While Statement

Posted: Tue Dec 29, 2009 10:35 am
by newbie1234
Master Jake wrote:Thanks everyone.

By the way, newbie1234, what's all this about a "must be" object orientation. The language doesn't force me to use it so why should I when I don't need to? :)
I'm just being a dick.

Re: Omitting Curly Brace In Do-While Statement

Posted: Mon Jan 04, 2010 11:17 am
by Ginto8
GyroVorbis wrote:It's just as fine as doing:

Code: Select all

if() something;
else something;
Some programmers don't like that and always use curly braces for one-liners, but I think that's just being anal. If you asked me, I would say it's perfectly legit.
I use something similar, but it helps me differentiate the condition and action:

Code: Select all

if()
    something;
else
    something;

Re: Omitting Curly Brace In Do-While Statement

Posted: Mon Jan 04, 2010 11:54 am
by Master Jake
Ginto8 wrote:

Code: Select all

if()
    something;
else
    something;
I use that method too, but I also put an additional space between the if, else-if, and else statements like:

Code: Select all

if (conidition)
	result;

else if (condition)
	result;

else
	result;
Just helps me.