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

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.

主站蜘蛛池模板: 铜鼓县| 叙永县| 衢州市| 太康县| 荥阳市| 松阳县| 阳谷县| 郯城县| 东城区| 乐山市| 阳山县| 新泰市| 岳普湖县| 广元市| 阳泉市| 大关县| 乌拉特中旗| 杂多县| 和顺县| 潍坊市| 静海县| 博兴县| 惠东县| 乌苏市| 新平| 瓦房店市| 郧西县| 商南县| 昌吉市| 当雄县| 娄烦县| 靖州| 沙田区| 南昌县| 民权县| 中山市| 芜湖县| 陈巴尔虎旗| 宜丰县| 尚义县| 阿瓦提县|