0 added
0 removed
Original
2026-01-01
Modified
2026-03-09
1
<p>Congratulations, you completed the first baptism by fire of the first program! Now the Muffin will give you tasks for more complex programs, programs with conditions.</p>
1
<p>Congratulations, you completed the first baptism by fire of the first program! Now the Muffin will give you tasks for more complex programs, programs with conditions.</p>
2
<p>And you right away get<b>the first task</b>: you need to develop a program called “Can I Eat More?”. This is a very important issue for everyone who is preparing for the exhibition. The program uses the following data:</p>
2
<p>And you right away get<b>the first task</b>: you need to develop a program called “Can I Eat More?”. This is a very important issue for everyone who is preparing for the exhibition. The program uses the following data:</p>
3
<ul><li>daily amount of food (variable dayLimit);</li>
3
<ul><li>daily amount of food (variable dayLimit);</li>
4
<li>weight of food eaten today (variable foodInGrams).</li>
4
<li>weight of food eaten today (variable foodInGrams).</li>
5
</ul><p>If the Muffin ate less than the daily amount, then he can eat more. If he exceeded the norm, then he can’t eat anymore.</p>
5
</ul><p>If the Muffin ate less than the daily amount, then he can eat more. If he exceeded the norm, then he can’t eat anymore.</p>
6
<p>So far, we have written linear programs that always executed<em>the same commands</em>. Now we need the program to become non-linear: depending on <em>different conditions</em>, it must perform<em>different commands</em>. How to teach the program to check the conditions and make decisions based on the results of the checks?</p>
6
<p>So far, we have written linear programs that always executed<em>the same commands</em>. Now we need the program to become non-linear: depending on <em>different conditions</em>, it must perform<em>different commands</em>. How to teach the program to check the conditions and make decisions based on the results of the checks?</p>
7
<p>To check conditions, use the conditional operator if:</p>
7
<p>To check conditions, use the conditional operator if:</p>
8
if (condition) { action; }<p>Here, the “condition” is an expression that returns true or false, and the “actions” inside curly brackets are commands that are executed if the condition is satisfied.</p>
8
if (condition) { action; }<p>Here, the “condition” is an expression that returns true or false, and the “actions” inside curly brackets are commands that are executed if the condition is satisfied.</p>
9
<p>First, let’s write a condition that will check if the Muffin ate less than the daily amount of food. And if the condition is met, we’ll tell the Muffin that he can eat a little more.</p>
9
<p>First, let’s write a condition that will check if the Muffin ate less than the daily amount of food. And if the condition is met, we’ll tell the Muffin that he can eat a little more.</p>