By GokiSoft.com| 09:10 11/12/2020|
Lập Trình C

[Share Code] Tìm hiểu mệnh đề điều kiên if, else, switch trong 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[]) {
	//code start
	/**
	//flowchart 1
	int num, r;
	printf("\nNhap num = ");
	scanf("%d", &num);
	
	r = num % 2;
	
	//step 1
//	if (r == 0) {
//		//dieu kien dung
//		printf("\nNumber is even");
//	}
	if(r == 0) {
		//dieu kien dung
		printf("\nNumber is even");
	} else {
		//dieu kien sai (r != 0)
		printf("\nNumber is odd");
	}
	*/
	//flowchart 2
	/*
	int yearsWithUs, bizDone;
	//nhap du lieu
	printf("\nNhap YearWithUs: ");
	scanf("%d", &yearsWithUs);
	printf("\nNhap bizDone: ");
	scanf("%d", &bizDone);
	
	if(yearsWithUs >= 10) {
		//dieu kien dung
		if(bizDone > 5000000) {
			//dieu kien dung
			printf("\nClassified as an MVS");
		} else {
			//dieu kien sai
			printf("\nA Little more effort required");
		}
	} else {
		//dieu kien sai
		printf("\nA Little more effort required");
	}
	//Phuong trinh bac 1: ax + b = 0
	float a, b, x;
	//nhap
	printf("\nNhap a = ");
	scanf("%f", &a);
	printf("\nNhap b = ");
	scanf("%f", &b);
	
	//cach 1
	if(a == 0) {
		if(b == 0) {
			//dieu kien dung
			printf("\nPTVSN");
		} else {
			//dieu kien sai
			printf("\nPTVN");
		}
	} else {
		//dieu kien sai
		x = -b/a;
		printf("\nNghiem x = %f", x);
	}
	//cach2 => rut gon lenh (tim hieu them cach viet moi)
	//a == 0 && b == 0 => PTVNS
	//a == 0 && b != 0 => PTVN
	//khac => x = -b/a
	if(a ==0 && b == 0) {
		//dieu kien dung
		printf("\nPTVSN");
	} else if (a == 0 && b != 0) {
		//dieu kien dung
		printf("\nPTVN");
	} else {
		x = -b/a;
		printf("\nNghiem x = %f", x);
	}
	*/
	//flowchart 3
	//a => int
	//a = 1 => printf: Hello A
	//a = 2 => printf: Hello B
	//a = 3 => printf: Hello C
	//a = 4 => printf: Hello D
	//khac => Other
	int a;
	printf("\nNhap a = ");
	scanf("%d", &a);
	
	if(a == 1){
		printf("\nHello A");
	} else if(a == 2) {
		printf("\nHello B");
	} else if(a == 3) {
		printf("\nHello C");
	} else if(a == 4) {
		printf("\nHello D");
	} else {
		printf("\nOther");
	}
	//cach viet khac nua cho TH nay => switch
	//a co kieu du lieu la: int, char
	switch(a) {
		case 1:
			printf("\nHello A -> switch");
			break;
		case 4:
			printf("\nHello D -> switch");
			break;
		case 2:
			printf("\nHello B -> switch");
			break;
		case 3:
			printf("\nHello C -> switch");
			break;
		default:
			printf("\nOther -> switch");
			break;
	}
	//test voi truong hop khac
	//a = 1 || 3 => Hello AC
	//a = 2 || 4 => Hello BD
	//Other
	switch(a) {
		case 1:
		case 3:
			printf("\nHello AC -> switch");
			break;
		case 2:
		case 4:
			printf("\nHello BD -> switch");
			break;
		default:
			printf("\nOther -> switch");
			break;
	}
	
	//code end
	return 0;
}




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

5

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

TRẦN VĂN ĐIỆP [community,C1812L,C2002L,T2008A]
TRẦN VĂN ĐIỆP

2020-12-11 04:22:56



#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[]) {
	//code loop
//	int cnt = 0;
//	
//	while(cnt < 1000) {
//		printf("\nScooby");
//		cnt++;
//	}
	
	//Test
	//In cac so chay tu 1 -> 10
	printf("So chay tu 1 -> 10\n");
	int i = 1;
	
	while(i <= 10) {
		printf("%d\n", i);
		i++;
	}
	
	printf("\nSo chan chay tu 1 -> 10\n");
	//In cac so chan chay tu 1 -> 10
	i = 1;
	
	while(i <= 10) {
		if(i % 2 == 0) {
			printf("%d\n", i);
		}
		
		i++;
	}
	
	//In cac so chay tu 1 -> 10 => so do ko chia het cho 3
	printf("So chay tu 1 -> 10\n");
	i = 1;
	
	while(i <= 10) {
		if(i % 3 == 0) {
//			i++;
//			continue;
			break;
		}
		printf("%d\n", i);
		i++;
	}
	
	//Tinh tong cac so chay tu 1->10
	int sum = 0;
	i = 0;
	
	while(i<=10) {
		sum += i;
		i++;
	}
	
	printf("\nsum = %d", sum);
	
	return 0;
}