• Post Reply Bookmark Topic Watch Topic
  • New Topic

Switch Statements  RSS feed

 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to make sure that to the answer to the question below is -1. Please correct me if I am wrong and explain and want to learn. Thanks!


What is y after the following switch statement?

        int x = 0;
        int y = 0; 
        switch (x + 1)  {
            case 0: y = 0;
            case 1: y = 1;
            default: y = -1
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant to say I want to learn. Lol
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Correct. And also, please QuoteYourSources.

Henry
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay sorry
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally there is a break statement at the end of each case statement.  Otherwise when one of the case statements is selected, the following case statements will be executed until a break or the end of the switch statement.
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags 

First of all this code doesn't compile as there is no ; after default: y = -1  and switch statement is not closed. If we still assume there is ; and switch statement is closed properly then yes y will be -1, reason as Norm explained.

Worth reading --->The switch Statement
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!