Pseudocode for Loops2:18 with Reggie Williams
Expand your pseudocode vocabulary with some simple conventions for describing loops.
Pseudocode for loops
retrieve student_grades set grades to student_grades set grade_total to 0 for each grade in grades if grade is not a 1, 2, 3, or 4 print "invalid grade" print grade print "can't complete calculation" stop else add grade to grade_total endif endfor set gpa to grade_total / number of grades print gpa
You can describe any programming concept with pseudo code. 0:00 I'll show you how to describe two programming control structures, loops and 0:03 functions, using easy-to-understand language. 0:07 I'll start with loops in this video and continue with functions in the next. 0:09 If you want to follow along, open the workspace associated with this video and 0:13 the file named gpa.txt. 0:17 Given a list of student grades, calculate their GPA. 0:22 The grading scale used should be 1 to 4. 0:24 If any grade isn't a 1, 2, 3, or 4, print a message and stop the program. 0:27 Say you're creating a GPA calculator that takes a student's grades and 0:34 determines their overall grade point average. 0:37 First, we need to get the students grades. 0:40 You might not know exactly how that's going to be performed. 0:43 Maybe those grades are manually input or 0:46 they're retrieved from a database or they exist somewhere else. 0:47 The nice thing about pseudo code is that it can describe just what needs to happen, 0:51 not exactly how. 0:55 When you sit down to program, you'll need to figure out that part. 0:57 But when you are first working through the logic, you could just write retrieve 1:00 student_grades to capture what you'll need to program eventually. 1:04 You also need a variable to store the total value of the grades, 1:09 set grade_total to 0 does that. 1:12 To get the total value for the grades, you should add them all up. 1:15 Looping through the collection of grades one at a time will do the trick. 1:19 So for each grade in the grades collection, 1:23 the program will add that grade to the grade_total variable. 1:25 The requirements say we need to check if the grade is a valid number. 1:29 So I'll add a conditional statement. 1:32 Notice that I've indented the conditional statement because it's inside the loop. 1:34 I'll also indent again inside the conditional statement, 1:39 to show these steps take place when the grade isn't a valid number, 1:41 print out a few messages, then stop the loop. 1:45 If the grade is valid, then it's added to the grade_total. 1:47 Once the loop is done, there's just two more steps. 1:53 Calculate the grade point average by dividing the grade_total 1:56 by the number of grades, then print out the gpa. 1:59 This pseudo code seems to match all of the requirements for the project. 2:03 In reviewing it, 2:06 I realize that this program would probably work better as a function, 2:07 a collection of programming steps that you can reuse over and over again. 2:10 In the next video, I'll convert this example to use a function. 2:14
You need to sign up for Treehouse in order to download course files.Sign up