Thứ Ba, 3 tháng 2, 2015

Do...while

Filled under:

Vòng lặp thực hiện lặp lại khối lệnh cho đến khi biểu thức cho gia trị sai.
+ Cú pháp:
do
<khối lệnh>;
while (biểu thức);
Trong đó:
- Biểu thức: có thể là một biểu thức hoặc nhiều biểu thức con. Nếu là nhiều biểu thức con thì cách nhau bởi dấu phẩy (,) và tính đúng sai của biểu thức được quyết định bởi biểu thức con cuối cùng.
- Trong thân do…while (<khối lệnh>) có thể chứa một hoặc nhiều cấu trúc điều khiển khác.
- Trong thân do…while có thể sử dụng lệnh continue để chuyển đến đầu vòng lặp (bỏ qua các câu lệnh còn lại trong thân).
- Muốn thoát khỏi vòng lặp do…while tùy ý có thể dùng các lệnh break, goto, return
Ý nghĩa:
Thực hiện khối lệnh. Kiểm tra biểu thức, nếu đúng thì lặp lại thực hiện khối lệnh, nếu sai thì kết thúc vòng lặp.

Posted By Unknown04:47

For

Filled under:

- Cú pháp tổng quát của vòng lặp for như sau:

for(khởi tạo giá trị cho biến điều khiển; biểu thức điều kiện; biểu thức thay đổi của biến điều khiển){
     câu lệnh;
}

Trong đó:
- Khởi tạo giá trị cho biến điều khiển là một câu lệnh gán giá trị ban đầu cho biến điều khiển trước khi thực hiện vòng lặp. Lệnh này chỉ được thực hiện duy nhất một lần.
- Biểu thức điều kiện là một biểu thức quan hệ, xác định điều kiện thoát cho vòng lặp.
- Biểu thức thay đổi của biến điều khiển xác định biến điều khiển sẽ bị thay đổi như thế nào sau mỗi lần vòng lặp được lặp lại
- Ba phần trên được ngăn cách bằng dấu chấm phảy. Câu lệnh trong thân vòng lặp có thể là một lệnh hoặc nhiều lệnh.

Ví dụ:
int i;
for(i = 1; i <= 10; i++){
    printf("%d", i);
}

- Trong các vòng lặp for, biểu thức điều kiện luôn được kiểm tra ngay khi bắt đầu 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ừ ban đầu điều kiện đó là sai.

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 i, a;
char name[30];
printf("Nhap so tuoi: ");
scanf("%d", &a); fflush(stdin);
printf("Nhap ten: ");
gets(name);
for(i = 1; i <= a; i++){
printf("%s \t", name);
}
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;
for(;x<=30;x=x+2){ printf("%d \t",x); } 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;
for(; x>=0 ;x=x-1){ printf("% d",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:
  1. Get the number.
  2. To begin, set the factorial of the number to be one.
  3. While the number is greater than one
  4. Set the factorial to be the factorial multiplied by the number.
  5. Decrement the number.
  6. 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;

for(;x>=0;x=x-5)
{
     printf("% d \t",x);
}
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;
}

*******************************************

Posted By Unknown04:41

While

Filled under:

- 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:
  1. Get the number.
  2. To begin, set the factorial of the number to be one.
  3. While the number is greater than one
  4. Set the factorial to be the factorial multiplied by the number.
  5. Decrement the number.
  6. 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;
}

*******************************************

Posted By Unknown03:56