Simplify nested If into a loop - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-10-16T20:28:29Z https://mathematica.stackexchange.com/feeds/question/156668 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/156668 1 Simplify nested If into a loop Elsa https://mathematica.stackexchange.com/users/14085 2017-09-28T02:10:36Z 2017-09-28T03:41:53Z <p>I would like to simplify a set of tests (meant to be applied on a random set of parameters), written for now as nested If:</p> <pre><code>Tableinit = {{"a", "b"}}; c = {1.8, 1.3, 2.5}; Do[Print[set = {RandomReal[{0, 3}], RandomReal[{0, 2}]}]; testP = {a -&gt; set[], b -&gt; set[]}; If[a &gt; c[] /. testP, If[a &gt; c[] /. testP, If[a &gt; c[] /. testP, Print["yaha"], Continue[]], Continue[]], Continue[]], {3}] </code></pre> <p>So that I get for example:</p> <pre><code>{1.89136,0.143095} {2.98853,1.98695} yaha {1.93,0.424178} </code></pre> <p>into a loop. The closest I got was:</p> <pre><code>Tableinit = {{"a", "b"}}; c = {1.8, 1.3, 2.5}; Do[Print[set = {RandomReal[{0, 3}], RandomReal[{0, 2}]}]; testP = {a -&gt; set[], b -&gt; set[]}; Do[If[a &gt; c[[i]] /. testP, Print["yaha"], Break[]], {i, 3}], {3}] </code></pre> <p>But as expected I get:</p> <pre><code>{1.89136,0.143095} {2.98853,1.98695} yaha yaha yaha {1.93,0.424178} yaha </code></pre> <p>Instead of getting "yaha" only when the 3 conditions are fulfilled.</p> <p>How could I write it? Any help will be greatly appreciated. Thanks. </p> https://mathematica.stackexchange.com/questions/156668/-/156672#156672 2 Answer by Alan for Simplify nested If into a loop Alan https://mathematica.stackexchange.com/users/19530 2017-09-28T03:41:53Z 2017-09-28T03:41:53Z <pre><code>If[And @@ Thread[a &gt; c /. testP], Print["yaha"]] </code></pre>