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

  • Context: Mathematica 
  • Thread starter Thread starter habad
  • Start date Start date
  • Tags Tags
    Loop Mathematica
Click For Summary
SUMMARY

The forum discussion addresses the use of the "Which" function within a "For" loop in Mathematica. The user initially encounters issues with the output being "Null" due to the incorrect handling of the "Which" statement. A solution is provided, where the result of the "Which" function is assigned to a variable before being printed, specifically using the syntax w = Which[a == 1611, x, a == 1612, y]; followed by Print[w];. This adjustment resolves the issue and produces the expected output.

PREREQUISITES
  • Basic understanding of Mathematica syntax and functions
  • Familiarity with control structures in programming, specifically "For" loops
  • Knowledge of the "Which" function in Mathematica
  • Ability to debug and modify code in Mathematica
NEXT STEPS
  • Explore advanced usage of the "Which" function in Mathematica
  • Learn about variable assignment and scope in Mathematica
  • Investigate other control structures in Mathematica, such as "While" and "If"
  • Practice debugging techniques for Mathematica code
USEFUL FOR

This discussion is beneficial for novice Mathematica users, programmers looking to enhance their understanding of control structures, and anyone seeking to troubleshoot and optimize their Mathematica code.

habad
Messages
2
Reaction score
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
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.
 
Thank you so much. It works for me. :)
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 12 ·
Replies
12
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 23 ·
Replies
23
Views
6K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K