【错误】 “=” 与 "==" 不分

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main() {
    int a,b;
    a =1;
    
    while (a <= 10){
        cout << "" << a << "" << "  ";
        
        if (a=10){
            cout << endl; 
        }
        
        a++;
    }
    
    
    
    return 0;
}

上面是今天学习while是所写的代码,目的是输出while循环的次数,并在最后一次输出后换行

但编译运行的结果出乎我的意料,只输出了第一次

但在加入if语句之前并不会这样

看了几遍代码,觉得没有问题(真的)。。。

后来去问别人,发现自己又犯了同一个错误,”=“ 和 ”==“不分

 

”=“:一般是赋值给变量

”==“:判断左边是否等于右边

       等于:整个表达式的值为true

       不等于:整个表达式的值为false

 

正确的代码如下

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main() {
    int a,b;
    a =1;
    
    while (a <= 10){
        cout << "" << a << "" << "  ";
        
        if (a==10){
            cout << endl; 
        }
        
        a++;
    }
    
    
    
    return 0;
}

 

反思:1.代码还是看的和敲得太少了

      2.对自己不够自信,要自信些

                                      2018.01.27

                                        水汐音

posted @ 2018-01-27 14:49  水汐音  阅读(170)  评论(0编辑  收藏  举报