basilis5
(Tom Zinger)
March 19, 2014, 3:37pm
1
I have a trigger on/off. I want a large part of my code running only when trigger is on. Nevertheless using “if” function leaves me with no other option but to recall it for each expression I use.
This is a naive example to point out what i mean:
t = 0;
If[t == 1,
test1 = Log[23]
]
If[t == 1,
test2 = test1*3 + 45
]
setting t==1 (on, runs my code. test1 is initialized. Then I do the same thing with test2 (i mean, repeating the “if” check).
is there any way to run all my code within one “if”?
fkampas
(Frank Kampas)
March 19, 2014, 4:03pm
2
How about defining a function that performs the tasks you want to switch on or off and putting a call to that function in your If statement?
basilis5
(Tom Zinger)
March 19, 2014, 4:30pm
3
how am i supposed to do that in my example? any idea?
in real life, I have lots of stuff going on, much more complicated expressions that test1 and test2 in my previous example.
Isn’t this what you need?
If[
t == 1,
test1 = Log[23];
test2 = test1*3 + 45;
]
dreiss
(David Reiss)
March 19, 2014, 5:07pm
5
Small suggestion: it is often better to use === as opposed to == as in,
If[
t ===1,
test1 = Log[23];
test2 = test1*3 + 45;
]