Help with "Which[] in a For loop using Mathematica

In summary, the conversation discusses a code involving a "Which" statement and a "For" loop and the issue of getting the desired result. It is concluded that if the output of the "Which" statement is not saved in a variable, it will result in a Null value being printed. Adding a semicolon after the "Which" statement will discard the output and result in no value being printed. To get the desired result, the output of the "Which" statement must be saved in a variable and then printed separately.
  • #1
habad
2
0
Hello,
I am a novice in Mathematica, but insisting to learn it. I would appreciate help in solving the following problem:

(*a=1;Which[a==1,x,a==2,b]*)

list := List[1611, 1612, 1613, 1614, 1615, 1616, 1617, 1618, 32, 10]

For[i = 1, i < 4, i++,

a = list[];

Print[a];

Print[list[]];

Which[a == 1611, x, a == 1612, y];

Print[%]]


When I run the code, I get the following result:

1611

1611

Null

1612

1612

Null

1613

1613

Null

While I think I am faithfully mimicking the Mathematica "Which statement" quoted above, it seems including the "Which" statement in the "For" loop does not make work as intended. Is this right? If not, please point out my mistake.

Best Regards
 
Physics news on Phys.org
  • #2
Compare these two

Code:
In[3]:= q = 1611;
Which[q == 1611, x,
 q == 1612, y]

Out[4]= x

In[5]:= q = 1611;
Which[q == 1611, x,
  q == 1612, y];

In the first the value of the Which is x. In the second the semicolon, after after that Which, discards the output and the result is simply nothing (or Null). So in your example you are Printing the result returned from the previous expresssion, the Which, and that is nothing or Null. Thus Null is Printed.

If you changed your code to be

Code:
w = Which[a == 1611, x,
   a == 1612, y];
Print[w]

then you might get what you are hoping for.
 
  • #3
Thank you so much. It works for me. :)
 

1. What is the purpose of the "Which" statement in a For loop using Mathematica?

The "Which" statement in a For loop using Mathematica allows you to specify different conditions and corresponding actions to be executed. This is useful when you want to perform different tasks depending on different conditions.

2. How is the syntax for "Which" different from other conditional statements in Mathematica?

The syntax for "Which" is different from other conditional statements in Mathematica because it uses a list of conditions and actions instead of individual statements. This makes it more concise and easier to read.

3. Can multiple conditions be specified in a single "Which" statement?

Yes, multiple conditions can be specified in a single "Which" statement by separating them with a comma. The first condition that evaluates to "True" will trigger the corresponding action to be executed.

4. How does the "Which" statement handle cases where no conditions are met?

If none of the conditions in a "Which" statement evaluate to "True", then no action will be executed. However, you can include a final "Else" statement to specify what should be done in this case.

5. Can the "Which" statement be nested within other conditional statements in Mathematica?

Yes, the "Which" statement can be nested within other conditional statements in Mathematica, such as "If" or "Switch". This allows for more complex and specific conditions to be evaluated and actions to be performed.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
261
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
825
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
22
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • Programming and Computer Science
Replies
16
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top