Solving Switch Statement Problem with y as Integer

In summary, the conversation is about using a switch statement to set the value of B based on the value of y, which is an integer number. The possible values for B are true, false, or the vector [1 0 1]. If y is {1,2,3,5,7}, then B is set to true. If y is {4, 6, 8, 10}, then B is set to false. If y is neither, then B is set to [1 0 1]. The code provided in the conversation uses a switch statement with the criteria being y and the variable being assigned a value being B.
  • #1
magma_saber
73
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
  • #2
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
 
  • #3
never mind i figured it out.
 

1. How do I use a switch statement to solve a problem with y as an integer?

To use a switch statement to solve a problem with y as an integer, you first need to declare the variable y and assign it a value. Then, you can use the switch statement with y as the parameter in the parentheses. Inside the curly braces, you can use case statements to handle different values of y and their corresponding actions.

2. What is the purpose of using a switch statement for y as an integer?

The purpose of using a switch statement for y as an integer is to efficiently handle multiple cases or conditions based on the value of y. This allows for cleaner and more organized code compared to using multiple if/else statements.

3. Can I use multiple cases with the same action in a switch statement for y as an integer?

Yes, you can use multiple cases with the same action in a switch statement for y as an integer. This is useful when multiple values of y require the same action to be taken.

4. What happens if none of the cases in a switch statement for y as an integer match the value of y?

If none of the cases in a switch statement for y as an integer match the value of y, the default case will be executed. This is typically used as a catch-all case to handle any unexpected or invalid values of y.

5. Is it necessary to include a default case in a switch statement for y as an integer?

No, it is not necessary to include a default case in a switch statement for y as an integer. However, it is considered good practice to include one in case of unexpected or invalid values of y. Omitting a default case may result in the code not functioning as intended or causing errors.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
855
  • Engineering and Comp Sci Homework Help
Replies
3
Views
868
  • Engineering and Comp Sci Homework Help
3
Replies
80
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
33
Views
3K
Back
Top