IF ELSE IF

#include <stdio.h>
main(){

system("color E8");

float grade;

printf("\n\n\nInput your Grade:");
scanf("%f",&grade);

if(grade>=94.5 && grade<=100)
printf("Your Grade is A!\nOutstanding!\nYou Passed!");

else if(grade>=89.5 && grade<=94.4)
printf("Your Grade is B+\nVery Safisfactory!\nYou Passed!");

else if(grade>=85.5 && grade<=89.4)
printf("Your Grade is B\nSafisfactory\nYou Passed!");

else if(grade>=81.5 && grade<=85.4)
printf("Your Grade is C+\nFairly Safisfactory\nYou Passed!");

else if(grade>=77.5 && grade<=81.4)
printf("Your Grade is C\nSafisfactory\nYou Passed!");

else if(grade>=73.5 && grade<=77.4)
printf("Your Grade is D+\nGrade still good!\nYou Passed!");

else if(grade>=69.5 && grade<=73.4)
printf("Your Grade is D\nYou can Improve this!\nYou Passed!");

else if(grade>64.5 && grade<69.4)
printf("Your Grade is E+\nYou can go Higher than This!\nStill Passed!");

else if(grade>=59.5 && grade<=64.4)
printf("Your Grade is E!\nYou Barely Passed!\nMiracle!");

else if(grade>=0 && grade<=59.4)
printf("Your Grade is F!\nYou didnt make it...\nStudy Hard To Pass!!");

else if(grade<=0)
printf("Less than 0!");

else(grade<=0 || grade>=101)
printf("Not Possible!\n");


getch();
}






Can somebody help me in thisss? Its really hard for me as a Beginner Thank you guyss
Last edited on
?????help you do what?

Aceix.
To Correct the Codes and whatever needed TY
What is wrong with your code?
Take this snippet:

1
2
if(grade>=94.5 && grade<=100)
printf("Your Grade is A!\nOutstanding!\nYou Passed!");


try adding curly braces:

1
2
3
4
if(grade>=94.5 && grade<=100)
{
printf("Your Grade is A!\nOutstanding!\nYou Passed!");
}


And you've got a lot of if's and else if's consider using the switch statement.

http://www.cplusplus.com/doc/tutorial/control/#switch

-EDIT-

oh and it should be:

1
2
3
4
if( (grde>=94.5) && (grade <= 100) )
{
//do something
}
Last edited on
main()
main can only be declared as int main() or int main(int argc, char* argv[]).

system("color E8");
Platform-specific and somewhat dangerous. No need for this call.

However, there's still other problems. I'm not going to tell you explicitly because they are obvious if you know where to look (HINT: Compiler errors)

EDIT: There's also no need to put brackets in your if-else blocks. This is because the block is only one statement. Otherwise, you would need brackets.
Last edited on
Hi Christian0555,

Consider what will happen if grade is 94.45 ?

An else statement cannot have a expression, it is literally:

1
2
3
else {
  // do something
}


That should have produced a compiler error, please always post your compilation errors in full - don't edit anything.

Please use code tags, select all of your code then press the <> button on the right under the format menu.

When using scanf , make use of the value it returns, to see that it worked. Always initialise your variables with something, not necessarily zero. I find some invalid value a good idea - say a negative number in this case - that way when a result is negative it means no new value was assigned to a variable.

@Homberto switch is for const integer values known at compile time, so no good here. But braces are always a good idea, even though they aren't technically needed here, I always do them even when there is one statement.

Hope all goes well. :+)
Last edited on
With if statements, the {} are only needed if you have more than one line to execute. if and for loops execute the very next line (so long as you don't do if(); /* or */ for();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <cstdio>

using namespace std;

int main()
{
	//system("color E8"); // can't find what the heck this does, google sends me german sites
	float grade;
	
	do{
		printf("\nInput your grade: ");
		scanf("%f", &grade);
			
		if ( grade >= 75.5)
			printf("Not bad, but could be better\n");
		else if (grade >= 40.5 && grade <= 75.4)
			printf("Still not bad, but not good either\n");
		else if (grade >= 0 && grade <= 40.4)
			printf("That is bad, but you can try again later\n");
		else//{
			printf("Are you even trying?!\n");
			printf("Should be outputted by else, but with no braces it prints every time\n");
		//}
	}while (grade >= -1);
			
	return 0;
}
Input your grade: 90
Not bad, but could be better
Should be outputted by else, but with no braces it prints every time

Input your grade: 50
Still not bad, but not good either
Should be outputted by else, but with no braces it prints every time

Input your grade: 20
That is bad, but you can try again later
Should be outputted by else, but with no braces it prints every time

Input your grade: -22
Are you even trying?!
Should be outputted by else, but with no braces it prints every time
As TheIdeasMan pointed out, you have a hole in your logic for a grade like 94.45. You can avoid this sort of problem and make your code faster by doing only one comparison:
1
2
3
4
5
6
7
8
if (grade >= 94.5)
printf("Your Grade is A!\nOutstanding!\nYou Passed!");

else if (grade >= 89.5)
printf("Your Grade is B+\nVery Safisfactory!\nYou Passed!");

else if (grade >= 85.5)
printf("Your Grade is B\nSafisfactory\nYou Passed!");

For example, at line 4, you don't need to check if the grade is less than 94.5 because if it wasn't, then the expression at line 1 would be true and line 2 would execute instead of line 4. To make this work, check for a valid grade first:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
if (grade < 0.0)
printf("Less than 0!\n");

else if (grade > 100.0)
printf("Not possible\n");

else if(grade>=94.5)
printf("Your Grade is A!\nOutstanding!\nYou Passed!");

else if(grade>=89.5)
printf("Your Grade is B+\nVery Safisfactory!\nYou Passed!");

else if(grade>=85.5)
printf("Your Grade is B\nSafisfactory\nYou Passed!");

else if(grade>=81.5)
printf("Your Grade is C+\nFairly Safisfactory\nYou Passed!");

else if(grade>=77.5)
printf("Your Grade is C\nSafisfactory\nYou Passed!");

else if(grade>=73.5)
printf("Your Grade is D+\nGrade still good!\nYou Passed!");

else if(grade>=69.54)
printf("Your Grade is D\nYou can Improve this!\nYou Passed!");

else if(grade>64.5)
printf("Your Grade is E+\nYou can go Higher than This!\nStill Passed!");

else if(grade>=59.5)
printf("Your Grade is E!\nYou Barely Passed!\nMiracle!");

else // grade >= 0
printf("Your Grade is F!\nYou didnt make it...\nStudy Hard To Pass!!");


BHX wrote:
With if statements, the {} are only needed if you have more than one line to execute. if and for loops execute the very next line (so long as you don't do if(); /* or */ for();


Even though that is technically correct, it gives the wrong impression.

But it is still IMO a very good idea to use braces anyway, it will save you one day when you add more code later.

Not sure whether this was your point: Your statement reinforces the bad outcome, but the code shows how it can go bad - so there is a bit of conflict there.

IMO, better to make a statement that reinforces the behaviour that you do want, then show code to show how it operates.

I wonder if the OP has cottoned on to what happens when a value is entered that is in between the limits of each else if: 94.45, 89.45, 85.45 etc.

I am also against using namespace std; and do loops. The latter can always be written as a while loop, albeit at the expense of 1 more variable, and that would be less error prone.

Btw, if one needs to do a null statement (rarely) then it should be in braces and commented, here is an excerpt From K&R C programming, which I added the braces & comment:

1
2
3
4
5
6
7
/* strcpy: copy t to s; pointer version 2 */
void strcpy(char *s, char *t)
{
   while ((*s++ = *t++) != '\0') {
              ;  /* null statement */
   }
}



http://zanasi.chem.unisa.it/download/C.pdf


All these things I mention might seem a bit pedantic, but I mention them because they are good practise, and should aid in preventing silly but hard to see / find errors.

Hope all is well :+)
Last edited on
Thanks Guys! Your Answer really Helps :)
@Christian0555
Glad we could help.

@TheIdeasMan
The example isn't conflicted with what I said. I explained that {} wasn't required if you were only doing one statement to be executed and told that it executes the next line then proceeded to show an example that illustrated that as well as the possible error of using more than two statements without braces. Every programming book I have ever looked at uses the exact same approach.

I'm against using namespace std; in any file other than the file that has the main function. Yes, you could use a while loop instead of the do, but they are both equally error prone because you could easily forget to put the toggle statement in the loop to make the control variable false to break out of the loop.

TheIdeasMan wrote:
Btw, if one needs to do a null statement (rarely) then it should be in braces and commented,...

You are approaching this with the wrong mindset. This is the beginner forum with a beginner question. The remark about if(); /* and */ for(); wasn't to illustrate a null statement, but the rather common beginner mistake of doing if(); /* or */ for(); because of always remembering the statement that most books have about lines always ending with a semicolon.

It is fine to have your opinions and preferences on how things should be coded, but it doesn't matter what your preferences is, rather that the beginner adopts their own. For example, my coding style usually gets some comments from other programmers stating "IMO, that makes for poor readability", but I only change my preferences when I join projects (which is rarely now) and then go back to my prefered style after.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// my prefered style
void function()
{

}

class CName{ 
      // members
      public:
            // methods
};

struct SName{
};

int main()
{
      if(){
      }else{
      }

      for(){
      }

      while(){
      }

      do{
      }while();
}
Last edited on
Registered users can post here. Sign in or register to post.