- Cấu trúc :
If(expression)statements;
VD: If(x==12)
prinf("x=12");
Dùng để khai báo một lệnh khi một điều kiện nào đó được chấp nhận.
Nếu khai báo một khối lệnh ta dùng cấu trúc :
If(expression)
{ statements1;
statements2;
....
}
Vd :If(x==12)
{ printf("x=%d",x);
}
-Ngoài điều kiện được chấp nhận để chương trình chạy chúng ta cũng có thể khai báo song song với if nếu điều kiện không thỏa mãn bằng cách sử dụng Else.
Cấu trúc
if (condition)
statement1;
else
statement2;
VD:
If(x==10)
printf("x bang 10");
else
printf("x khong bang 10");
II-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.
A không chia hết cho 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,b;
printf("Nhap A:");
scanf("%d",&a);
printf("\nNhap B: ");
scanf("%d",&b);
if (0==a%b)
{
printf("\n\n A chia het cho B");
}
else
{
printf ("\n\n A khong chia het cho B");
}
printf("\n\n\n");
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.
Bài Làm
#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,T;
printf("Nhap A :");
scanf("%d",&a);
printf("Nhap B :");
scanf("%d",&b);
printf("Tong cua A va B =%d",T=a+b );
if(T>1000){printf("\n\nT se lon hon 1000");
}
else{ if(T<1000)
printf("\n\nT se nho hon 1000");
}
return 0;
}
Bài 3
#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);
printf("\n\nTong A Va B =%d",c=a+b);
if(a==c){printf("\n\nTa co A=A-B=C-B=%d=C",c=a-b);}
else{printf("\n\nC se khac A va 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 Bài Làm
#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,T;
char cap='F';
printf("\nNhap Cap : ");
scanf("%c",&cap);
printf("Tien Luong Co Ban =");
scanf("%d",&a);
if(cap=='A'){printf("Tong Luong = %d",T=a+300);
}
else if(cap=='B'){printf("Tong Luong = %d",T=a+250);
}
else if(cap=='C'){printf("Tong Luong = %d",T=a+100);
}
return 0;
}
Bài 5
Write a program to evaluate the Grade of a student for the following constraints
If marks >75 – grade A
If 60< marks < 75 – grade B
If 45<marks<60 – grade C
If 35<marks<45 - grade D
If marks < 35 – grade E
Bài Làm
#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;
printf("Nhap vao so hoc sinh :");
scanf("%d",&a);
if(a>75){printf("Lop A");
}
else if(a<=75&&a>=60){printf("Lop B");
}
else if(a<=60&&a>=45){printf("Lop C");
}
else if(a<=45&&a>=35){printf("Lop D");
}
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;
printf("Nhap vao so hoc sinh :");
scanf("%d",&a);
if(a>75){printf("Lop A");
}
else if(a<=75&&a>=60){printf("Lop B");
}
else if(a<=60&&a>=45){printf("Lop C");
}
else if(a<=45&&a>=35){printf("Lop D");
}
return 0;
}
Kết luận: qua bài ta tìm hiểu được về lệnh điều kiện If else...
đó là bước đầu trong việc tìm hiểu về lập trình,tuy dễ nhưng việc đọc hiểu và giải những bài toán bằng ngôn ngữ lập trình cũng vẫn khá là vấn đề nan giải....








Không có nhận xét nào:
Đăng nhận xét