Learning Switch Statements: Add/Subtract/Multiply/Divide Equations

  • Thread starter Thread starter Peach
  • Start date Start date
  • Tags Tags
    Switch
AI Thread Summary
Switch statements in programming, particularly in Java and C#, require case values to be constant primitive types or enumerated types. This means you cannot perform calculations like adding, subtracting, multiplying, or dividing within the case statements themselves. For example, expressions such as Math.Pow(x, 6)/6 or Math.PI*Math.Pow(x, 2) cannot be used directly as case values. Instead, calculations should be performed before the switch statement, storing the result in a variable, which can then be used in the switch statement for comparison. This approach allows for the effective use of switch statements while adhering to their syntactical constraints.
Peach
Messages
80
Reaction score
0
I'm new to programming and learning the switch statements right now. I'm wondering if you can add/subtract/multiply/divide equations in the switch statements.

Example: y = x^6/6

And if I want that in case 1, how do I calculate y assuming I already have x input beforehand?
 
Technology news on Phys.org
In Java cases in a switch statement need to be constant primitive types or enumerated types. C# relaxes this a little adding support for strings. But you can't have the following:
Code:
switch(y){
   case Math.Pow(x, 6)/6:
          //do something
   case Math.PI*Math.Pow(x, 2):
          //do something else
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top