https://fopp.umsi.education/books/published/fopp/GeneralIntro/MoreAboutPrograms.html#preview-of-control-structures

Hello, I was going through preview of control structures (Article : 1.5.1). While running the conditionals for example: if, continue, else ; I am getting output “adding the item to your list”. Although I followed the previous instructions to add items. Please help me to understand the IF, CONTINUE and ELSE conditionals

Here’s the output:

adding the item to the list
adding the item to the list
Here is our grocery list:
[‘flour, olive oil’, ‘mustard oil’]

Hi,

I am not quite sure what you need help with. If you are trying to get a deeper understanding of control structure, that will probably come at a later chapter. I think the goal of this “Preview” section is to literally preview this concept / feature. It gives you a taste of what’s possible. I don’t think you are expected to have a full understanding of it.

If you do want to get a deeper understanding, that’s awesome too! You can probably look ahead in the text (as long as you don’t get too far and too deep).

Essentially, what is happening here is this:

  1. The while loop continuously prompts the user to enter a grocery item.
  2. The user adds items, one by one. Individually, they get temporarily stored in the grocery_item variable and then added to the grocery_list list variable.
  3. done is kind of like an escape word. When the user types it in, the while loop’s condition evaluates to False and the loop exits. No more items will be added to the list.

Within the loop, if / else work as follows:

  1. The if condition is evaluated. If the user’s entry wasn’t the word done, then the else part is executed. The item is added to the list.
  2. If the user’s entry was the word done, the if part does nothing. (The continue statement within if just does nothing and allows the program flow to continue.) The while loop goes to the next iteration. Since the word done violates the while loop’s condition, the while loop terminates.
1 Like

Thank you so much for your valuable answer!. Yes I was trying to understand what was happening behind the active code and how. It would be so much helpful for me if I get a deeper understanding on IF, CONTINUE and ELSE .

I just added that part to my previous reply. :wink::grinning:

Thank you so much for your support! :relaxed: :smiley: