Skip to main content
Question

Creating a Loop - Problem: Infinite Loop - Loop not breaking

  • 12 June 2024
  • 1 reply
  • 13 views

I'm working on a carbon calculator using Typeform to ask people a set of questions about Housing, Transport, and more. I need to create a loop based on the answers to previous questions. For example, if a user indicates they have 3 cars, the question group with all the car-related questions should loop 3 times before moving to the next question. If the user has 0 cars, the loop will not execute, and it will jump to next question skipping the question group with all the car-related questions. (see the attached image for the logic).

Problem Description

Question 11: Asks the user how many cars they have.

Question 12: A question group with 4 sub-questions about cars (12.a, 12.b, 12.c, 12.d).

What Works: If the user inputs 0, it correctly directs them to question 13.

Where I Need Help: If the user inputs any number greater than 0, it enters an infinite loop and doesn’t break. Here is the logic I have used:

  1. I created a NUMBER variable called 'car_count' and initially assigned it the value '0'.
  2. If the user has more than 0 cars, then it should assign the value of 'cars' (answer of Question 11)  to 'car_count'.
    LOGIC USED ON QUESTION 11.
  3. If 'cars' is equal to 0, go to Question 13. For all other cases, go to Question Group 12.
  4. At the end of Question Group 12, specifically in 12.d, I have the following logic set:
    1. Always subtract 1 from 'car_count'.
    2. If 'car_count' is greater than 0, go to 12.
    3. If 'car_count' is equal to 0, go to 13, which should ideally break the loop but instead goes back to 12.
Logics Used On Question 12.d

 

Logic Map

I'm not sure what I'm doing wrong here. I would be really grateful if someone could help me with this.

1 reply

Userlevel 7
Badge +6

@nidishmurugan - the problem is that when you jump back to the same question, you replace the answers, so car 2 will replace car 1 etc.. 

better solution is to create a question group and figure out how many/max number of cars that you will accept  - ie allow your car_count to have a max of 10 and then have the question group for cars start with the counter at max  10(or whatever you choose)  and answer the questions for the first car . then put in place a logic rule that reduces the car_count by 1 and if it is NOT 0, go to the next questions for car 2. do the same until the counter gets down to 0 then exit. 

if the counter goes to 0 at any time, exit

i know it’s a bit of a set up to start with but this way you will not lose values in looping. 

 

des

Reply