官术网_书友最值得收藏!

Flow control

We learned about comparisons and logical operators earlier. Now what do we do if we want different things to happen depending on the results of those comparisons? Flow control helps us do exactly that. Let's learn how we can specify what happens under different circumstances.

If else

If/else is the basic flow control statement. Let's look at this sentence:

If it's raining I'll take an umbrella.

Using an if statement, that sentence would be written like this:

if(bRaining)
{
    bUmbrella = true;
}

We could also add an else statement to it:

If it's raining I'll take an umbrella, otherwise I'll wear short sleeves.

That would be written like this:

if(bRaining)
{
    bUmbrella = true;
}
else
{
    bShortSleeves = true;
}

We can also use Else If for other conditions.

If it's raining I'll take an umbrella, or if it's cold I'll wear a coat, otherwise I'll wear short sleeves.

We could write that like this:

if(bRaining)
{
    bUmbrella = true;
}
else if(Temperature < ComfortableTemperature)
{
    bCoat = true;
}
else
{
    bShortSleeves = true;
}

The important thing to remember about else/if is, that only one of these conditions will run. If it's raining and cold, only the bRaining section of the code will run, not bRaining and Temperature < ComfortableTemperature.

主站蜘蛛池模板: 三江| 洪江市| 泰和县| 荆门市| 徐水县| 松桃| 鄢陵县| 宣武区| 潢川县| 景泰县| 泌阳县| 探索| 同江市| 南靖县| 巴中市| 汽车| 光泽县| 名山县| 曲水县| 丰宁| 黄大仙区| 额济纳旗| 霍林郭勒市| 崇阳县| 马尔康县| 西畴县| 青田县| 富锦市| 高雄市| 屯昌县| 济阳县| 郯城县| 呼和浩特市| 吉木萨尔县| 中宁县| 安仁县| 齐齐哈尔市| 安国市| 石阡县| 绥芬河市| 甘泉县|