Click here to Skip to main content
16,015,809 members
Articles / Programming Languages / C++
Tip/Trick

"if" with no code block parenthesis

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
12 Feb 2011CPOL 23.4K   1   8
Avoid using "if" with no code block parenthesis

Though it is supported, You should not use "if" without parenthesis for code block.

You should use:

MIDL
MIDL
if(condition)
{
// Your code 
}

and you should not use:

MIDL
MIDL
if(condition)
// Your code

One of my colleagues encountered the problem due to the unparenthesized "if".

The story goes like this:

His code was like this:

MIDL
MIDL
if(condition)
   System.out.println("This is log message to be printed on Console");

for( int i = 0; i < 10; i ++ )
{
    //Some important Code 
}

For final release, he globally replaced the System.out.println with //System.out.println so that all SOP statements would be commented.
But due the use of unparenthesized "if", the for loop went inside the if which was not desirable.

and the product started crashing.....

So the situation was ....Product was running properly if we keep all SOP.

After scanning the code throughly, we found out the culprit .....if with no code block parenthesis :omg:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) nVidia Corporation
India India
Prafulla is a passionate software developer. He has mostly worked on C++ and .NET projects. He has a Masters Degree in Software Systems from BITS, Pilani India. Currently he is working for nVidia corporation as a Senior System Engineer.

He is an embedded technology enthusiast. He likes to spend his time on electronics projects.

Comments and Discussions

 
GeneralReason for my vote of 5 You learn something new every day. ... Pin
ZamirF15-Feb-11 9:17
ZamirF15-Feb-11 9:17 
GeneralIndivara is completely correct - the issue is not so much th... Pin
Pete Goodsall14-Feb-11 2:37
Pete Goodsall14-Feb-11 2:37 
GeneralReason for my vote of 5 AGREED!! I don't even like the if's... Pin
Joe Bonifazi11-Feb-11 5:41
Joe Bonifazi11-Feb-11 5:41 
GeneralHave to agree with Indivara on this. Pin
Albert Holguin10-Feb-11 12:43
professionalAlbert Holguin10-Feb-11 12:43 
GeneralGood. Pin
shakil030400310-Feb-11 3:34
shakil030400310-Feb-11 3:34 
GeneralI see the danger here, but the bigger issue seems to be glob... Pin
Indivara9-Feb-11 22:40
professionalIndivara9-Feb-11 22:40 
QuestionIS THERE ANY RULE ? Pin
sudheer muhammed10-Feb-11 0:40
sudheer muhammed10-Feb-11 0:40 
AnswerRe: IS THERE ANY RULE ? Pin
dontumindit13-Feb-11 11:44
dontumindit13-Feb-11 11:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.