1. 程式人生 > >【基礎】循環以及循環控制語句

【基礎】循環以及循環控制語句

初始 返回 自增 區別 嵌套for循環 循環結構 使用方法 後置 共同點

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 為亮點

【基礎】循環以及循環控制語句