while循环语句

condition 可以是任意的表达式,当为任意非零值时都为真。当条件为真时执行循环。
The while loop loops through a block of code as long as a specified condition is true:

while (condition) {
  // code block to be executed
}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <iostream>
using namespace std;
int main(){
    int i =0;
    while(i <5) {
        cout << i <<"\n";
        i++;
      }
    cout<<i;
return 0;
}

edit & run

用while求解1-100的和

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <iostream>
using namespace std;
int main(){
     int b=1,c=0;
     while (b<=100){
     c=c+b;
     b++;
}
cout<<c<<endl;
return 0;
}

edit & run

Scroll to Top