Convert boolean inside if statement
Hello, I am working on a program where I am using a particular logic for a flag, I am declaring a boolean variable, and then making an if statement that executes if the statement and then add value to an array, but I want to alternatively add values to my array. EG I have 1001 I want to add 1 in array1, 0 in both arrays, 0 in both arrays, 1 in array1. This is how I have written this in code. But the problem is that the boolean is always true, it doesn't get flase even after the if statement has executed. This is my code:
14 Replies
⌛
This post has been reserved for your question.
Hey @. 𝚂𝚎𝚛𝚊𝚙𝚑𝚒𝚗𝚊 ~ 👠! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
What is your reversedBinaryValue? Would like to test it
Its any binary value, 0s and 1s
Say, 100101
Then x will be 100001 and y will be 000100
Yeah, but is it in like a List or something? Since you are using .size()
Oh, its an arraylist
You need to declare
boolean addToSplitOne = true;
before the beginning of the loop
Otherwise you are setting it to true
in each iterationBefore the beginning of the for loop?
yes
Awesome, thank you. I just woke up, I will check as soon as I am on my PC!
That works thank you! But is there any chance you can explain why that didnt work? I would assume that it was due to the block level but it was still a higher block than where it was changing.
that here
ah wrong one
that
loops work by executing one instruction after another
so if the
boolean addToSplitOne = true;
is inside the loop, addToSplitOne = true
will be executed in each iterationAh, that makes sense.
So basically no matter what, all the control variables must always be declared outside the loops
Along with all the flags
depends
If you want to keep the values outside of iterations, you can do that
That does quite make sense
Thank you very much for your help, I will be closing the thread now!
Post Closed
This post has been closed by <@369486413890060289>.