How can I use conditionals in Mathematica to check for multiples of 3?

  • Mathematica
  • Thread starter Niles
  • Start date
  • Tags
    Mathematica
In summary, conditionals in Mathematica are statements that allow the program to make decisions based on a given condition. They are written using the "If" function and can have multiple conditions by using the "ElseIf" function. The "If" function is used for simple conditional statements, while the "Switch" function is used for more complex ones. Conditional statements can also be used in a loop using the "Do" function.
  • #1
Niles
1,866
0
Hi all

I'm am looking for a smart way to make the following conditional in Mathematica:

Code:
if[i == 3*{1,2,3,4,5,N}, ...]
,

ï.e. if the variable i is a multiple of 3 up to some integer N. How can I do this smartest in Mathematica?


Niles.
 
Physics news on Phys.org
  • #2
Er, what are you asking?

How to make a conditional statement? Check the documentation for If. (Or the guide section on programming)

How to make a range of integers? Check the documentation for Table. (Or the guide section on lists and sequences)
 
  • #3
Ok, my answers were both answered looking at the documentation. Thanks.
 

Related to How can I use conditionals in Mathematica to check for multiples of 3?

1. What are conditionals in Mathematica?

Conditionals in Mathematica are statements that allow the program to make decisions based on a given condition. They evaluate whether a statement is true or false, and then execute different blocks of code accordingly.

2. How do you write a conditional statement in Mathematica?

In Mathematica, conditional statements are written using the "If" function. The syntax is: If[condition, true statement, false statement]. The condition is evaluated first, and if it is true, the true statement is executed. If the condition is false, the false statement is executed.

3. Can you have multiple conditions in a single conditional statement?

Yes, you can have multiple conditions in a single conditional statement by using the "ElseIf" function. The syntax is: If[condition1, true statement1, ElseIf[condition2, true statement2, false statement]]. You can have as many "ElseIf" statements as needed.

4. What is the difference between "If" and "Switch" in Mathematica?

The "If" function is used for simple conditional statements, where only one condition needs to be evaluated. The "Switch" function, on the other hand, is used for more complex conditional statements, where multiple conditions need to be evaluated and different actions need to be taken based on those conditions.

5. Can you use conditional statements in a loop in Mathematica?

Yes, you can use conditional statements in a loop in Mathematica by using the "Do" function. The syntax is: Do[If[condition, action], {variable, initial value, final value}]. The loop will continue to execute until the condition becomes false.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
313
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
748
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top