C语言学习四流程控制if条件选择、for循环和强制类型转换

# include <stdio.h>

int main(void)
{
	int i, j;

	scanf("%d %d", &i, &j);

	if (i > j)
		printf("i大于j\n");
	else
		printf("i小于j\n");

	return 0;

}

 

if..else if...else示例

# include <stdio.h>

int main(void)
{
	float score; //score 分数

	printf("请输入您的考试成绩:");
	scanf("%f", &score);

	if (score > 100)
		printf("这是做梦!\n");
	else if (score >= 90)
		printf("优秀!\n");
	else if (score >= 80)
		printf("良好!\n");
	else if (score >= 60)
		printf("及格!\n");
	else if (score >= 0 )
		printf("不及格!继续努力!\n");
	else
		printf("输入的分数过低,不要如此自卑!\n");

	return 0;
}

 

循环示例:

/*
	2013年3月12日15:45:09
	1+2+3+4+.......+100
*/
# include <stdio.h>

int main(void)
{
	
	int i;
	int sum = 0;

	for (i=1; i<=10; ++i)
		sum += i;

	printf("sum = %d\n", sum);

	return 0;
}

 

示例:

# include <stdio.h>

int main(void)
{

	int i;
	int sum = 0;

	for (i=3; i<=10; ++i)
	{
		if (i%3 == 0)
			sum = sum +  i;
		printf("sum = %d\n", sum);
	}
	return 0;
}

 

强制类型转换示例:

# include <stdio.h>

int main(void)
{

	int i;
	float sum = 0;

	for (i=1; i<=100; ++i)
	{
		sum += 1/(float)(i);  //强制类型转换
	}
	printf("sum = %f\n", sum);

	return 0;
}

 

你可能感兴趣的:(c)