Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 10-17-2004, 02:46 PM   #1 (permalink)
Pup no More
 
Loup's Avatar
 
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
So if StudentAverage = 92, will it assign Grade = "A", then go to the endo, or will it also assign Grade = "B" becasue 92 is also > 79?

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
Loup is offline  
Old 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..
TheAgent is offline  
Old 10-17-2004, 09:26 PM   #3 (permalink)
Crazy
 
Location: California
Or, if the language supports it, you could use a switch statement.
bingle is offline  
Old 10-17-2004, 10:21 PM   #4 (permalink)
Tilted
 
Location: I am not living.
Or you could use an array, and just do something like:

grade = lettergrades[StudentAverage/10];
__________________
"Hope is for people that don't stand a chance."
Mavric98 is offline  
Old 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.
SinisterMotives is offline  
Old 10-18-2004, 05:58 PM   #6 (permalink)
Junkie
 
Depending on the language you can use a SELECT statement too.
vanblah is offline  
Old 10-18-2004, 09:27 PM   #7 (permalink)
Upright
 
Quote:
Originally Posted by vanblah
Depending on the language you can use a SELECT statement too.
SELECT is the same as switch, which is a poor programming construct, and should not be used for great than / less than (equals is ok, but its still a bad idea)...

always use IF / ELSEIF / ELSE and not SELECT or SWITCH whenever you can!
TheAgent is offline  
Old 10-18-2004, 09:56 PM   #8 (permalink)
Devils Cabana Boy
 
Dilbert1234567's Avatar
 
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
Dilbert1234567 is offline  
Old 10-19-2004, 06:10 AM   #9 (permalink)
Junkie
 
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.
vanblah is offline  
Old 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
DewMan is offline  
Old 10-20-2004, 08:11 PM   #11 (permalink)
Upright
 
Quote:
Originally Posted by DewMan
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
exactly... that was one of the options i originally gave...
TheAgent is offline  
Old 10-20-2004, 10:03 PM   #12 (permalink)
Crazy
 
Location: here and there
Quote:
Originally Posted by DewMan
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
its simple, but bad programming. if you have a high proportion of good students and/or hit the loop a large number of times you are reassigning your variables 3-4 times per loop. plus forcing the evaluation of the if statement every loop. at least with an if/else loop it drops out of the evaluation once it hits a match.

i think Dilbert1234567's solution is the best.
theFez is offline  
Old 10-21-2004, 09:23 AM   #13 (permalink)
Tilted
 
Location: I am not living.
Quote:
Originally Posted by DewMan
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
This is extremely bad programming because you are testing score 4 times no matter what even when at times it would only need to be tested once.
__________________
"Hope is for people that don't stand a chance."
Mavric98 is offline  
Old 10-21-2004, 04:09 PM   #14 (permalink)
Upright
 
Location: Salt Lake City
Quote:
Originally Posted by Mavric98
This is extremely bad programming because you are testing score 4 times no matter what even when at times it would only need to be tested once.
True. I didn't say it was efficient. People were giving responses using constructs that weren't used in the pseudocode. I gave him an option that didn't require anything more than what posted already.
DewMan is offline  
Old 10-21-2004, 09:02 PM   #15 (permalink)
Junkie
 
-Ever-'s Avatar
 
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--
-Ever- is offline  
Old 10-21-2004, 10:36 PM   #16 (permalink)
Crazy
 
Location: here and there
Quote:
Originally Posted by -Ever-
I'm new to programming, but I think using an ELSE within the If/End If is the right way to go.
its a better way to go for sure. the problem with the sequence of
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
theFez is offline  
 

Tags
if or then, pseudocode, statements


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 10:25 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360