+ 1

Can anyone tell me why this code loops true ?

This code loops true and I want to know why and how to stop it. https://code.sololearn.com/cf6WrHQR8L01/?ref=app

27th Apr 2020, 8:57 PM
Joseph Oritseweyinmi
Joseph Oritseweyinmi - avatar
5 Answers
+ 3
What are you trying to achieve?
27th Apr 2020, 9:27 PM
RKK
RKK - avatar
+ 2
put break statement if (char.IsNumber(e) || e == 1 || e == 0) { Console.WriteLine(true); break; }
27th Apr 2020, 9:50 PM
RKK
RKK - avatar
+ 1
string input = "101101"; char[] convert = input.ToCharArray(); foreach (var e in convert) { if (char.IsNumber(e) || e == 1 || e == 0) { Console.WriteLine(true); } else { Console.WriteLine(false); } } isNumber(Char) indicates whether the specified character passed is number or not? so: e is always either 1 or 0 based on your input( "101101")) if (char.IsNumber(e) || e == 1 || e == 0) since e = 1 and we know 1 is numeric(number) so it's return true. Try to replace if (char.IsNumber(e) || e == 1 || e == 0) with if (e == 1 || e == 0) and see the difference.
27th Apr 2020, 9:01 PM
RKK
RKK - avatar
+ 1
Rkk thanks for your answer, but it returns false, instead of true. I want to remove the loop. I want it to say true once.
27th Apr 2020, 9:22 PM
Joseph Oritseweyinmi
Joseph Oritseweyinmi - avatar
+ 1
I'm trying to stop the foreach loop from looping the output. Because the output always loops.
27th Apr 2020, 9:35 PM
Joseph Oritseweyinmi
Joseph Oritseweyinmi - avatar