C
C#2y ago
crab2_

can't make a cycle properly

i want to make a cycle that counts the number of loops the program does but it's just infinitely counts numbers from 0 by some reason
15 Replies
Thinker
Thinker2y ago
Did you mean to do count != 1 instead of num != 1? What this code will do is get a long num, then run that loop forever unless num is 1. However you don't update num inside the loop
hengi
hengi2y ago
it's the Collatz conjecture peepoHappy you want to count how many steps it took to get to 1, right?
crab2_
crab2_OP2y ago
exactly
hengi
hengi2y ago
the inner loop will run until the number reaches 1, then the outer loop should immediately break after that? because num == 1, count will always be 1 i think you want a counter variable inside the while loop at the start of the loop, you just increase it by 1, no need for two loops OH it runs infinitely because you did a semicolon after the for parantheses
crab2_
crab2_OP2y ago
yeah but it still doesn't do what i pepecry
hengi
hengi2y ago
yes, because you don't actually need the for loop this will basically just increase count after one whole process of doing the Collatz conjecture on a number. to count the steps, you want to count inside the while loop
crab2_
crab2_OP2y ago
like this?
while (num != 1){
count = count + 1;
}
while (num != 1){
count = count + 1;
}
hengi
hengi2y ago
yes
crab2_
crab2_OP2y ago
and it infinitely counting again
hengi
hengi2y ago
how does your code look now?
crab2_
crab2_OP2y ago
hengi
hengi2y ago
ohh
hengi
hengi2y ago
try this
crab2_
crab2_OP2y ago
still
crab2_
crab2_OP2y ago
nvm fixed @hengi tysm
Want results from more Discord servers?
Add your server