Omitting Curly Brace In Do-While Statement

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Master Jake
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 69
Joined: Sat Dec 12, 2009 8:43 pm
Programming Language of Choice: C/C++
Location: United States
Contact:

Omitting Curly Brace In Do-While Statement

Post 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);
newbie1234
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 15
Joined: Fri May 15, 2009 9:01 am
Current Project: I'm currently making my own GUI
Favorite Gaming Platforms: Err... Linux?
Programming Language of Choice: C++
Location: St. Petersburg, Russian Federation

Re: Omitting Curly Brace In Do-While Statement

Post 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());
}
;)
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: Omitting Curly Brace In Do-While Statement

Post by RyanPridgeon »

It will work in other compilers and operating systems as well.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Omitting Curly Brace In Do-While Statement

Post 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.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Omitting Curly Brace In Do-While Statement

Post 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.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Omitting Curly Brace In Do-While Statement

Post 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
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Omitting Curly Brace In Do-While Statement

Post 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.
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Omitting Curly Brace In Do-While Statement

Post 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
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Omitting Curly Brace In Do-While Statement

Post 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).
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Omitting Curly Brace In Do-While Statement

Post 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:
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
Master Jake
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 69
Joined: Sat Dec 12, 2009 8:43 pm
Programming Language of Choice: C/C++
Location: United States
Contact:

Re: Omitting Curly Brace In Do-While Statement

Post 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? :)
newbie1234
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 15
Joined: Fri May 15, 2009 9:01 am
Current Project: I'm currently making my own GUI
Favorite Gaming Platforms: Err... Linux?
Programming Language of Choice: C++
Location: St. Petersburg, Russian Federation

Re: Omitting Curly Brace In Do-While Statement

Post 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.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Omitting Curly Brace In Do-While Statement

Post 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;
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Master Jake
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 69
Joined: Sat Dec 12, 2009 8:43 pm
Programming Language of Choice: C/C++
Location: United States
Contact:

Re: Omitting Curly Brace In Do-While Statement

Post 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.
Post Reply