【基础】循环以及循环控制语句

while:

/*
演示while和do...while的使用方法及区别 

while (表达式){
    代码块 
}

do {
    代码块 
} while(表达式);

while与do...while的共同点:当表达式的结果为true(或1)时,才继续循环
                          当表达式的结果为false(或0时),跳出循环 
                   不同点:相比于while,do...while至少会先执行一遍代码块内的代码 
*/ 
 #include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main() {
    int a,b;
    a =1;
    b = 1;
    
    while (a <= -1){
        cout << "while:";
        cout << "" << a << "" << endl;
        
        a++;
    }
    //这里的while循环不会输出任何信息 
    
    do {
        cout << "do...while:";
        cout << "" << b << "" << "  " << endl;
        
        b++;
    } while(b <= -1);
    //而这里的do...while循环会先执行一次 ,再跳出循环                                                                                                                                                                         
    
    return 0;
}

for:

/*
演示for的使用方法

for (初始语句;表达式1;表达式2){
    循环体 
}
执行一次初始语句 ----> 计算 表达式 1 的值  
                            -- true --> 进入并执行循环体 ----> 计算 表达式 2的值   ----> 回到表达式1 
                            -- false --> 退出循环 
*/

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main() {
    for (int i=1; i<=10; i++){
        cout << i << endl; 
    } 
    
    for (int i=1; i<10; i++){
        cout << i << endl; 
    }
    
    for (int i=10; i>=1; i--){
        cout << i;
        
        for (int j=0; j<10; j++){
            cout << j << " ";
            
            if (j==9){
                cout << endl;
            }
            
        }
    }
    //嵌套for循环,一共执行了 10(外层) * 10(内层) = 100 次 
    
    return 0;
}

break、continue:

/*
演示break和continue的用法

break:
  不考虑当前循环结构,直接从当前循环跳出,执行
  循环后面的语句
 
continue:
  结束本次循环,进入下一轮循环 
*/

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main() {
    cout << "原本:" << endl;
    int i;
    
    for (i=0; i<10; i++){
        cout << i << " ";
    } 
    
    cout << endl;
    cout << "break when i==5:" << endl;
    
    for (i=0; i<10; i++){
        cout << i << " ";
        
        if (i==5){
            break;
        }      
    }
    
    cout << endl;
    cout << "continue when i==5:" << endl;
    
    for (i=0; i<10; i++){
        cout << i << " ";
        
        if (i==5){
            continue;
        }
    } 
    
    return 0;
}

自增自减运算符:

/*
1.自增运算符: ++
  自减运算符: --
  自增(自减)运算符:使当前变量的值 +1 (或 -1)

2.自增(自减)运算符可以分为前置和后置两种用法

以自增为例,自减亦然 
*/

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main() {
    int a;
    a = 1;
    
    cout << a++ << endl;
    cout << a << endl;
    /*
    后置运算符特点:首先将a的值储存到一个临时变量中,对a进行自增操作, 
                         最后返回临时变量的值 
                      就是说,后置运算符返回的是a执行自增操作前的原始值,然后进行自增操作 
    */ 
    
    a--;
    
    cout << ++a << endl; 
    //前置运算符先让a自增,然后直接返回a的值 
    
    return 0;
}

练习

练习1:判断质数

判断给出数字是否为质数

#include<iostream>
using std::cin;
using std::cout;
using std::endl;

int main(){
    int n,m,a;
    cin >> n;
    a = 1;
    
    for (m =2; m<n; m++){
        if (n % m == 0){
            a = 0;
            cout << "NO";
            break;
        }
    }
    
    if (a == 1){
        cout << "YES";
    }
    
    return 0;
}

Tips: 变量 a 为亮点

代码:https://pan.baidu.com/s/1htHpGX2

原文地址:https://www.cnblogs.com/syxy/p/8436231.html