Message Boards Message Boards

0
|
4477 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to merge some parts of my code, that run only within some logical tests

Posted 10 years ago
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"?
POSTED BY: Tom Zinger
4 Replies
Small suggestion: it is often better to use === as opposed to == as in,
If[
t ===1,
test1 = Log[23];
test2 = test1*3 + 45;
]
POSTED BY: David Reiss
Isn't this what you need?
If[
t == 1,
test1 = Log[23];
test2 = test1*3 + 45;
]
POSTED BY: Sam Carrettie
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?
POSTED BY: Frank Kampas
Posted 10 years ago
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.
POSTED BY: Tom Zinger
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract