Thứ Sáu, 30 tháng 1, 2015

Bài tập Day3 (More if)

                                                            Điều kiện "If ... Else"
I, Khái niệm
 -Mệnh lênh "If" và mệnh lệnh "else" có thể theo sau như 1 cặp và thực thi những mệnh lệnh có thể có các kết quả khác nhau tùy theo giá trị biến nhập vào.Mệnh lệnh "Else" sẽ chạy khi kết quả ở mệnh lệnh "If" không thỏa mãn.
*>Cú pháp:
-Trong môn C, mệnh lệnh if else thông thường có cú pháp dạng như:

 if(boolean_expression)
{
printf("/* statement(s) will execute if the boolean expression is true */");
}
else
{
      printf("/* statement(s) will execute if the boolean expression is false */");
  }
-if và else
-Nếu kết quả được thỏa mãn đúng ở mệnh lệnh nào, chương trình sẽ kết thúc tại mệnh lệnh đó.

II.Toán tử Else If
- "If" và "else" đi với nhau thành 1 cặp, nhưng trong trường hợp nhiều hơn 2 điều kiện mệnh lệnh, thay vì dùng nhiều cặp "If" "Else" với các ngoặc xoắn, ta dùng lệnh "Else if", thực chất lệnh này hoạt động như 2 lệnh cơ bản ở trên, chương trình sẽ kết thúc ở bất kỳ lệnh nào cho kết quả thỏa mãn.

III.If lồng If
-Khi gặp các trường hợp trong 1 lệnh trong số n lệnh phải bao gồm thêm một hoặc vài trường hợp nữa, ta sẽ dùng cấu trúc If lồng If.
-Thường cấu trúc if lồng nhau càng nhiều cấp độ phức tạp càng cao, chương trình chạy càng chậm và trong lúc lập trình dễ bị nhầm lẫn.Và, các lệnh if…else lồng nhau thì else sẽ luôn luôn kết hợp với if nào chưa có else gần nhất. Vì vậy khi gặp những lệnh if không có else, Bạn phải đặt chúng trong những khối lệnh được bọc bởi cặp ngoặc xoắn {} rõ ràng để tránh bị hiểu sai câu lệnh. 

IV.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.

**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;
int b;
int r;
printf("nhap vao a = ");
scanf("%d", &a);
printf("nhap vao 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.

**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;
int b;
int r;
printf("nhap vao a = ");
scanf("%d", &a);
printf("nhap vao b = ");
scanf("%d", &b);
r = b + a;
if(r == 1000)
{
printf("tong r cua a va b bang 1000");
}
else
{
    printf("tong r cua a va b khac 1000");
    }
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

**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; int b; printf("nhap vao a = "); scanf("%d", &a); printf("nhap vao b = "); scanf("%d", &b); if(a == b) { printf("hai gia tri nhap vao bang nhau"); } else { printf("hai gia tri nhap vao khong bang"); } 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 ).

**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) { char grade; int salary; printf("Your basic salary is "); scanf("%d", &salary); 
fflush(stdin); printf("Your grade is "); grade = getchar();
if(grade == 'A') { printf("salary = %d",salary + 300); } else if(grade == 'B') { printf("salary = %d",salary + 250); } else { printf("salary = %d",salary + 100); } return 0; }

Lưu ý: Vì bài trên yêu cầu nhập trình độ grade ở đinh dạng biến char để tính lương, nhưng khi điền A ở mệnh lệnh If trong ngoặc sẽ báo lỗi chưa khai báo biến A, dĩ nhiên bài này A không phải 1 biến mà là chỉ là 1 thông số dẫn tới kết quả cuối.Vậy ta dùng hàm getchar để nhập 1 ký tự, và vì hàm này nhận cả ký hiệu \n cho nên phải dùng fflush(stdin) trước khi dùng


**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 mark; printf("Your mark is "); scanf("%d", &mark); if(mark > 75) { printf("Your grade is A"); } else if(mark <= 75 && mark >= 60) { printf("Your grade is B"); } else if(mark <= 60 && mark >= 45) { printf("Your grade is C"); } else if(mark <= 45 && mark >= 30) { printf("Your grade is D"); } else if(mark <=30 && mark >= 0) { printf("Your grade is F"); } else printf("Error"); return 0; }


V.Kết Luận
-Qua bài này giúp ta hiểu về khái niệm lệnh điều kiện "If...Else" để giải quyết những câu lệnh có nhiều kết quả tùy vào thông số biến ta gán, cùng vài trường hợp sử dụng cách lệnh "Else If" cho mệnh lệnh nhiều hơn 2 trường hợp ra kết quả

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

Đăng nhận xét