---
# System prepended metadata

title: 語法複習--for
tags: [一社一服務]

---

---
tags: 一社一服務
---
# 語法複習--for
[回目錄](https://hackmd.io/@PeiYun/CppTutorial)
```cpp=
for (初值設定; 執行條件; 回合收尾) 
    重覆內容;
```
例題1
輸入一整數x，計算 1加到x的和
```cpp=
#include<iostream>
using namespace std;
int main(){
    int sum=0;
    for(int n=1; n<=x; n++){
        sum+=n;
    }
    cout<<sum;
    return 0;
}
```