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

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

Prestwood eMagazine

March 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...
First Comment
Comment 1 of 7

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 9 years ago

Comment 2 of 7

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

Comment 3 of 7

In fact so scarce that the majority seeking out expert Write My Essays to help with their paintings face not anything but unhappiness.

Posted 44 days ago

Comment 4 of 7

this is in all likelihood not what was expected as now statement1 will dependably be executed. It is never again part of the if articulation. This slip-up would be substantially less prone to happen if the opening prop were toward the finish of the if line. So pick whichever style suits you best, yet know about this issue when utilizing Berkley style, particularly when you have various engineers working in a similar module. Essay Services

Posted 37 days ago

Comment 5 of 7

Being an understudy, you need to defeat a couple of snags that come in your direction when you go up against a testing paper. Be that as it may, in the event that you neglect to obtain help from a written work proficient, you will neglect to beat your preventions, and you'll liable to come up short your subject too. Here are a few impediments that include: Essay Writing Service

Posted 37 days ago

Comment 6 of 7

Only a couple of days left before the due date? We have a group of exceedingly proficient, experienced and gifted paper scholars and they will furnish you with an astoundingly elegantly composed exposition to guarantee your prosperity. Essay Writing Service UK

Posted 37 days ago

Latest Comment
Comment 7 of 7

this is no doubt not what was normal as now statement1 will reliably be executed. It is never again part of the if explanation. This goof would be considerably less inclined to happen if the opening prop were toward the complete of the if line. So pick whichever style suits you best, yet think about this issue while using Berkley style, especially when you have different specialists working in a comparable module.Affordable SEO Services

Posted 12 days 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 = P1236A1
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
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!

1,596 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