-3

How can I get to the same result but using the while operator instead? Also, printing which ones are even and which ones are odd

var arrayNumber: Array = [1,2,3,4,5,6,7,8,9,10]

for myInt: Int in arrayNumber {
    if(myInt % 2 == 0) {
        print("\(arrayNumber) is even number")
    } else {
        print("\(myInt) is odd number")
    }
}
Yan
  • 7
  • 5
  • Can you clarify your question? `while` what? Do you want to stop at the first odd number? Print odd/even for all numbers (in which case a `for` is more appropriate) – Paulw11 Feb 03 '22 at 01:32
  • Using the while operator, to do the same functionality as a "for in loop" would but using while instead and to return the output for which numbers are even and which are odd – Yan Feb 03 '22 at 02:13
  • Does this answer your question? [How to know if a number is odd or even in Swift?](https://stackoverflow.com/questions/24056985/how-to-know-if-a-number-is-odd-or-even-in-swift) – pkamb Feb 03 '22 at 02:40
  • Hey @pkamb, thanks for trying to help, someone has answered but for some reason deleted his code, but unfortunately it does not answer my question because the while operator hasn't been used, it needs to do the same functionality as in the link above but using the operator while instead, and to print which ones are odd and which ones are even – Yan Feb 03 '22 at 02:52

1 Answers1

1

The best thing to do is use an iterator for the while loop. it.next() will return nil if there are no more elements in the array.

let arrayNumber = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var it = arrayNumber.makeIterator()
while let next = it.next() {
    if next % 2 == 0 {
        print("even", next)
    } else {
        print("odd", next)
    }
}
Rob C
  • 4,877
  • 1
  • 11
  • 24
  • Thank you Rob, that's a great way to tackle it, but I need it to return the odd numbers as well instead of a nil value – Yan Feb 03 '22 at 02:22
  • @YanesSkrabe What do you mean you need to return odd numbers? You never mentioned returning. All you are doing above is printing. Do you mean you need to print odd numbers? – Rob C Feb 03 '22 at 02:25
  • Yes, that's right, to print the odd numbers, someone has answered but I suppose that the author deleted the following : var arrayNumber = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] var index = 0 while index < arrayNumber.count { var num = arrayNumber[index] if (num % 2 == 0) { print("\(num) is an even number") } else { print("\(num) is an odd number") } index += 1 } – Yan Feb 03 '22 at 02:29
  • I bet you could have pieced that together yourself ;) Anyways, answer updated. – Rob C Feb 03 '22 at 02:36
  • I appreciate your help! – Yan Feb 03 '22 at 02:57
  • @YanesSkrabe better to use `BinaryInteger` `isMultiple(of:)` method. – Leo Dabus Feb 03 '22 at 03:13
  • @LeoDabus Thanks, I didn't know that method existed. But how is it better than using the modulo operator. – Rob C Feb 03 '22 at 03:22
  • Better readability. It might also be more efficient but I haven't checked its implementation yet. You can read about the [evolution proposal](https://github.com/apple/swift-evolution/blob/main/proposals/0225-binaryinteger-iseven-isodd-ismultiple.md) – Leo Dabus Feb 03 '22 at 03:26
  • There is some conditions check before returning the modulo operation – Leo Dabus Feb 03 '22 at 03:32
  • `@inlinable public func isMultiple(of other: Self) -> Bool {` `// Nothing but zero is a multiple of zero.` `if other == 0 { return self == 0 }` `// Special case to avoid overflow on .min / -1 for signed types.` `if Self.isSigned && other == -1 { return true }` `// Having handled those special cases, this is safe.` `return self % other == 0` `}` – Leo Dabus Feb 03 '22 at 03:34