By GokiSoft.com| 14:15 24/11/2021|
Lập Trình C

[Video] Tìm hiểu mệnh đề điều kiên if else & switch trong C - Khóa học lập trình C



#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[]) {
	//Phan 1: Nhap du lieu -> tim hieu kien thuc bo sung
	/**
	int x, y;
	printf("\nNhap x = ");
	scanf("%d", &x);
	
	printf("\nNhap y = ");
	scanf("%d", &y);
	
	printf("\nx = %d, y = %d", x, y);
	
	printf("\nNhap x, y: ");
	scanf("%d%d", &x, &y);//Luu ->%d%d -> ko dc chua bat ky ky tu nao khac -> ngoai %d, %f, %c, %s
	
	printf("\nx = %d, y = %d", x, y);
	
	//Tim hieu cach nhap ky tu bang cach moi
	char c;
	printf("\nNhap c = ");
	//scanf("%c", &c);
	fflush(stdin); fflush(stdout);
	c = getchar();
	
	//printf("\nc = %c", c);
	printf("\nC = ");
	putchar(c);
	
	char s[20];
	printf("\nNhap s = ");
	fflush(stdin); fflush(stdout);
	gets(s);
	
	puts(s);
	*/
	
	//Phan 2: Menh de dieu kien if else
	//Step 1
	/**int num;
	printf("\nNhap num = ");
	scanf("%d", &num);
	
	int r = num % 2;
	
	if(r == 0) {
		//Dieu kien: neu r = 0 -> Dung -> xu ly khoi code day.
		printf("\nNumber is even");
	}
	*/
	
	//Step 2
	/**int num;
	printf("\nNhap num = ");
	scanf("%d", &num);
	
	int r = num % 2;
	
	if(r == 0) {
		//Dieu kien: neu r = 0 -> Dung -> xu ly khoi code day.
		printf("\nNumber is even");
		printf("\nNumber is even");
		printf("\nNumber is even");
	} else {
		//Dieu kien: r != 0 -> Hien thi noi dung duoi day
		printf("\nNumber is odd");
		printf("\nNumber is odd");
		printf("\nNumber is odd");
		printf("\nNumber is odd");
		printf("\nNumber is odd");
		printf("\nNumber is odd");
	}*/
	//Step 3
	int yearWithUs, bizDone;
	
	printf("\nNhap yearWithUs, bizDone: ");
	scanf("%d%d", &yearWithUs, &bizDone);
	
	if(yearWithUs >= 10) {
		//Dieu kien dung
		if(bizDone > 5000000) {
			printf("\nClassified as an MVS");
		} else {
			printf("\nA Little more effort required");
		}
	} else {
		//Dieu kien sai
		printf("\nA Little more effort required");
	}
	
	//Nang cao 1 chut
	if(yearWithUs >= 10 && bizDone > 5000000) {
		printf("\nClassified as an MVS");
	} else {
		printf("\nA Little more effort required");
	}
	
	if(yearWithUs >= 10) {
		//Dieu kien dung
		if(bizDone > 5000000) {
			printf("\nClassified as an MVS");
		} else {
			printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
		}
	} else {
		//Dieu kien sai
		printf("\nA Little more effort required");
	}
	
	if(yearWithUs >= 10 && bizDone > 5000000) {
		printf("\nClassified as an MVS");
	} else if(yearWithUs >= 10) {
		printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
	} else {
		printf("\nA Little more effort required");
	}
	
	//Step 4: Switch
	char c;
	printf("\nNhap c = ");
	fflush(stdin);fflush(stdout);
	scanf("%c", &c);
	
	if(c == 'A') {
		printf("\nHello A");
	} else if(c == 'B') {
		printf("\nHello B");
	} else if(c == 'C') {
		printf("\nHello C");
	} else {
		printf("\nGoodbye!!!");
	}
	
	//TH: c -> int, char -> co the su dung cach sau
	switch(c) {
		case 'A':
			printf("\nHello A");
//			break;//Luu y: phai co break
		case 'B':
			printf("\nHello B");
			break;//Luu y: phai co break
		case 'C':
			printf("\nHello C");
			break;//Luu y: phai co break
		default:
			printf("\nGoodbye!!!");
			break;//Luu y: phai co break
	}
	
	return 0;
}




Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)