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

  • Thread starter Thread starter Peach
  • Start date Start date
  • Tags Tags
    Switch
Click For Summary
SUMMARY

The discussion focuses on the limitations of using switch statements in programming languages like Java and C#. Specifically, it highlights that switch cases in Java require constant primitive types or enumerated types, while C# allows strings but still does not support expressions like mathematical calculations directly in case statements. The user seeks to understand how to implement equations such as y = x^6/6 within a switch statement context, which is not feasible due to these constraints.

PREREQUISITES
  • Understanding of switch statements in Java and C#
  • Basic knowledge of mathematical operations and expressions
  • Familiarity with programming concepts such as variables and data types
  • Experience with Java 8 or C# 7.0 or later versions
NEXT STEPS
  • Research alternatives to switch statements in Java, such as if-else chains
  • Explore the use of function calls within switch cases in C#
  • Learn about constant expressions in Java and how to define them
  • Investigate the use of lambda expressions for mathematical operations in C#
USEFUL FOR

Beginner programmers, Java and C# developers, and anyone interested in understanding the limitations and alternatives to switch statements for mathematical operations.

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
}
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
1K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
3
Views
2K
  • · Replies 27 ·
Replies
27
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 22 ·
Replies
22
Views
2K