I. if
- Cấu trúc của câu lệnh if là:if(điều kiện A){- Câu điều kiện if có thể hiểu là nếu mà điều kiện A đúng thì sẽ thực hiện câu lệnh B.
câu lệnh B;
}
- Nếu chỉ có một câu lệnh trong lệnh if thì ta có thể viết:
if(x>2)- Nhưng nếu có 2 câu lệnh trở lên ta phải đặt 2 câu lệnh ấy trong dấu {}
printf("x lon hon 2");
if(x>2){- Bởi vì nếu không đặt vào trong dấu {} thì câu lệnh if sẽ chỉ nhận dòng lệnh đầu tiên và dòng lệnh thứ 2 sẽ được hiều là một lệnh mới ngang hàng với if.
printf("Gia tri cua x la %d", x);
printf("Va x lon hon 2");
}
- Tuy nhiên chúng ta nên đặt câu lệnh trong dấu {} để tiện cho việc sửa chữa và bổ sung thêm câu lệnh.
II. if...else
- Cấu trúc của câu lệnh if...else là:
if(điều kiện A){- Câu điều kiện if...else có thể hiểu là nếu điều kiện A đúng thì thực hiện câu lệnh B, nếu không thì thực hiện câu lệnh C.
câu lệnh B;
}
else{
câu lệnh C;
}
Lưu ý: - Câu lệnh if có thể đứng một mình còn else thì không, else chắc chắn phải đi với if.
- Đến đây có một vấn đề đặt ra đó là nếu một bài toán mà có nhiều điều kiện thì chúng ta sẽ phải viết nhiều dòng if và máy tính sẽ phải chạy hết những dòng lệnh if đó. Để giảm thời gian xử lý của máy tính người ta sẽ dùng lệnh if...else if...else....
if(điều kiện){- Máy tính sẽ xử lý các dòng lệnh và khi tìm được điều kiện đúng thì nó sẽ in ra luôn kết quả và bỏ qua những dòng lệnh còn lại, như vậy thời gian xử lý sẽ nhanh hơn.
}
else if(điều kiện){
}
else if(điều kiện){
}
......
else{
}
III. Bài tập
Bài 1: Write a program that accepts two numbers a and b and checks whether or not a is divisible by b.
***********************************************************
#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 b;
int r;
printf("Nhap a:");
scanf("%d", &a); fflush(stdin);
printf("Nhap b:");
scanf("%d", &b);
r = b % a;
if(r==0){
printf("b chia het cho a");
}
else{
printf("b khong chia het cho a");
}
return 0;
}
***********************************************************
Bài 2: Write a program to accept 2 numbers and tell whether the product of the two numbers is equal to or greater than 1000.
***********************************************************
#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, b, c;
printf("Nhap a:");
scanf("%d", &a);
printf("Nhap b:");
scanf("%d", &b);
c = a + b;
if(c > 1000){
printf("Tong cua hai so la %d lon hon 1000", c);
}
else{
if(c==1000){
printf("Tong cua hai so la 1000");
}
else{
printf("Tong cua hai so la %d nho hon 1000", c);
}
}
return 0;
}
***********************************************************
Bài 3: Write a program to accept 2 numbers. Calculate the difference between the two values. If the difference is equal to any of the values entered, then display the following message :
Difference is equal to value <number of value entered>
If the difference is not equal to any of the values entered, display the following message:
Difference is not equal to any of the values 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 a, b;
printf("Nhap a:");
scanf("%d", &a);
printf("Nhap b:");
scanf("%d", &b);
if(a==b){
printf("a = b");
}
else{
printf("a khac b");
}
return 0;
}
#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, b;
printf("Nhap a:");
scanf("%d", &a);
printf("Nhap b:");
scanf("%d", &b);
if(a==b){
printf("a = b");
}
else{
printf("a khac b");
}
return 0;
}
***********************************************************
Bài 4: Montek company gives allowances to its employees depending on their grade as follows :
Grade Allowance
A 300
B 250
Others 100
Calculate the salary at the end of the month. (Accept Salary and Grade from the user ).
***********************************************************
#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) {
char cap;
int luong;
printf("Luong co ban cua ban la: ");
scanf("%d", &luong);
fflush(stdin);
printf("Cap cua ban la: ");
cap=getchar();
if(cap == 'A')
{
printf("Luong cua ban la : %d",luong + 300);
}
else if(cap == 'B')
{
printf("Luong cua ban la : %d",luong + 250);
}
else
{
printf("Luong cua ban la : %d",luong + 100);
}
return 0;
}
***********************************************************
- Lưu ý: Ở đây A và B chỉ là những kí tự và để chỉ cấp độ của người dùng nên ta sẽ dùng lệnh getchar() thay cho scanf và trong lệnh if A và B phải được đặt trong dấu ''.
Bài 5: Write a program to evaluate the Grade of a student for the following constraints
***********************************************************
#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 mark;
printf("Your mark is ");
scanf("%d", &mark);
if(mark > 7.5)
{
printf("Your grade is A");
}
else if(mark <= 7.5 && mark >= 6.0)
{
printf("Your grade is B");
}
else if(mark <= 6.0 && mark >= 4.5)
{
printf("Your grade is C");
}
else if(mark <= 4.5 && mark >= 3.0)
{
printf("Your grade is D");
}
else
{
printf("Your grade is F");
}
return 0;
}
***********************************************************
0 nhận xét:
Đăng nhận xét