Declaring and Writing a For Loop in Pseudocode

132 Views Asked by At

I am working on writing the pseudocode for a calorie counting program that calculates the total weekly calories for breakfast. the program should allow the user to enter the number of calories consumed each day for 7 days, then report the number of breakfast calories consumed for the entire week.

My professor covered the topic of For Loops briefly, but didn't explain much about how to write them.

How would I write a For loop for the program above, and what would I delcare the variables of calories as?

So far I have a small piece written, but it's short and I don't know where to go with it, or if it needs anything additional.

Start

Declare BreakfastCalories[7] as an Array

For i = 1 to 7

  • Display "Enter breakfast calories dor day" i
  • Enter calories(I)

End For

Set total = day1 + day2 + day3 + day4 + day5 + day6 + day7

Display "Total breakfast calories consumed is", total

End

1

There are 1 best solutions below

2
alejandroMAD On

You seem to have understood well the feel of pseudocode and how it makes for a high-level description of a program's workflow. It should be noted that there's not a unique closed way of writing pseudocode, because these descriptions are mostly intended for humans to read and not for computers to interpret, so you may write your pseudocode with a certain degree of freedom or personal/team guidelines or preferences, provided it is quite readable to other developers.

I suggest this improvement to what you already worked out, so you can think about indexes and dive a little bit closer to what the actual variables and code in your program will finally look like:

initialize breakfastCalories array of size 7
initialize totalCalories to 0

for i = 1 to 7:
  prompt the user to input the calories taken for breakfast on day i
  set breakfastCalories[i] to the user's input
  add breakfastCalories[i] to totalCalories

display "The total calories taken for breakfast this week is: " + totalCalories

Be sure to also check out flowcharts and UML charts like activity diagrams to further improve your analytical skills and help you to code. I hope this helped!