Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   IF/THEN Statements (Pseudocode) (https://thetfp.com/tfp/tilted-technology/72937-if-then-statements-pseudocode.html)

Loup 10-17-2004 02:46 PM

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

TheAgent 10-17-2004 04:06 PM

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? ;)

bingle 10-17-2004 09:26 PM

Or, if the language supports it, you could use a switch statement.

Mavric98 10-17-2004 10:21 PM

Or you could use an array, and just do something like:

grade = lettergrades[StudentAverage/10];

SinisterMotives 10-18-2004 10:50 AM

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.

vanblah 10-18-2004 05:58 PM

Depending on the language you can use a SELECT statement too.

TheAgent 10-18-2004 09:27 PM

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!

Dilbert1234567 10-18-2004 09:56 PM

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.

vanblah 10-19-2004 06:10 AM

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.

DewMan 10-20-2004 03:19 PM

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

TheAgent 10-20-2004 08:11 PM

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... :)

theFez 10-20-2004 10:03 PM

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.

Mavric98 10-21-2004 09:23 AM

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.

DewMan 10-21-2004 04:09 PM

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.

-Ever- 10-21-2004 09:02 PM

I'm new to programming, but I think using an ELSE within the If/End If is the right way to go.

theFez 10-21-2004 10:36 PM

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.


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


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