while loop question

i'm new to swift programming and was hoping to utilize a conditional && for a while loop but it almost seems like i need to break it out and have a nested while loop?


what i'm trying to do:

------------------

counter1 = 0

counter2 = 0


while (counter1<0) && (counter2<0)

-----------------


that way i can use common code, otherwise it seems like i need to have 2 nested while loops and possibly duplicate code for each loop?


Thanks

It does work !


What error do you get ?


You need to write :


while (counter1<0) && (counter2<0) {


Didn't you forget the open bracket after while ?


With the initial conditions you set (both counters = 0), you will not execute anything inside the loop.


If you want to execute at least once, use repeat while :


repeat {
    print("I did it once at least", counter1, counter2)
     counter1 += 1
     counter2 += 2
} while (counter1<0) && (counter2<0)

It would help if you:

  • Posted a slightly larger snippet of code, because it’s very hard to tell what you’re trying to do from the code you did post

  • Put your code in code block (using the

    <>
    button in the editor), because that makes it easier to read and easier to reference by line number

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
while loop question
 
 
Q