Printing results if certain conditions are met in Mathematica

Click For Summary
SUMMARY

The discussion focuses on using conditional statements within a for loop in Mathematica to selectively print results based on specific criteria. The user seeks to display results only when certain conditions are met, such as when the index is between 25 and 30 or when the index meets specific modular conditions. The provided code snippet demonstrates the implementation of these conditions using the If and Or functions in Mathematica. The solution effectively filters the output to show only relevant results from a large number of iterations.

PREREQUISITES
  • Familiarity with Mathematica programming language
  • Understanding of control structures, specifically for loops
  • Knowledge of logical operators such as And and Or
  • Basic understanding of modular arithmetic
NEXT STEPS
  • Explore advanced conditional statements in Mathematica
  • Learn about performance optimization in Mathematica loops
  • Investigate the use of functional programming techniques in Mathematica
  • Study the Print function and its alternatives for output management in Mathematica
USEFUL FOR

This discussion is beneficial for Mathematica users, programmers looking to optimize their code, and anyone interested in implementing conditional logic within iterative processes.

nikolafmf
Messages
112
Reaction score
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
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:
Thank you very much for your kind replay :).
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K