目录
while循环
while的语法结构:
while(表达式)
循坏语句;
break在while中的作用:while语句中的break是永久终止循环
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 10)
{
if (6 == i)
break;
printf("%d ", i);
i++;
}
return 0;
}
continue在while中的作用:while语句中的continue是continue后面的代码不会执行,而是直接跳转到while语句的判断部分。进入下一次循环的入口判断。
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 10)
{
if (6 == i)
continue;
printf("%d ", i);
i++;
}
return 0;
}
1、打印1-10的数字。
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 10)
{
printf("%d ", i);
i++;
}
return 0;
}
for循环
for 循环的语法结构:
for(表达式1;表达式2;表达式3)
循环语句;
//打印1到10
#include <stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 10; i++)
{
printf("%d ", i);
}
return 0;
}
表达式1为初始化部分,用于初始化循环变量的。
表达式2为条件判断部分,用于判断循环的终止与进行。
表达式3为调整部分,用于循环条件的调整。
break在for中的作用:for语句中的break是跳出for循环
//for语句中break
#include <stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 10; i++)
{
if (i == 8)
break;
printf("%d ", i);
}
return 0;
}
continue在for中的作用:for语句中的continue是跳过continue后面的代码,进行执行表达式3,然后表达式2判断。
//for语句中的continue
#include<stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 10; i++)
{
if (i == 8)
continue;
printf("%d ", i);
}
return 0;
}
do......while 循环
do while循环的语法结构:
do
循环语句;
while (表达式);
#include <stdio.h>
int main()
{
int i = 10;
do
{
printf("%d ", i);
} while (i < 10);
return 0;
}
#include <stdio.h>
int main()
{
int i = 10;
do
{
printf("%d ", i);
i--;
} while (i > 0);
return 0;
}
do while循环中至少执行一次
break在do while 循环中的作用:do while 循环中的break是跳出do while 循环
#include <stdio.h>
int main()
{
int i = 10;
do
{
if (5 == i)
break;
printf("%d ", i);
i--;
} while (i > 0);
return 0;
}
continue在do while 循环中的作用:do while循环中的continue是结束continue后面的代码不执行,进入判断。
#include <stdio.h>
int main()
{
int i = 10;
do
{
if (5 == i)
continue;
printf("%d ", i);
i--;
} while (i > 0);
return 0;
}
1>getchar 和 putchar
getchar : 是读取一个字符,用int的形式,getchar在读取失败时返回EOF(-1),Ctrl+z错误
putchar :是输出一个字符
#include <stdio.h>
int main()
{
int ch = getchar();//读取一个字符
putchar(ch);//输出一个字符
return 0;
}
#include <stdio.h>
int main()
{
int ch = 0;
while ((ch = getchar()) != EOF)
{
putchar(ch);
}
return 0;
}
#include <stdio.h>
int main()
{
char input[20] = { 0 };
printf("请输入密码:>");
scanf("%s", input);//scanf输完之后敲回车相当于\n
int tem = 0;
while ((tem = getchar()) != '\n')//把缓冲区的字符全部提取出去
{
;
}
printf("请确认密码(Y/N):>");
int ch = getchar();
if (ch == 'Y')
printf("确认成功");
else
printf("确认失败");
return 0;
}
#include <stdio.h>
int main()
{
int ch = 0;//若将ch放在此处,则只读取一次。
while ((ch=getchar()) != EOF)//getchar 放在循环之中,判断的时候读取,每次判断每次读取
{
if (ch < '0' || ch > '9')
continue;
putchar(ch);
}
return 0;
}
2>scanf在读取时,先从输入缓冲区中读取字符,若输入缓冲区中没有字符,则用键盘输入,scanf在遇到空格后不再读取。回车 代表字符\n
3>sleep睡眠函数、引用头文件#include <windows.h>
4>system 调用系统cls清屏。
5>数组本身就有地址,不需要取地址。