+ 17

Can someone please explain how 6 is the output?

public class Program { public static void main(String[] args) { int a = 0; do { a+=2; } while (a<5); System.out.println(a); } }

6th Sep 2020, 1:15 PM
Kanyinsola Ogunwale
Kanyinsola Ogunwale - avatar
4 Answers
+ 5
The loop is done 3 times
6th Sep 2020, 2:06 PM
Rafael Bergmann
Rafael Bergmann - avatar
+ 13
First loop, a = 0 Then increased by 2 a = 2 Checking for condition: a < 5 => 2 < 5 ✅ Second loop, a = 2 Incrementing by 2 a = 4 Checking for condition: a < 5 => 4 < 5 ✅ Third loop, a = 4 Incrementing by 2 a = 6 Checking for condition: a < 5 => 6 < 5 ❌ Loop breaks Final value of a = 6 🙌
6th Sep 2020, 1:19 PM
Namit Jain
Namit Jain - avatar
+ 9
Thank you
6th Sep 2020, 1:22 PM
Kanyinsola Ogunwale
Kanyinsola Ogunwale - avatar
+ 2
thank you
11th Sep 2020, 4:33 AM
kallam ganga
kallam ganga - avatar