Hello
I am trying to find a prime number at a certain position. I also extended Int type to check if number is Prime and it is good there.
Could someone help me explain why Xcode Playground keep throwing error for my while loop below?
var stopLoopCondition:Int = 6;
var counterLoop:Int = 1;
var numberToCheck:Int = 1;
var myFinalPrime:Int = 1;
while counterLoop <= stopLoopCondition {
if (numberToCheck.isPrime) {
myFinalPrime = numberToCheck;
counterLoop = counterLoop + 1;
}
numberToCheck += 1;
}
print(myFinalPrime);
Error in Xcode: 'Fatal error: Can't form Range with upperBound < lowerBound'
If I move the statement 'numberToCheck += 1' before If statement. The code is good and compiler run well. However, when I put this statement below if statement, the compiler doesn't run and gives me error. I don't understand why because I initiated value for 'numberToCheck' and I don't use this value in loop condition.
Thank you for your time.