Author Topic: Nested if statements (C programming)  (Read 4428 times)

Offline Zexion

  • Addicted to LoL
  • Chaos User
  • ******
  • Posts: 1519
  • LV: 69
  • Gender: Male
  • My anaconda don't...jk it does
    • View Profile
Nested if statements (C programming)
« on: October 30, 2012, 02:45:05 AM »
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:

Code: [Select]
/* 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.

Offline KK20

  • Master Scripter Fixer
  • Global Moderator
  • Guardian of Chaos
  • ****
  • Posts: 3143
  • LV: 378
  • Gender: Male
  • Choco Octo <3
    • View Profile
Re: Nested if statements (C programming)
« Reply #1 on: October 30, 2012, 03:42:56 AM »
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 AceUpgrade RMXP to RMVXA performance!
XPA TilemapTilemap rewrite with many features, including custom resolution!


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

Join the CP Discord Server!

Offline Ryex

  • Arctic Bird of Programming
  • Global Moderator
  • Chaos Ultimate
  • ****
  • Posts: 5135
  • LV: 198
  • Gender: Male
  • Wants to write a compiler for fun
    • View Profile
Re: Nested if statements (C programming)
« Reply #2 on: October 30, 2012, 04:15:56 AM »
^^ 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.

DropBox, the best free file syncing service there is.
(click to show/hide)

Offline Zexion

  • Addicted to LoL
  • Chaos User
  • ******
  • Posts: 1519
  • LV: 69
  • Gender: Male
  • My anaconda don't...jk it does
    • View Profile
Re: Nested if statements (C programming)
« Reply #3 on: October 30, 2012, 06:27:14 AM »
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"

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19985
  • LV: 648
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Nested if statements (C programming)
« Reply #4 on: October 30, 2012, 09:41:52 AM »
You may want to use brackets as the are required in C.

Code: [Select]
if (ph > 7 && ph < 12)
Also, the conditions would be

Code: [Select]
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:

Code: [Select]
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      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


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

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

Offline Zexion

  • Addicted to LoL
  • Chaos User
  • ******
  • Posts: 1519
  • LV: 69
  • Gender: Male
  • My anaconda don't...jk it does
    • View Profile
Re: Nested if statements (C programming)
« Reply #5 on: October 30, 2012, 08:33:12 PM »
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:

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19985
  • LV: 648
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Nested if statements (C programming)
« Reply #6 on: October 30, 2012, 09:20:22 PM »
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      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


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

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

Offline Eclipse

  • Awakened Visionist
  • **
  • Posts: 80
  • LV: 1
    • View Profile
Re: Nested if statements (C programming)
« Reply #7 on: January 12, 2013, 07:57:30 AM »
Are you allowed to use switch statements?  They're very nice for situations such as this.

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

Offline Zexion

  • Addicted to LoL
  • Chaos User
  • ******
  • Posts: 1519
  • LV: 69
  • Gender: Male
  • My anaconda don't...jk it does
    • View Profile
Re: Nested if statements (C programming)
« Reply #8 on: January 12, 2013, 01:39:08 PM »
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..

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19985
  • LV: 648
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Nested if statements (C programming)
« Reply #9 on: January 12, 2013, 03:28:56 PM »
HOW DOES SHE DARE GIVE ME ONLY A B FOR MY MASTERPIECE
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


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

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

Offline Zexion

  • Addicted to LoL
  • Chaos User
  • ******
  • Posts: 1519
  • LV: 69
  • Gender: Male
  • My anaconda don't...jk it does
    • View Profile
Re: Nested if statements (C programming)
« Reply #10 on: January 14, 2013, 05:24:00 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.

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19985
  • LV: 648
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Nested if statements (C programming)
« Reply #11 on: January 14, 2013, 10:20:22 AM »
I know, but I achieved my goal in making somebody laugh.
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


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

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