Computer Science, asked by anindyaadhikari13, 2 months ago

Write the output of the following. Explain your answer.

public class abc {
public static void main(String[] args) {
int c=3, d=4;
if(c++==d)
System.out.println("Yes.");
else if(c++==d)
System.out.println("No.");
}
}

Language: Java. ​

Answers

Answered by Oreki
13

\textsf{\large \textbf{Given Snippet}}

\texttt{public class abc \{}

    \texttt{\hspace{1em} public static void main(String[] args) \{}

       \texttt{\hspace{2em} int c = 3, d = 4;}

       \texttt{\hspace{2em} if (c++ == d)}

          \texttt{\hspace{3em} System.out.println("Yes.");}

       \texttt{\hspace{2em} else if (c++ == d) }

          \texttt{\hspace{3em} System.out.println("No.");}

    \texttt{\hspace{1em} \}}

\texttt{\}}

\textsf{\large \textbf{After Execution}}

   \texttt{\large No.}

\textsf{\large \textbf{Explanation}}

  \text{As c is post incremented the first if condition is evaluated as false but the} \text{second condition evaluates to true and the final value of c becomes 5.}

Answered by Agent761
1

No output

Explanation:

Actually in your 'if' part, postfix is used, which means the new value will be increemented afterwards so, this is false and the compiler moves to next part without saving this and your second part also has the same condition. hence, both statements are false and you get no output.

Similar questions