Cấu trúc lặp while: Chức năng của nó đơn giản chỉ là lặp lại [Khối lệnh] khi điều kiện biểu thức điều khiển] còn thoả mãn. Cú pháp:
while (biểu thức điều kiện)
{
Khối lệnh;
}
while (biểu thức điều kiện) { Khối lệnh; }
while (biểu thức điều kiện)
{
    Khối lệnh;
}
Trong đó: -[biểu thức điểu khiển] là biểu thức logic. -[Khối lệnh] còn thực hiện khi[biểu thức điều khiển] còn đúng. - Trong[khối lệnh] cần có sự tác động để[biểu thức điều khiển] sai.

Ví dụ 1

  • Yêu cầu: Tính tổng các số từ 1 đến n, với n nhập vào từ bàn phím.
  • Code
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
int n;
// nhap n
cout<<"\n n = ";
cin>>n;
// tinh tong
int s = 0,i = 0;
while(i<=n){
s = s+i;
i++;
}
// in ket qua
cout<<"\n Tong = "<<s;
return 0;
}
#include <iostream> #include <conio.h> using namespace std; int main() { int n; // nhap n cout<<"\n n = "; cin>>n; // tinh tong int s = 0,i = 0; while(i<=n){ s = s+i; i++; } // in ket qua cout<<"\n Tong = "<<s; return 0; }
#include <iostream>
#include <conio.h>

using namespace std;

int main() {
   int n;
   // nhap n
   cout<<"\n n = ";
   cin>>n;
   // tinh tong
   int s = 0,i = 0;
   while(i<=n){
       s = s+i;
       i++;
   }
   // in ket qua
   cout<<"\n Tong = "<<s;

   return 0;  
}

Ví dụ 2

  • Yêu cầu: Đếm số chẵn nhỏ hơn n, với n nhập vào từ bàn phím.
  • Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
int n;
// nhap n
cout<<"\n n = ";
cin>>n;
// dem so chan
int dem = 0,i = 0;
while (i<=n){
if (i%2==0) dem++;
i++;
}
// in ket qua
cout<<"\n Ket qua = "<<dem;
return 0;
}
#include <iostream> #include <conio.h> using namespace std; int main() { int n; // nhap n cout<<"\n n = "; cin>>n; // dem so chan int dem = 0,i = 0; while (i<=n){ if (i%2==0) dem++; i++; } // in ket qua cout<<"\n Ket qua = "<<dem; return 0; }
#include <iostream>
#include <conio.h>

using namespace std;

int main() {
   int n;
   // nhap n
   cout<<"\n n = ";
   cin>>n;
   // dem so chan
   int dem = 0,i = 0;
   while (i<=n){
     if (i%2==0) dem++;
     i++;
   }
   // in ket qua
   cout<<"\n Ket qua = "<<dem;

   return 0;  
}

Ví dụ 3

  • Yêu cầu: In ra màn hình tất cả số nguyên gồm 4 chữ sao cho tổng các chữ số bằng 10.
  • Code
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
cout<<"In ket qua: ";
int i = 1000;
while (i<=9999)
{
if(i%10+(i/10)%10+(i/100)%10+i/1000==10)
cout<<i<<";\t";
i++;
}
return 0;
}
#include <iostream> #include <conio.h> using namespace std; int main() { cout<<"In ket qua: "; int i = 1000; while (i<=9999) { if(i%10+(i/10)%10+(i/100)%10+i/1000==10) cout<<i<<";\t"; i++; } return 0; }
#include <iostream>
#include <conio.h>

using namespace std;

int main() {
   cout<<"In ket qua: ";
   int i = 1000;
   while (i<=9999)
   {
     if(i%10+(i/10)%10+(i/100)%10+i/1000==10)
       cout<<i<<";\t";
     i++;
   }

   return 0;
}

Ví dụ 4

  • Yêu cầu: In ra màn hình bảng cửu chương.
  • Code
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
cout<<"In bang cuu chuong: \n";
int i = 1;
while (i<=9)
{
int j = 1;
while (j<9){
cout<<i<<"x"<<j<<"= "<<i*j<<";\t";
j++;
}
cout<<"\n";
i++;
}
return 0;
}
#include <iostream> #include <conio.h> using namespace std; int main() { cout<<"In bang cuu chuong: \n"; int i = 1; while (i<=9) { int j = 1; while (j<9){ cout<<i<<"x"<<j<<"= "<<i*j<<";\t"; j++; } cout<<"\n"; i++; } return 0; }
#include <iostream>
#include <conio.h>

using namespace std;

int main() {
   cout<<"In bang cuu chuong: \n";
   int i = 1;
   while (i<=9)
   {
    int j = 1;
    while (j<9){
     cout<<i<<"x"<<j<<"= "<<i*j<<";\t";
     j++;
    }
    cout<<"\n";
    i++;
   }

   return 0;  
}

Ví dụ 5

#include <iostream>
#include <conio.h>
using namespace std;
int main() {
cout<<"In so nguyen to nho hon 1000: \n";
int i = 1;
while (i<=1000)
{
int test = 0,j = 2;
while (j<i){
if(i%j==0) {
test = 1;
break;
}
j++;
}
if(test==0) cout<<i<<" ; ";
i++;
}
return 0;
}
#include <iostream> #include <conio.h> using namespace std; int main() { cout<<"In so nguyen to nho hon 1000: \n"; int i = 1; while (i<=1000) { int test = 0,j = 2; while (j<i){ if(i%j==0) { test = 1; break; } j++; } if(test==0) cout<<i<<" ; "; i++; } return 0; }
#include <iostream>
#include <conio.h>

using namespace std;

int main() {
   cout<<"In so nguyen to nho hon 1000: \n";
   int i = 1;
   while (i<=1000)
   {
     int test = 0,j = 2;
       while (j<i){
       if(i%j==0) {
         test = 1;
         break;
       }
       j++;
     }
     if(test==0) cout<<i<<" ; ";
     i++;
   }

   return 0;
}