Nested if statements (C programming)

Started by Zexion, October 29, 2012, 07:45:05 pm

Previous topic - Next topic

Zexion

Hey all! I'm taking an intro to computers class which is about programming in C. Every week we get an assignment for homework, and I usually have no problem doing it. While this may not be a problem per say, I am doubting that I am doing this efficiently... I just feel like I'm using more if's than I need to.. here's the code:

/* Nested if statements for ph flowchart */

#include <stdio.h>

main()
{
      double ph;
     
      if ph > 7 && ph < 12
      printf("Alkaline");
      else if ph > 7
      printf("Very alkaline");
      else if ph == 7
      printf("Neutral");
      else if ph > 2
      printf("Acidic");
     


It's not done, just wanted to put this up before everyone shuts off lol.

KK20

Since the class is entry level programming, I don't think your teacher expects anything complicated. However, the first two if-statements are something I wouldn't do (Probably do if ph >= 12 first and then else if ph > 7).

What exactly are the guidelines to the assignment?

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Ryex

^^ what he said

in a situation where you are checking if you have a number in a range it's best to start at the highest range and work your way down using '>'

in this case

if >= 12  (could also use > 11 if you want to consider 11.1 to be in that range)
else if >7
else if == 7
else if > 2
else (we know it's less than 2)

if this were ruby I'd recommend a switch with range operators but c's switch statement is... weird.
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Zexion

The rules are that I can only use nested if statements, no cases or individual if statements. It's basically a flow chart and for the first condition i did that because it says
if ph >7 true and if ph < 12 true then print "alkaline" if false print "Very alkaline"
but if ph > 7 is not and ph is = 7 then print "acidic" and if ph > 2 print "very acidic"

Blizzard

You may want to use brackets as the are required in C.

if (ph > 7 && ph < 12)


Also, the conditions would be

if (ph == 7)
{
    printf("neutral\n");
}
else if (ph < 7)
{
    if (ph < 3)
    {
        printf("very ");
    }
    printf("acidic\n");
}
else if (ph > 7)
{
    if (ph > 12)
    {
        printf("very ");
    }
    printf("alkaline\n");
}


Those are literally nested if-statements. Or the alternative:

if (ph < 3)
{
    printf("very acidic\n");
}
else if (ph < 7)
{
    printf("acidic\n");
}
else if (ph == 7)
{
    printf("neutral\n");
}
else if (ph < 12)
{
    printf("alkaline\n");
}
else // if (ph <= 14)
{
    printf("very alkaline\n");
}

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Zexion

I think the second one is what she would expect :P but the first one was what I wanted to know lol. I mainly don't want to create bad habits when programming don't want to have to fix stuff later on. Thanks blizz  :up:

Blizzard

The first code is faster and more optimized (assuming a printf() call takes as long as the given string and has no calling overhead), but a bit harder to read while the second one is much easier to read. I prefer the first type since it's not that hard to read. You just need a movement to figure out what's going on.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

DarknessSurrounds

Are you allowed to use switch statements?  They're very nice for situations such as this.

http://www.cprogramming.com/tutorial/lesson5.html

Zexion

Actually this was a while back, so it's already done. At the time though i wasn't able to use switch statements and I was just having problems getting used to a new language (mainly because rgss made me lazy) , but it's cake now lol. I ended up getting a b on the final which was a bummer D: but whatever life goes on... and oh lord at least it wasn't as bad as trig... I just don't get that stuff man..

Blizzard

HOW DOES SHE DARE GIVE ME ONLY A B FOR MY MASTERPIECE
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Zexion

Quote from: Blizzard on January 12, 2013, 08:28:56 am
HOW DOES SHE DARE GIVE ME ONLY A B FOR MY MASTERPIECE


LOL. Literally busted out laughing XD
Rofl...but no, she gave me a 100 for that, but it was just a homework assignment :P I was talking about the final exam that i totally got cocky on lol.

Blizzard

I know, but I achieved my goal in making somebody laugh.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.