Search code examples
javaloopsif-statementcomputer-sciencedo-while

Understanding Do While Loop with nested if else statement


My professor made us do this in class and I am super confused looking at those outputs. Does this(x + " " + y + " " + z) mean add all 3 variables if (y > z) ? The output doesn't make any sense to me at all.

public class Practice
    {
        public static void main (String[] args)
        {

            int x=10, y=11, z=3;
            do
            {
                System.out.println(x + " " + y + " " + z);
                if (y > z)
                {
                    y = y-5;
                }
                else
                {
                    y = y + 3;
                }
                x = x - 2;
            } while (x > 0);
            System.out.println("Bye");
        }
    }
OUTPUT:
10 11 3
8 6 3
6 1 3
4 4 3
2 -1 3
Bye

Solution

  • System.out.println(x + " " + y + " " + z);
    

    This is not adding x,y and z. Its just appending values in string for printing your numbers are converted to strings