0
Who can help me? Java question
I don’t understand very well Java so far. Who could help me explaining this short code’s lines. —— No entiendo aún mucho Java. Quien podría explicarme este código de Java 🤓😃 int x=1; Switch(1){ Case 1: System.out.print(1); Case 2: System.out.print(2); break; Case 3: System.out.print(3); } No entiendo porque la respuesta es 12 Gracias a quien me pueda explicar 😊😊😊
5 Antworten
+ 5
In your program errors are there ..every java keyword starts with lowercase letters..in your code you wrote Switch,Case ...(there must be in lowercase)
Coming to your program logic....
variable x is initialized with value 1.
In switch ,it searches is there any matching case label available or not.if available then it executes all matching case labels.. otherwise not execute.
Here, switch(1) contains value 1 so,corresponding case 1 is executed and then there is no break statement so it execute case 2 too .
Now , output is
1
2
NOTE: one thing int x=1 here there is no use
+ 3
anyone who has knowledge about Java
have you learnd c
switch has condition
cases have answers
if any one of following cases will satisfy the condition it breaks the program or stop the program
cases = options
break = stop the process when this satisfy
+ 2
Johanna Andrade
My pleasure...🙂
+ 1
Thanks ☕Navya💥 for your explanation 😊
+ 1
thanks Deepak Jaiswar ☺️