Your full service technology partner! 
-Collapse +Expand
Search C++ Group:

-Collapse +Expand C++ To/From
-Collapse +Expand C++ Study Test
-Collapse +Expand C++ Store

Prestwood eMagazine

June 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.");


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 )

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 )

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 15 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 15 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 = P1135A1
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 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
Since 2/11/2009
Sales Website: Or visit our legacy sales site:

©1995-2024 Prestwood IT Solutions.   [Security & Privacy]