By GokiSoft.com| 15:48 14/09/2020|
Lập Trình C

Share Code - Tìm hiểu về If, Switch, Loop trong C - Lập trình C



- Nôi dung buổi học
	- Tìm hiểu lại if, else
	- Switch
	- Loop => while, do .. while, for
==============================================
Bài toán:
	Nhập vào 1 ký c
		- c: W => Hiển thị dòng chữ Hello World
		- c: w => Hiển thị dòng chữ Hello World
		- c: G => Hello Gokisoft
		- c: g => Hello Gokisoft
		- c: T => Hello T2008A
		- c: t => Hello T2008A
		- ký khác => Other.
	



#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 c;
	
	printf("\nNhap c: ");
	scanf("%c", &c);
	
//	if (c=='W') printf(" hello world");
//	if (c=='G') printf(" hello gokisoft");
//	if (c=='t') printf(" hello t2008a");
//	else printf(" other");
	//Comment
	//Cach 1 => chuyen code tu flowchart
	/*if (c == 'W') {
		printf("\nHello World!!!");
	} else {
		if(c == 'G') {
			printf("\nHello Gokisoft");
		} else {
			if(c == 'T') {
				printf("\nHello T2008A");
			} else {
				printf("\nOther");
			}
		}
	}*/
	
	//chuyen sang cach 2 => sang
//	if(c == 'W') {
//		printf("\nHello World!!!");
//	} else if (c == 'G') {
//		printf("\nHello Gokisoft");
//	} else if (c == 'T') {
//		printf("\nHello T2008A");
//	} else {
//		printf("\nOther");
//	}
	
	//Cach 3: Su dung switch
	//TH => Menh de dieu kien => chi co 1 var => kieu du lieu bien : int, char
	//swtich case
	switch(c) {
		case 'W':
		case 'w':
			printf("\nHello World!!!");
			break;
		case 'G':
		case 'g':
			printf("\nHello Gokisoft");
			break;
		case 'T':
		case 't':
			printf("\nHello T2008A");
			break;
		default:
			printf("\nOther");
			break;
	}
	
	return 0;
}





#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 cnt = 10;
	printf("\n============ BAI 1 - 1");
	while(cnt < 10) {
		printf("\nScooby");
		cnt++;
	}
	
	cnt = 10;
	printf("\n============ BAI 1 - 2");
	do {
		printf("\nScooby");
		cnt++;
	} while(cnt < 10);
	
	for(cnt=0;cnt<10;cnt++) {
		printf("\nScooby");
	}
	
	//Chuong trinh in cac so tu 1 -> 10.
	cnt = 1;
	while(cnt <= 10) {
		printf("\n%d", cnt);
		cnt++;
	}
	//Chuong trinh in cac so chan tu 1 -> 10.
	cnt = 2;
	while(cnt <= 10) {
		printf("\n%d", cnt);
		cnt+=2;
	}
	//Chuong trinh in cac so chan & khong chia het cho 6 => tu 1 -> 10.
	cnt = 2;
	while(cnt <= 10) {
		if(cnt % 6 == 0) {
			cnt+=2;
			continue;//gioi thieu ve lenh continue => hieu dc y nghia cua continue.
		}
		printf("\n%d", cnt);
		cnt+=2;
	}
	//Chuong trinh in cac so chan & stop khi chia het cho 8 => 1 -> 10
	printf("\n============ BAI 2");
	cnt = 2;
	while(cnt <= 10 && cnt % 8 != 0) {
		printf("\n%d", cnt);
		cnt+=2;
	}
	
	cnt = 2;
	while(cnt <= 10) {
		if(cnt % 8 == 0) {
			break;
		}
		printf("\n%d", cnt);
		cnt+=2;
	}
	
	//Su dung while, do..while, for => Tinh tong cac so chay tu 1 => 10
	
	
	return 0;
}




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

5

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