<p>What is BODMAS and why is it useful in programming?</p>

[{'answer_id': 3511, 'author': 'Michael Stum', 'author_id': 91, 'author_profile': 'https://Stackoverflow.com/users/91', 'pm_score': 5, 'selected': True, 'text': '<p><a href="http://www.easymaths.com/What_on_earth_is_Bodmas.htm" rel="noreferrer">http://www.easymaths.com/What_on_earth_is_Bodmas.htm</a>:</p>\n\n<blockquote>\n  <p>What do you think the answer to 2 + 3 x 5 is?</p>\n  \n  <p>Is it (2 + 3) x 5 = 5 x 5 = 25 ?</p>\n  \n  <p>or 2 + (3 x 5) = 2 + 15 = 17 ?</p>\n  \n  <p>BODMAS can come to the rescue and give us rules to follow so that we always get the right answer:</p>\n  \n  <p>(B)rackets (O)rder (D)ivision (M)ultiplication (A)ddition (S)ubtraction</p>\n  \n  <p>According to BODMAS, multiplication should always be done before addition, therefore 17 is actually the correct answer according to BODMAS and will also be the answer which your calculator will give if you type in 2 + 3 x 5 .</p>\n</blockquote>\n\n<p>Why it is useful in programming? No idea, but i assume it\'s because you can get rid of some brackets? I am a quite defensive programmer, so my lines can look like this:</p>\n\n<pre><code>result = (((i + 4) - (a + b)) * MAGIC_NUMBER) - ANOTHER_MAGIC_NUMBER;\n</code></pre>\n\n<p>with BODMAS you can make this a bit clearer:</p>\n\n<pre><code>result = (i + 4 - (a + b)) * MAGIC_NUMBER - ANOTHER_MAGIC_NUMBER;\n</code></pre>\n\n<p>I think i\'d still use the first variant - more brackets, but that way i do not have to learn yet another rule and i run into less risk of forgetting it and causing those weird hard to debug errors?</p>\n\n<p>Just guessing at that part though.</p>\n\n<p>Mike Stone EDIT: Fixed math as Gaius points out</p>\n'}, {'answer_id': 3515, 'author': 'Chris Benard', 'author_id': 448, 'author_profile': 'https://Stackoverflow.com/users/448', 'pm_score': 3, 'selected': False, 'text': '<p>Another version of this (in middle school) was "Please Excuse My Dear Aunt Sally".</p>\n\n<ul>\n<li>Parentheses</li>\n<li>Exponents</li>\n<li>Multiplication</li>\n<li>Division</li>\n<li>Addition</li>\n<li>Subtraction</li>\n</ul>\n\n<p>The mnemonic device was helpful in school, and still useful in programming today.</p>\n'}, {'answer_id': 3518, 'author': 'Patrick McElhaney', 'author_id': 437, 'author_profile': 'https://Stackoverflow.com/users/437', 'pm_score': 3, 'selected': False, 'text': '<p>Order of operations in an expression, such as:</p>\n\n<pre><code>foo * (bar + baz^2 / foo) \n</code></pre>\n\n<ul>\n<li><strong>B</strong>rackets first</li>\n<li><strong>O</strong>rders (ie Powers and Square Roots, etc.)</li>\n<li><strong>D</strong>ivision and <strong>M</strong>ultiplication (left-to-right)</li>\n<li><strong>A</strong>ddition and <strong>S</strong>ubtraction (left-to-right)</li>\n</ul>\n\n<p>source: <a href="http://www.mathsisfun.com/operation-order-bodmas.html" rel="nofollow noreferrer">http://www.mathsisfun.com/operation-order-bodmas.html</a></p>\n'}, {'answer_id': 3525, 'author': 'Wally Lawless', 'author_id': 37, 'author_profile': 'https://Stackoverflow.com/users/37', 'pm_score': 2, 'selected': False, 'text': '<p>When I learned this in grade school (in Canada) it was referred to as BEDMAS:</p>\n\n<p><strong>B</strong>rackets<br>\n<strong>E</strong>xponents<br>\n<strong>D</strong>ivision<br>\n<strong>M</strong>ultiplication<br>\n<strong>A</strong>ddition<br>\n<strong>S</strong>ubtraction</p>\n\n<p>Just for those from this part of the world...</p>\n'}, {'answer_id': 3527, 'author': 'ricree', 'author_id': 121, 'author_profile': 'https://Stackoverflow.com/users/121', 'pm_score': 1, 'selected': False, 'text': "<p>I'm not really sure how applicable to programming the old BODMAS mnemonic is anyways.  There is no guarantee on order of operations between languages, and while many keep the standard operations in that order, not all do.  And then there are some languages where order of operations isn't really all that meaningful (Lisp dialects, for example).  In a way, you're probably better off for programming if you forget the standard order and either use parentheses for everything(eg (a*b) + c) or specifically learn the order for each language you work in.</p>\n"}, {'answer_id': 10557, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 3, 'selected': False, 'text': '<p>I don\'t have the power to edit <a href="https://stackoverflow.com/a/3511/50776">@Michael Stum\'s answer</a>, but it\'s not quite correct.  He reduces</p>\n\n<pre><code>(i + 4) - (a + b)\n</code></pre>\n\n<p>to</p>\n\n<pre><code>(i + 4 - a + b)\n</code></pre>\n\n<p>They are not equivalent.  The best reduction I can get for the whole expression is</p>\n\n<pre><code>((i + 4) - (a + b)) * MAGIC_NUMBER - ANOTHER_MAGIC_NUMBER;\n</code></pre>\n\n<p>or</p>\n\n<pre><code>(i + 4 - a - b) * MAGIC_NUMBER - ANOTHER_MAGIC_NUMBER;\n</code></pre>\n'}, {'answer_id': 225202, 'author': 'Christopher Lightfoot', 'author_id': 24525, 'author_profile': 'https://Stackoverflow.com/users/24525', 'pm_score': 0, 'selected': False, 'text': "<p>I read somewhere that especially in C/C++ splitting your expressions into small statements was better for optimisation; so instead of writing hugely complex expressions in one line, you cache the parts into variables and do each one in steps, then build them up as you go along.</p>\n\n<p>The optimisation routines will use registers in places where you had variables so it shouldn't impact space but it can help the compiler a little.</p>\n"}]
['https://Stackoverflow.com/questions/3510', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/383/']