+ 1

Can anyone help me with this code? Why count outputs 3? In the if condition we decrease it with one right?

public class Fragments { public static void main(String[] args) { int i = 4, j = 5, k = 6, n = 7; System.out.println(i + j * k - k % n); boolean found = false; int count = 3; if (!found || --count == 0) System.out.print("found "); System.out.println(count); }}

1st Nov 2021, 4:43 PM
Maryam Magdy
Maryam Magdy - avatar
4 Réponses
+ 1
Maryam Magdy Because !found = !false = true So if you use or operator ( || ) and first condition get true then second condition will not be check so Count will be 3 But if !found = false then second condition will be check and count will be 2
1st Nov 2021, 5:02 PM
A͢J
A͢J - avatar
+ 2
You can't really decrement in the If. Decrement usually used in some sort of loop
1st Nov 2021, 4:53 PM
Iyvotvu
Iyvotvu - avatar
+ 2
!found === true on if statement (U can't change value of count inside if(...)) --count === 2 and --count != 0 Since u have use 'or' operator which means true || true // true false || true // true true || false // true false || false //false Here !found === true so if statement occurs
1st Nov 2021, 4:54 PM
Pariket Thakur
Pariket Thakur - avatar
+ 2
1st Nov 2021, 5:11 PM
Maryam Magdy
Maryam Magdy - avatar