![]() |
IF/THEN Statements (Pseudocode)
If you have an IF statement and the result is true and you endif, does it automatically go to the end of the loop?
Example: Code:
DO WHILE StudentNumber <> 99999 Thanks |
it will also assign a B...
you need an IF / ELSEIF / ENDIF ;) or another qualifier on the "B" part... for instance, Average > 79 AND Average < 90... or... just reverse the order... start with "if Av > 60 then grade = 'D'" and work your way up to "if Av > 89 then grade = 'A'" you follow? ;) |
Or, if the language supports it, you could use a switch statement.
|
Or you could use an array, and just do something like:
grade = lettergrades[StudentAverage/10]; |
An "endif" won't automatically break out of the "do while" loop, if that's what you're asking. The outer loop will only be broken if the loop's condition is met or if you use an explicit "break" statement.
Also, intead of individual "if...endif" blocks for each test, you should use an "if...elseif...elseif...else" ladder. A "switch" is generally used to test for equivalence, and not for greater-than and less-than comparisons, although it can be kludged in various ways depending on the language. |
Depending on the language you can use a SELECT statement too.
|
Quote:
always use IF / ELSEIF / ELSE and not SELECT or SWITCH whenever you can! |
use a select case its easier
select case grade case 100 to 90 'a case 80 to 90 'b case 70 to 80 'c ... end select its much esier this way. |
I agree that in some situations SELECT is poor programming ... but in a simple application like this one I find that it can be more organized. Such as the way Dilbert1234567 suggested.
|
The simplest thing would be to reverse your logic.
Check for the lowest value first, and the highest value last. Your value will get updated at each intermediate step that the check is valid. if (score > 60) grade = d if(score > 70) grade = c if(score > 80) grade = b if(score > 90) grade = a |
Quote:
|
Quote:
i think Dilbert1234567's solution is the best. |
Quote:
|
Quote:
|
I'm new to programming, but I think using an ELSE within the If/End If is the right way to go.
|
Quote:
if statements is that each and every if has to be evaluated, then the variable is assigned, and the next if is evaluated. if the evaluation fails, the variable is not set, but even if it succeeds the next if statement is evaluated. with an if/else with two conditions in the if statement (ie if(grade <= 100 && grade >= 90){ return 'A'; } elseif(grade < 90 && grade >= 80){ return 'B'; } with most compilers and interpreters the if will stop the evaluation on the first fail. It knows that if the first condition fails, an AND is impossible so it kicks out. In a sequence of if/else statements, once one matches, it kicks out of the sequence. a compiler does a good job of optimizing code if you let it. |
All times are GMT -8. The time now is 09:24 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project