Solving Switch Statement Problem with y as Integer

  • Thread starter Thread starter magma_saber
  • Start date Start date
  • Tags Tags
    Integer Switch
Click For Summary
SUMMARY

The discussion revolves around implementing a switch statement in programming to assign values to the variable B based on the integer value of y. The correct implementation requires using the switch statement to evaluate y, not B. The final solution correctly assigns true to B when y is in the set {1, 2, 3, 5, 7}, false when y is in {4, 6, 8, 10}, and assigns the vector [1 0 1] for any other values of y. The solution emphasizes the importance of correctly structuring the switch statement for accurate results.

PREREQUISITES
  • Understanding of switch statements in programming
  • Familiarity with conditional logic
  • Basic knowledge of variable assignment
  • Experience with integer data types
NEXT STEPS
  • Learn about switch statement syntax in your programming language of choice
  • Explore conditional statements and their applications
  • Study variable scope and assignment in programming
  • Investigate error handling in switch statements
USEFUL FOR

This discussion is beneficial for novice programmers, students tackling homework assignments involving control structures, and anyone looking to improve their understanding of switch statements and conditional logic in programming.

magma_saber
Messages
73
Reaction score
0

Homework Statement


switch statements. y is an integer number. Use a switch statement to set B equal to one of the following values: true, false, or the vector [1 0 1]. If y is {1,2,3,5,7}, then set B to true. If y is {4, 6, 8, 10}, then set B to false. If y is neither, then set B to the vector [1 0 1]. You must use a switch statement to solve this problem and your answer should be stored in the variable named "B".

Homework Equations





The Attempt at a Solution


B='true'
switch B
case '{1,2,3,5,7}'
y=true
case '{4, 6, 8, 10}'
y=false
otherwise
y=[1 0 1]
end

i tried that and it said it was wrong. a file was provided that tells you if its wrong.
 
Physics news on Phys.org
I believe you want your switch to be the criteria you are comparing, ie. y and your statement following the case to be B (the variable you are assigning a value to).

Switch y

case '{1,2,3,5,7}'
B = true
case '{4,6,8,10}'
B = false
.
.
.
etc
 
never mind i figured it out.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 80 ·
3
Replies
80
Views
10K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
2K
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
33
Views
5K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K