1 00:00:00,830 --> 00:00:03,760 You can describe any programming concept with pseudo code. 2 00:00:03,760 --> 00:00:07,140 I'll show you how to describe two programming control structures, loops and 3 00:00:07,140 --> 00:00:09,740 functions, using easy-to-understand language. 4 00:00:09,740 --> 00:00:13,710 I'll start with loops in this video and continue with functions in the next. 5 00:00:13,710 --> 00:00:17,919 If you want to follow along, open the workspace associated with this video and 6 00:00:17,919 --> 00:00:19,338 the file named gpa.txt. 7 00:00:22,335 --> 00:00:24,997 Given a list of student grades, calculate their GPA. 8 00:00:24,997 --> 00:00:27,705 The grading scale used should be 1 to 4. 9 00:00:27,705 --> 00:00:34,120 If any grade isn't a 1, 2, 3, or 4, print a message and stop the program. 10 00:00:34,120 --> 00:00:37,550 Say you're creating a GPA calculator that takes a student's grades and 11 00:00:37,550 --> 00:00:39,360 determines their overall grade point average. 12 00:00:40,910 --> 00:00:43,580 First, we need to get the students grades. 13 00:00:43,580 --> 00:00:46,290 You might not know exactly how that's going to be performed. 14 00:00:46,290 --> 00:00:47,790 Maybe those grades are manually input or 15 00:00:47,790 --> 00:00:50,760 they're retrieved from a database or they exist somewhere else. 16 00:00:51,900 --> 00:00:55,680 The nice thing about pseudo code is that it can describe just what needs to happen, 17 00:00:55,680 --> 00:00:57,240 not exactly how. 18 00:00:57,240 --> 00:01:00,660 When you sit down to program, you'll need to figure out that part. 19 00:01:00,660 --> 00:01:04,288 But when you are first working through the logic, you could just write retrieve 20 00:01:04,288 --> 00:01:07,550 student_grades to capture what you'll need to program eventually. 21 00:01:09,440 --> 00:01:12,943 You also need a variable to store the total value of the grades, 22 00:01:12,943 --> 00:01:15,690 set grade_total to 0 does that. 23 00:01:15,690 --> 00:01:19,340 To get the total value for the grades, you should add them all up. 24 00:01:19,340 --> 00:01:22,200 Looping through the collection of grades one at a time will do the trick. 25 00:01:23,390 --> 00:01:25,340 So for each grade in the grades collection, 26 00:01:25,340 --> 00:01:29,170 the program will add that grade to the grade_total variable. 27 00:01:29,170 --> 00:01:32,410 The requirements say we need to check if the grade is a valid number. 28 00:01:32,410 --> 00:01:33,810 So I'll add a conditional statement. 29 00:01:34,850 --> 00:01:39,110 Notice that I've indented the conditional statement because it's inside the loop. 30 00:01:39,110 --> 00:01:41,950 I'll also indent again inside the conditional statement, 31 00:01:41,950 --> 00:01:45,230 to show these steps take place when the grade isn't a valid number, 32 00:01:45,230 --> 00:01:47,990 print out a few messages, then stop the loop. 33 00:01:47,990 --> 00:01:50,850 If the grade is valid, then it's added to the grade_total. 34 00:01:53,055 --> 00:01:56,270 Once the loop is done, there's just two more steps. 35 00:01:56,270 --> 00:01:59,390 Calculate the grade point average by dividing the grade_total 36 00:01:59,390 --> 00:02:02,250 by the number of grades, then print out the gpa. 37 00:02:03,410 --> 00:02:06,820 This pseudo code seems to match all of the requirements for the project. 38 00:02:06,820 --> 00:02:07,690 In reviewing it, 39 00:02:07,690 --> 00:02:10,490 I realize that this program would probably work better as a function, 40 00:02:10,490 --> 00:02:14,630 a collection of programming steps that you can reuse over and over again. 41 00:02:14,630 --> 00:02:17,660 In the next video, I'll convert this example to use a function.