- Cú pháp của vòng lặp while như sau:
while(điều kiện là đúng){
câu lệnh;
}
Trong đó:
- Câu lệnh có thể là rỗng, hay là một lệnh đơn, hay một khối lệnh. Nếu vòng lặp while chứa một tập các lệnh thì chúng phải được đặt trong dấu ngoặc xoắn {}.
- Điều kiện có thể là biểu thức bất kì
- Vòng lặp sẽ được thực hiện lặp đi lặp lại khi điều kiện trên là đúng. Chương trình sẽ chuyển đến thực hiện lệnh tiếp sau vòng lặp khi điều kiện trên là sai.
- Vòng lặp for có thể được sử dụng khi số lần thực hiện vòng lặp đã được xác định trước. Khi số lần lặp không biết trước, ta có thể sử dụng vòng lặp while.
- Ví dụ:
int i = 1;
while(i <= 10){
printf("i = %d", i );
i++;
}
- Cũng giống như vòng lặp for, vòng lặp while kiểm tra điều kiện ngay khi bắt đầu thực hiện vòng lặp. Do đó các lệnh trong thân vòng lặp sẽ không được thực hiện nếu ngay từ đầu điều kiện đó là sai.
- Biểu thức điều kiện trong vòng lặp có thể phức tạp hay đơn giản. Các biến trong biểu thức điều kiện có thể bị thay đổi giá trị trong thân vòng lặp, nhưng cuối cùng điều kiện đó phải sai, nếu không vòng lặp sẽ không bao giờ kết thúc.
Bài tập:
Bài 1: Declare a variable which has the age of the person. Print the user’s name as many times as his age
*******************************************
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int a;
int i = 1;
char name[30];
printf("Nhap so tuoi: ");
scanf("%d", &a); fflush(stdin);
printf("Nhap ten: ");
gets(name);
while(i <= a){
printf("%s \t", name);
i++;
}
return 0;
}
*******************************************
Bài 2: The program displays even numbers from 1 to 30.
*******************************************
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int x=2;
while(x <= 30){
printf("% d",x);
x=x+2;
}
return 0;
}
*******************************************
Bài 3: The program displays numbers from 10 to 0 in the reverse order
*******************************************
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int x=10;
while(x>=0){
printf("% d",x);
x=x--;
}
return 0;
}
*******************************************
Bài 4: The program will accept integers and display them until zero (0) is entered
*******************************************
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int i;
printf("Nhap i: ");
scanf("%d", &i);
for(;i >=0; i--){
printf("%d \t", i);
}
return 0;
}
*******************************************
Bài 5: Find the factorial of a number.
Hint:
Get the number.
To begin, set the factorial of the number to be one.
While the number is greater than one
Set the factorial to be the factorial multiplied by the number.
Decrement the number.
Print out the factorial
*******************************************
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n, i;
int gt = 1;
printf("Nhap n: ");
scanf("%d", &n);
for(i=1; i<=n; i++){
gt=gt*i;
}
printf("Giai thua cua n la: %d", gt);
return 0;
}
*******************************************
Bài 6: Write a program to print the series 100, 95 , 90, 85,………., 5.
*******************************************
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int x=100;
while(x>=0){
printf("% d",x);
x = x -5;
}
return 0;
}
*******************************************
Bài 7: Accept two numbers num1 and num2. Find the sum of all odd numbers between the two
numbers entered .
*******************************************
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int i, a, b;
int x = 0;
printf("Nhap so a: ");
scanf("%d", &a);
printf("Nhap so b: ");
scanf("%d", &b);
if(a<b){for(i=a ; i<= b; i++){
if(i % 2 == 1){
x = x + i;
}
}
printf("Tong: %d", x);
}
else{for(i=b ; i<= a; i++){
if(i % 2 == 1){
x = x + i;
}
}
printf("Tong: %d", x);
}
return 0;
}
*******************************************
Bài 8: Write a program to generate the Fibonacci series. (1,1,2,3,5,8,13,………)
*******************************************
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int a=0,b=1,c=1,i ;
for(a,b,c,i;i<=20;a=b,b=c,c=a+b,i++){
printf("%d",c);
}
return 0;
}
*******************************************