Printing results if certain conditions are met in Mathematica

In summary, the conversation discusses using a for loop in Mathematica to calculate results and how to print only certain results that meet specific conditions. The speaker also provides a code example using conditional statements to achieve this.
  • #1
nikolafmf
114
0
Hello everyone,

I have for loop in Mathematica. After every step Mathematica has new results calculated. I want to see only some results, those who met certain conditions, not results from all steps. Say, I have 10000 steps and I don't need all 10000 results, but only those that met certain conditions. There are at least two or more conditions to be met. How can I tell Mathematica to print only those results?

So I want to tell this to Mathematic ain a for loop:

If a is true and b is true or c is true and d is true, then print blablabla, else do nothing.
 
Physics news on Phys.org
  • #2
In[1]:=For[i=1,i<100,i++,
(*doing stuff*)
If[Or[And[i>25,i<30],And[Mod[i,7]==3,Mod[i,11]==8]],Print]
(*doing more stuff*)
]

From In[1]:=26
From In[1]:=27
From In[1]:=28
From In[1]:=29
From In[1]:=52
 
Last edited:
  • #3
Thank you very much for your kind replay :).
 

Related to Printing results if certain conditions are met in Mathematica

What is Mathematica?

Mathematica is a computational software program used for mathematical, scientific, and engineering calculations. It is also commonly used for data analysis, visualization, and programming.

How do I print results if certain conditions are met in Mathematica?

To print results if certain conditions are met in Mathematica, you can use the "If" statement. The syntax for this statement is "If[condition, true result, false result]". This statement evaluates the condition and prints the true result if the condition is met, and the false result if the condition is not met.

Can I use multiple conditions in the "If" statement in Mathematica?

Yes, you can use multiple conditions in the "If" statement in Mathematica. You can use logical operators like "&&" (AND) and "||" (OR) to combine multiple conditions. For example, "If[condition1 && condition2, true result, false result]" will evaluate both conditions and print the true result if both conditions are met, and the false result if either one of the conditions is not met.

How do I add an "else" statement in the "If" statement in Mathematica?

To add an "else" statement in the "If" statement in Mathematica, you can use the "Else" or "ElseIf" statements. The syntax for these statements is "Else[false result]" and "ElseIf[condition, true result]". The "Else" statement will print the false result if the condition is not met, while the "ElseIf" statement will evaluate an additional condition and print the true result if the condition is met.

Are there other ways to print results if certain conditions are met in Mathematica?

Yes, there are other ways to print results if certain conditions are met in Mathematica. You can also use the "Switch" statement, which allows you to specify multiple cases and their corresponding results. Another option is to use the "Which" statement, which evaluates multiple conditions and prints the result of the first condition that is met.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
313
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
692
  • Engineering and Comp Sci Homework Help
Replies
1
Views
335
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
  • Programming and Computer Science
Replies
1
Views
786
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Programming and Computer Science
Replies
18
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top