I.T. Discussion Community!
-Collapse +Expand
C++
Search C++ Group:

Advanced
-Collapse +Expand C++ To/From
To/FromCODEGuides
-Collapse +Expand C++ Study Test
PRESTWOODCERTIFIED
-Collapse +Expand C++ Store
PRESTWOODSTORE

Prestwood eMagazine

September Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingC++Standard C++C++ Language...   Print This     
C++ Code Snippet:
 A flashcard from our C++ Flashcards Library
 A code snippet from our C++ Code Snippets Page
 Tags: C++ , If Statement
C++ If Statement (if..else if..else)

Same as standard C.


//C++Builder example using the VCL ShowMessage.
int x;
  
x = 8;
  
if (x == 10) {
ShowMessage("x is 10.");
} else if (x < 10) {
ShowMessage("x is less than 10.");
} else {
ShowMessage("x must be greater than 10.");
}

Comments

1 Comments.
Share a thought or comment...
Comment 1 of 2

What happened to the indentation in your example?

Coding style is almost a religious issue with most developers. A common point of contention is the location of the braces. The most popular styles are K&R style and Berkley style. K&R style is named after Kernighan and Ritchie, authors of "The C Programming Language", and is sometimes called kernel style. K&R style locates the opening brace at the end of the if/else/for/while line. Berkley style moves the opening brace to the next line and aligned with the if/else/for/while keyword. I am tolerant of either style; however, there is one significant advantage to K&R style. Suppose you have the following example in Berkley style.

    if ( expression )
    {
        statement1;
    }

Now suppose another developer adds a statement to the if statement but fails to notice the braces so the result looks like this.

    if ( expression )
        statement2;
    {
        statement1;
    }

This example will still compile because a compound statement can occur anywhere a statement can occur; however this is almost certainly not what was intended as now statement1 will always be executed. It is no longer part of the if statement. This mistake would be much less likely to happen if the opening brace were at the end of the if line. So choose whichever style suits you best, but be aware of this issue when using Berkley style, especially when you have multiple developers working in the same module.

Posted 9 years ago

Comment 2 of 2

Good catch. Thanks. I'm in the process of reviewing the other posts now.

I just got lazy. Although to be honest I'm not a stickler for indentation style. My company, Prestwood Software, is a general development company and we see so many different styles (and use of a non-standard style) that indentation is more of a luxury than a necessity. I just tell my developers to fit in as best they can which is easier, of course, when K&R, Berkeley, or any of the others are in use. For new projects we have more influence but even then many of our clients have coding standards for us to follow. Going forward I will strive for consistency online.

In general I try to stick with the default coding style of the development tool I'm using and/or what I've seen most. Based on my experience, that means Berkley/Allman indent style for C++, Java, JavaScript, and PHP and a K&R indent style for C# (and Object Pascal but for Object Pascal it's rare to see the Berkeley indent style in use). I do use tabs for indentation but I convert tabs to spaces when writing because horizontal space is valuable and limited with web pages and printing.

Posted 9 years ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1242A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101877 Counter
9596
Since 2/11/2009
-
   Contact Us!
 
PrestwoodBoards.com was developed and is maintainted by me. Do you have a question or suggestion? Do you see a problem? Contact me now. My goal is to build an ad-free and spam-free source of I.T. information with many contributers (ok to promote your website/company in your bio). Yes, my company Prestwood IT Solutions is mentioned in my bio which shows with every post, but you can contribute and promote your pet project too!

2,216 People Online Now!!  
Sign In to see who's online now!  Not a member? Join now. It's free!
Show more stats...


©1995-2018 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech