什么叫语句?
一个分号隔开的就是一条语句
printf("这是一条语句");
分支语句(选择语句)
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int age = 10;
if (age < 18)
{
printf("未成年\n");
printf("不能谈恋爱");
}
else
{
if (age >= 18 && age < 28)
printf("青年\n");
else if (age >= 28 && age < 58)
printf("壮年\n");
else if (age >= 58 && age < 90)
printf("老年\n");
else
printf("高寿\n");
}
}
if (conditifion){
return x;
}
return y;
if (conditifion)
{
return x;
}
else
{
return y;
}
#include<stdio.h>
int main()
{
int num = 4;
if (num = 5)
printf("hehhehe\n");
return 0;
}
#include<stdio.h>
int main()
{
int num = 4;
if (5==num)
printf("hehhehe\n");
return 0;
}
输出100以内的奇数
#include<stdio.h>
int main()
{
int i = 1;
while (i <= 100)
{
if (1 == i % 2)
printf(" %d", i);
i++;
}
return 0;
}
#include<stdio.h>
int main()
{
int i= 1;
while (i <= 100)
{
printf("%d ", i);
i += 2;
}
return 0;
}
#include<stdio.h>
int main()
{
int day = 0;
scanf("%d", &day);
switch (day)
{
case 1:
printf("星期一\n");
break;
case 2:
printf("星期二\n");
break;
case 3:
printf("星期三\n");
break;
case 4:
printf("星期四\n");
break;
case 5:
printf("星期五\n");
break;
case 6:
printf("星期六\n");
break;
case 7:
printf("星期日\n");
break;
}
return 0;
}
不是case 后面必须跟break
#include<stdio.h>
int main()
{
int day = 0;
scanf("%d", &day);
switch (day)
{
case 1:
case 2:
case 3:
case 4:
case 5:
printf("工作日\n");
break;
case 6:
case 7:
printf("休息日\n");
break;
default:
printf("输入错误\n");
}
return 0;
}
#include<stdio.h>
int main()
{
int n = 1;
int m = 2;
switch (n)
{
case 1:
m++;
case 2:
n++;
case 3:
switch (n)
{
case 1:
n++;
case 2:
m++;
n++;
break;
}
case 4:
m++;
break;
default:
break;
}
printf("m= %d n= %d\n", m, n);
return 0;
}
上面的输出结果是什么?
答案: m=5 n=3
要点注意:没有遇到break , case是接着往下走的
###循环语句
####while循环
#include<stdio.h>
int main()
{
int i = 0;
while (i <= 10)
{
printf("%d ", i);
i++;
}
return 0;
}
while循环中break的作用:
在循环中,只要遇到break,就停止后期的所有的循环,直接终止循环,所以:while中的break是永久终止循环的。
#include<stdio.h>
int main()
{
int i = 0;
while (i <= 10)
{
if (5 == i)
break;
printf("%d ", i);
i++;
}
return 0;
}
while循环中continue的作用:
continue是用于终止本次循环的,也就是本次循环中continue后边的代码不会再执行,而是直接跳转到while语句的判断部分,进行下一次循环的入口判断。
#include<stdio.h>
int main()
{
int i = 0;
while (i <= 10)
{
i++;
if (5 == i)
continue;
printf("%d ", i);
}
return 0;
}
while循环的应用举例:
#include<stdio.h>
int main()
{
int ret = 0;
int ch = 0;
char password[20] = { 0 };
printf("请输入密码:>");
scanf("%s", password);
printf("请确认(Y/N):>");
while ((ch = getchar()) != '\n')
{
;
}
ret = getchar();
if (ret == 'Y')
{
printf("确认成功\n");
}
else
{
printf("放弃确认\n");
}
return 0;
}
#include<stdio.h>
int main()
{
int ch = 0;
while ((ch = getchar()) != EOF)
{
if (ch<'0' || ch>'9')
continue;
putchar(ch);
}
return 0;
}
for循环
for(初始化赋值如i=1;判断条件如i<=10;调整如i++)
{
执行语句如 printf("%d ",i);
}
#include<stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 10; i++)
{
printf("%d ", i);
}
return 0;
}
#include<stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 10; i++)
{
if (i == 5)
break;
printf("%d ", i);
}
return 0;
}
#include<stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 10; i++)
{
if (i == 5)
continue;
printf("%d ", i);
}
return 0;
}
- continue在for循环和while循环的区别
#include<stdio.h>
int main()
{
int i = 0;
while (i <= 10)
{
if (i == 5)
continue;
printf("%d ", i);
i++;
}
return 0;
}
区别就在于while循环中对i的调整是在循环体中靠后的位置,continue会将i的调整给跳过,而for循环不会这样。
#include<stdio.h>
int main()
{
int i = 0;
for (;;)
{
printf("hehhe\n");
}
return 0;
}
不建议初学就进行省略
#include<stdio.h>
int main()
{
int i = 0;
int j = 0;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
printf("%d \n",j);
}
return 0;
}
#include<stdio.h>
int main()
{
int i = 0;
int j = 0;
for (; i < 10; i++)
{
for (; j < 10; j++)
printf("%d \n",j);
}
return 0;
}
进入外循环,i=1,进入内循环,此时j=10,不满足,再跳到外循环。。。
#include<stdio.h>
int main()
{
int i = 0;
int k = 0;
for (i = 0, k = 0; k = 0; i++, k++)
k++;
return 0;
}
答案:0次
解:i,k都赋值为0;for循环,初始化赋值i=0,k=0;判断(赋值k=0(0为假))为假,不进入循环
do while循环
#include<stdio.h>
int main()
{
int i = 1;
do
{
printf("%d ", i);
i++;
} while (i <= 10);
return 0;
}
#include<stdio.h>
int main()
{
int i = 1;
do
{
if (i == 5)
break;
printf("%d ", i);
i++;
} while (i <= 10);
return 0;
}
#include<stdio.h>
int main()
{
int i = 1;
do
{
if (i == 5)
continue;
printf("%d ", i);
i++;
} while (i <= 10);
return 0;
}
练习
1.计算n的阶乘
#include<stdio.h>
int main()
{
int i = 0;
int n = 0;
int ret = 1;
scanf("%d", &n);
for (i = 1; i<=n; i++)
{
ret = ret*i;
}
printf("ret= %d\n", ret);
return 0;
}
- 1!+2!+3!+…+10!
#include<stdio.h>
int main()
{
int i = 0;
int ret = 1;
int n = 0;
int sum = 0;
for (n=1;n<=10;n++)
{
ret = 1;
for (i=1;i<=n;i++)
{
ret = ret*i;
}
sum = ret + sum;
}
printf("%d\n", sum);
return 0;
}
上面的代码很明显是效率不高的,可以优化的(比如说算4的阶乘,是1* 2 * 3 * 4,算5的阶乘,也是1* 2 *3 * 4 *5,这样每次都需要从一开始成,显然,有冗余的部分)
#include<stdio.h>
int main()
{
int i = 0;
int sum = 0;
int ret = 1;
for (i=1;i<=10;i++)
{
ret = ret*i;
sum = sum + ret;
}
printf("sum= %d", sum);
return 0;
}
3.顺序数组中查找指定值(二分查找/折半查找)
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6,7,8,9,10};
int k = 17;
int sz = sizeof(arr) / sizeof(arr[0]);
int left = 0;
int right = sz - 1;
while (left<=right)
{
int mid = (left + right) / 2;
if (arr[mid] > k)
{
right = mid - 1;
}
else if (arr[mid] < k)
{
left = mid + 1;
}
else
{
printf("找到了,下标是:%d\n", mid);
break;
}
}
if (left > right)
{
printf("找不到\n");
}
return 0;
}
4.多个字符从两端移动,向中间汇聚。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<stdlib.h>
int main()
{
char arr1[] = "welcome to bit!!!!!!";
char arr2[] = "####################";
int left = 0;
int right = strlen(arr1) - 1;
while (left<=right)
{
arr2[left] = arr1[left];
arr2[right] = arr1[right];
printf("%s\n", arr2);
Sleep(1000);
system("cls");
left++;
right--;
}
printf("welcome to bit!!!!!!\n");
return 0;
}
5.模拟用户登录,三次登录机会
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{
char password[20] = { 0 };
int i = 0;
for (i = 0; i < 3; i++)
{
printf("清输入密码:>");
scanf("%s", password);
if (strcmp(password,"123456")==0)
{
printf("登录成功\n");
break;
}
else
{
printf("密码错误\n");
}
}
if (3 == i)
{
printf("三次密码均错误,退出程序\n");
}
return 0;
}
|