While Loop question: not sure if there is anything I missed here

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.

Hello,

I don't think that the issue is in the code that you've posted. Somewhere, you are using ..< or ... (At least I believe so because of the error) and I don't see a use here.

Would you be willing to post isPrime? The issue is likely to be there.

1 Like

oh, dumb me. Thank you Griotspeak. I just checked my 'isPrime' extension. I started a for loop from 2 not 1. So, my first check is 'for num in 2..<self' giving me this error because 'self' has value of 1.

Thank you. I need a cup of coffee.

2 Likes
Terms of Service

Privacy Policy

Cookie Policy