记录C++学习 3 条件与分支(if)、循环(for、while)、控制流语句(continue、break、return)

条件与分支(if语句)

if例子

和python中相同,=为赋值运算符 ;==为比较运算符,返回true或false,if 后面若为true,运行语句块{}

#include <iostream>
#include "Log.h"

int main()
{
    int x = 5;
    bool comparisonresult = x == 5;
    if (comparisonresult)
    {
        Log("Hello world");
    }
    std::cin.get();
}

我们在debug模式下,打开反汇编视图,可以更清晰的看到计算机做了什么,

 只要if里的变量不为0,则执行{}语句块的内容,所以可以简化以上代码如下

#include <iostream>
#include "Log.h"

int main()
{
    int x = 5;
    if (x==5)
    {
        Log("Hello world");
    }
    std::cin.get();
}

 还可以进一步简化如下

#include <iostream>
#include "Log.h"

int main()
{
    int x = 5;
    if (x==5)
        Log("Hello world");
    std::cin.get();
}

再简化

但不推荐,在debug时会难以确定出错位置

#include <iostream>
#include "Log.h"

int main()
{
    int x = 5;
    if (x==5) Log("Hello world");
    std::cin.get();
}

 在检验指针是否为空时也常用,我们将指针放到if中,看是否运行语句块,判断指针为空即null

当指针不为空,如下

#include <iostream>
#include "Log.h"

int main()
{
    const char* ptr="hello";
    if (ptr) 
        Log(ptr);
    std::cin.get();
}

结果为hello

 当指针为null,如下

#include <iostream>
#include "Log.h"

int main()
{
    const char* ptr=nullptr;
    if (ptr) 
        Log(ptr);
    std::cin.get();
}

结果为空

 if else例子

#include <iostream>
#include "Log.h"

int main()
{
    const char* ptr = nullptr;
    if (ptr)
        Log(ptr);
    else
        Log("ptr is null");
    std::cin.get();
}

 else if例子

else if只有在if失败后才执行

#include <iostream>
#include "Log.h"

int main()
{
    const char* ptr = "ptr";
    if (ptr)
        Log(ptr);
    else if (ptr =="hello")
        Log("ptr is hello");
    else
        Log("ptr is null");
    std::cin.get();
}

 在上面代码中,else if永远不会执行,因为如果ptr不是null,会执行if,然后打印结束;如果是null,会执行else,打印结束。

其实else if并不是C++的关键词(python中的elif),是else和if的组合,就像下面的代码

#include <iostream>
#include "Log.h"

int main()
{
    const char* ptr = "ptr";
    if (ptr)
        Log(ptr);
    else
    {
        if (ptr == "hello")
            Log("ptr is hello");
    }
    std::cin.get();
}

 当我们将{}去掉,像之前那样简化成一行代码,就变成了else if(Ohhhhhhhhhhhhhhhhhhh!)

#include <iostream>
#include "Log.h"

int main()
{
    const char* ptr = "ptr";
    if (ptr)
        Log(ptr);
    else  if (ptr == "hello")
        Log("ptr is hello");
    std::cin.get();
}

多个if例子

可以运用多个if,如下

#include <iostream>
#include "Log.h"

int main()
{
    const char* ptr = "hello";
    if (ptr)
        Log(ptr);
    if (ptr =="hello")
        Log("ptr is hello");
    else
        Log("ptr is null");
    std::cin.get();
}

 结果是hello和ptr is hello都被打印

 但是,条件分支语句虽然非常方便实用,但会大大影响性能,降低运行速度。

循环(for、while语句)

for循环

我们希望把hello world打印5遍,当然,复制粘贴是可行的

#include <iostream>
#include "Log.h"

int main()
{
    Log("hello world");
    Log("hello world");
    Log("hello world");
    Log("hello world");
    Log("hello world");
    std::cin.get();
}

我们也可以用for循环,for循环包含三个部分,用分号隔开,变量声明,条件,每次迭代的变化

条件为真,便执行{}内代码块,条件为假,便退出循环

i++和i+=1和i=i+1相同

于是我们有以下代码

#include <iostream>
#include "Log.h"

int main()
{
    for (int i = 0; i < 5; i++)
    {
        Log("hello world");
    }
    std::cin.get();
}

while循环

while和for类似,但是没有前面的声明和后面每次迭代的变化,仅有条件语句

所以在前面要设置变量,在后面语句块要改变变量,代码如下

#include <iostream>
#include "Log.h"

int main()
{
    int i = 0;
    while(i<5)
    {
        Log("hello world");
        i++;
    }
    std::cin.get();
}

控制流语句(continue,break,return)

continue

continue表示进入循环的下一次迭代

#include <iostream>
#include "Log.h"

int main()
{

    for (int i=0;i<5;i++)
    {
        if (i % 2 == 0)
            continue;
        Log("hello world");
        std::cout << i << std::endl;
    }
    std::cin.get();
}

结果

hello world
1
hello world
3

break 

break跳出循环

#include <iostream>
#include "Log.h"

int main()
{

    for (int i=0;i<5;i++)
    {
        if (i % 2 == 0)
            break;
        Log("hello world");
        std::cout << i << std::endl;
    }
    std::cin.get();
}

 结果为空,因为i=0时,直接跳出循环了

return 

return退出这个函数,void函数不需要你为它提供返回值,所以return适用于void函数,在其他函数中你需要提供一个返回值

#include <iostream>
#include "Log.h"

int main()
{

    for (int i=0;i<5;i++)
    {
        if (i % 2 == 0)
            return 0;
        Log("hello world");
        std::cout << i << std::endl;
    }
    std::cin.get();
}

在以上代码中,其实执行到return 0 后就结束了,所以下面的代码不会执行,也不会打印任何东西

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值