PDA

View Full Version : Pseudocode programming


andyhaus1057
Jul 16, 2009, 11:00 AM
This pseudocode contains several nested If-Then-Else statements. It was written without proper alignment and indentation. How would I rewrite the code and use proper conventions of alignment and indentation?

If score < 60 Then
Display “Your grade is F.”
Else
If score <70 Then
Display “Your grade is D.”
Else
If score <80 Then
Display “Your grade is C.”
Else
If score <90 Then
Display “Your grade is B.”
Else
Display “Your grade is A.”
End If
End If
End If
End if

albear
Jul 16, 2009, 11:04 AM
(stick with me for a minute)

What if I scored 65, what would me grade be?

Perito
Jul 16, 2009, 11:29 AM
There are different standards for indenting, and your standards may differ. This is how I would do it, but I know that others would do it differently.

If score < 60 Then
&#160;&#160;&#160;&#160;Display “Your grade is F.”
Else
&#160;&#160;&#160;&#160;If score <70 Then
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Display “Your grade is D.”
&#160;&#160;&#160;&#160;Else
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;If score <80 Then
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Display “Your grade is C.”
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Else
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;If score <90 Then
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Display “Your grade is B.”
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Else
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Display “Your grade is A.”
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;End If
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;End If
&#160;&#160;&#160;&#160;End If
End if

I prefer that IF, ELSE, and END IF line up in the same column for each IF statement. One common variation is to put the ELSE at the end of the previous line similar to how THEN is placed. Some people put THEN on the next line and indent the command additionally.


(stick with me for a minute)

what if i scored 65, what would me grade be?

The first line, "If score < 60" is FALSE so program flow falls through to the else. In the next IF statement, "If score <70", that would be TRUE, so the program would print "Your grade is D". It would then have no further IF statements to process.