Getting Started with Pseudocode2:44 with Reggie Williams
Get started with pseudocode. Learn some basic conventions for describing a program with input, output, and variables.
prompt user for number set num1 to user_input prompt user for number set num2 to user_input set sum to num1 + num2 print sum
Using a conditional statement
prompt user for number if input is not a number print "input is not a number" prompt user for new number endif set num1 to user_input prompt user for number if input is not a number print "input is not a number" prompt user for new number endif set num2 to user_input set sum to num1 + num2 print sum
if input is not a number print "this is not a number" else if input is odd print "the number is odd" else the input is even print "the number is even" endif
Pseudo code expresses code logic in procedures using plain verbs and 0:00 nouns from spoken language. 0:04 When you write pseudo code, you explain each step in the program. 0:05 Think of it kinda like a recipe. 0:09 A program usually has some data, or ingredients, 0:10 that are acted upon just like you peel, mix, whisk, eat and bake while cooking. 0:12 A recipe lists the exact order in which to perform each step. 0:18 If you bake, for 0:21 example, before you mix the batter, your cake won't turn out right. 0:22 You can write pseudo code on paper with a pen, or open up a text editor and 0:26 use code comments to describe your program's logic in pseudo code. 0:30 Let me walk you through an example. 0:34 For any programming project, we usually start with a set of requirements. 0:36 These requirements, like a set of instructions, 0:40 tell you how the program should perform. 0:42 They'll often want some input, like retrieving two numbers from a user and 0:44 some kind of output like the result of adding those two numbers together. 0:48 You can use those requirements to guide the design of your program and 0:52 the pseudo code you write. 0:55 In this example, we'll take a simple specification and 0:57 convert it into programming steps using pseudo code. 1:00 If you'd like to follow along, open the work space associated with this video, and 1:03 the file print underscore sum dot txt. 1:07 [SOUND] Get two integer values from a user and output the sum of those numbers. 1:09 In other words, get two numbers, add them together and output the results. 1:16 The requirement is simple, but 1:22 a program would need to perform several steps to complete the task. 1:23 First, you need to prompt the user to type a number. 1:27 That's one step. 1:30 Then you create a variable to store the input. 1:32 Prompt for another number, store that number in a variable named num2. 1:36 Create another variable with the sum of the two values. 1:42 And finally, print the value inside the sum variable. 1:46 That's an example of pseudo code. 1:49 It reads easily and 1:51 could be used by a programmer coding in any programming language. 1:52 This example is simple, but 1:56 you can use pseudo code to describe any programming concept. 1:58 For example, here's how you could add a conditional statement to make sure 2:01 the input from the user was a number and not a letter. 2:04 Here, I've added conditional statements to check and see if the input is a number. 2:07 Notice that I've added indentation to show the code that runs 2:10 inside the conditional statement. 2:14 In other words, if that condition is true, 2:16 then the steps that are indented would happen. 2:18 To mark the end of a conditional statement add endif. 2:21 This makes clear where the condition ends and the rest of the program continues. 2:24 You can easily create conditional statements with multiple conditions too. 2:28 Pseudo code is simple and straightforward. 2:32 You can easily see the logic and you never get a syntax error. 2:35 In the next video, I'll show you pseudo code for 2:38 describing other programming concepts like loops and functions. 2:40
You need to sign up for Treehouse in order to download course files.Sign up