In order to solve problems of this nature, you need to apply the PEMDAS rule which states the order in which the mathematical signs would be arranged and that is Division, Multiplication, Addition and Subtraction.
In the expression given which is
40 + 24 / 8 - 2² - 1
You take the expressions separated division and start with that by putting them in parenthesis
40 + (24 / 8) - 2² - 1
40 + 3 - 2² - 1
There is no multiplication, hence you go on to the addition sign and put them in parenthesis
(40 + 3) - 2² - 1
43 - 2² - 1
43 - 4 - 1
43 - 5
38