• Post Reply Bookmark Topic Watch Topic
  • New Topic

Switch Statements  RSS feed

 
Sam Ketha
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do switch statements do and How do you use them? I tried the w3schools page on it but didn't understand it very well.
 
Wes McClintock
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
switch statements are almost the same as if statements, they're just designed to be larger.
for example:


is much faster than doing the same thing with if statements.
the "default" will be done if none of the cases return true.
 
Madhan Sundararajan Devaki
Ranch Hand
Posts: 312
Java MS IE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe, the posted code snippet should be modified as follows.



NOTE: The use of break
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if days == 9?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's more than 12 days
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously, Jesper, switch statements use nonary arithmetic.
 
Wes McClintock
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Madhan Sundararajan Devaki wrote:I believe, the posted code snippet should be modified as follows.



NOTE: The use of break


yep, i did this in a hurry, sorry.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
srikanth kumar aruru
Greenhorn
Posts: 4
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
switch statements are more than if statements. switch statement is more efficient than if else statements in terms of efficiency (number of cpu cycles will be less) , code looks clean as well.

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

The disadvantage of switch is that the values must be compile-time constants.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!