Chaos Project

Game Development => General Discussion => Topic started by: Zexion on October 29, 2012, 07:45:05 pm

Title: Nested if statements (C programming)
Post by: Zexion on October 29, 2012, 07:45:05 pm
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.
Title: Re: Nested if statements (C programming)
Post by: KK20 on October 29, 2012, 08:42:56 pm
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?
Title: Re: Nested if statements (C programming)
Post by: Ryex on October 29, 2012, 09:15:56 pm
^^ 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.
Title: Re: Nested if statements (C programming)
Post by: Zexion on October 29, 2012, 11:27:14 pm
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"
Title: Re: Nested if statements (C programming)
Post by: Blizzard on October 30, 2012, 02:41:52 am
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");
}

Title: Re: Nested if statements (C programming)
Post by: Zexion on October 30, 2012, 01: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:
Title: Re: Nested if statements (C programming)
Post by: Blizzard on October 30, 2012, 02: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.
Title: Re: Nested if statements (C programming)
Post by: DarknessSurrounds on January 12, 2013, 12: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
Title: Re: Nested if statements (C programming)
Post by: Zexion on January 12, 2013, 06:39:08 am
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..
Title: Re: Nested if statements (C programming)
Post by: Blizzard on January 12, 2013, 08:28:56 am
HOW DOES SHE DARE GIVE ME ONLY A B FOR MY MASTERPIECE
Title: Re: Nested if statements (C programming)
Post by: Zexion on January 13, 2013, 10:24:00 pm
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.
Title: Re: Nested if statements (C programming)
Post by: Blizzard on January 14, 2013, 03:20:22 am
I know, but I achieved my goal in making somebody laugh.