10-17-2004, 02:46 PM | #1 (permalink) |
Pup no More
Location: Voted the Best
|
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 IF StudentAverage > 89 THEN Grade ="A" ENDIF IF StudentAverage > 79 THEN Grade = "B" ENDIF ENDO Thanks
__________________
"If you cannot lift the load off another's back, do not walk away. Try to lighten it." ~ Frank Tyger Last edited by Loup; 10-17-2004 at 02:47 PM.. Reason: Forgot to specify something |
10-17-2004, 04:06 PM | #2 (permalink) |
Upright
|
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? Last edited by TheAgent; 10-17-2004 at 04:08 PM.. |
10-18-2004, 10:50 AM | #5 (permalink) |
Junkie
|
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. |
10-18-2004, 09:27 PM | #7 (permalink) | |
Upright
|
Quote:
always use IF / ELSEIF / ELSE and not SELECT or SWITCH whenever you can! |
|
10-18-2004, 09:56 PM | #8 (permalink) |
Devils Cabana Boy
Location: Central Coast CA
|
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.
__________________
Donate Blood! "Love is not finding the perfect person, but learning to see an imperfect person perfectly." -Sam Keen |
10-20-2004, 03:19 PM | #10 (permalink) |
Upright
Location: Salt Lake City
|
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 |
10-20-2004, 08:11 PM | #11 (permalink) | |
Upright
|
Quote:
|
|
10-20-2004, 10:03 PM | #12 (permalink) | |
Crazy
Location: here and there
|
Quote:
i think Dilbert1234567's solution is the best. |
|
10-21-2004, 09:23 AM | #13 (permalink) | |
Tilted
Location: I am not living.
|
Quote:
__________________
"Hope is for people that don't stand a chance." |
|
10-21-2004, 04:09 PM | #14 (permalink) | |
Upright
Location: Salt Lake City
|
Quote:
|
|
10-21-2004, 09:02 PM | #15 (permalink) |
Junkie
Location: San Francisco
|
I'm new to programming, but I think using an ELSE within the If/End If is the right way to go.
__________________
Embracing the goddess energy within yourselves will bring all of you to a new understanding and valuing of life. A vision that inspires you to live and love on planet Earth. Like a priceless jewel buried in dark layers of soil and stone, Earth radiates her brilliant beauty into the caverns of space and time. Perhaps you are aware of those who watch over your home And experience of this place to visit and play with reality. You are becoming aware of yourself as a gamemaster... --Acknowledge your weaknesses-- |
10-21-2004, 10:36 PM | #16 (permalink) | |
Crazy
Location: here and there
|
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.
__________________
# chmod 111 /bin/Laden Last edited by theFez; 10-21-2004 at 10:38 PM.. Reason: clarity |
|
Tags |
if or then, pseudocode, statements |
|
|