7/8/2023 0 Comments Pseudocode writer![]() This line will print our goodbye message to the user, so we’ll show that in our output and move to the end of the main() procedure: Since this is an if-else statement, and the Boolean expression was true, that means we can just skip the second block of statements and go straight down to the first line of code after the if-else statement, as shown here: This line will print a message to the user that the number provided as input was even, so we’ll display that in our output:Īt this point, we’ve reached the end of that block of statements, so we need to figure out where to go next. Since the Boolean expression is true, then our program will move to the first block of statements inside the if-else statement, and start executing that code: So, this expression is really the Boolean expression 0 = 0, which is true: Since $2$ goes evenly into $16$ exactly $8$ times, with no remainder, we’ll get the value $0$. Recall that the modulo operation will find the remainder of dividing the first number by the second number. In this case, our expression is x MOD 2 = 0, so we need to start by evaluating x MOD 2. So, the first step is to evaluate the Boolean expression in parentheses and determine if it is either true or false. In that case, we’ll store the number value $16$ in the variable x as shown in this code trace:Īt this point, we’ve reached the first part of our if-else statement. In this example, let’s assume the user inputs the string "16". On this line, we’ll read input from the user, convert it to a number, and store it in the variable named x. This first line will simply print a prompt for input to the user, so we’ll add that to the output and move to the next line as shown below: ![]() So, we’ll jump inside the main() procedure, and start running the code that is present there: ![]() Just like always, our “mental model” of a computer will first skim through the code to find all of the procedures, and then it will reach the call to the main() procedure at the bottom of the code as shown here: ![]() As always, we’ll start our code trace as shown below: Let’s run trace through this program a couple of times to see how an if-else statement works in pseudocode. ![]() This program will accept input from the user, and then determine if the user has input an even or odd number using the modulo operator. ![]()
0 Comments
Leave a Reply. |