当前位置:网站首页>C language -- while statement, dowhile statement, for loop and loop structure, break statement and continue statement
C language -- while statement, dowhile statement, for loop and loop structure, break statement and continue statement
2022-07-27 02:22:00 【Lydialyy】
Catalog
while And dowhile The difference between sentences :
3、 ... and 、for Statements and loop structures
3. Write a program : Judge whether a number is prime
4.for Flexible use of sentences
6. Print the multiplication table
Four 、break Statement and continue sentence
One 、while sentence
while( expression )
The loop body 
1. Code practice one : use C Language computing 1+2+3+……+100 Result
The code is as follows :
#include <stdio.h>
int main()
{
int i = 1,sum = 0;
while(i <= 100)
{
sum = sum + i;
i = i + 1;
}
printf(" The result is :%d\n",sum);
return 0;
}Running results :

getchar function
(1) Function summary :getchar Function from standard input stream (stdin) Get the next character in . Equivalent to calling getc(stdin) function .
(2) The function prototype :
#include <stdio.h>
…
int getchar()(void);(3) Return value :
If the function call succeeds , Return the obtained characters ( Use integer to express its ASCII code )
Return value if yes EOF, Indicates that the function call failed ;
- If the standard input stream is at the end , This function returns EOF, And set the end flag of the standard input stream .
- If there are other mistakes , This function also returns EOF, And set the error flag instead .
2. Code Practice II : Count the number of characters in a line of English sentences input from the keyboard
The code is as follows :
#include <stdio.h>
int main()
{
int count = 0;
printf(" Please enter a line of English characters :\n");
while(getchar() != '\n')
{
count = count + 1;
}
printf(" You have entered a total of %d Characters \n",count);
return 0;
}Running results :

Two 、dowhile sentence
do
The loop body
while( expression );
Practical application , Such as : Verify that the user password is correct
- If use while sentence , The flow chart is as follows :

If use dowhile sentence , The flow chart is as follows :

while And dowhile The difference between sentences :

3、 ... and 、for Statements and loop structures
1. form
for( expression 1; expression 2; expression 3)
The loop body The three expressions are separated by semicolons , among :
- expression 1 Is a loop initialization expression
- expression 2 It's a cyclic conditional expression
- expression 3 Is a loop adjustment expression
2. Print 10 Time LoveC
The code is as follows :
#include <stdio.h>
int main()
{
int count;
for(count = 0;count < 10;count++)
{
printf("LoveC!\n");
}
return 0;
}Running results :

3. Write a program : Judge whether a number is prime
Tips :
prime number : In more than 1 Of the natural number , except 1 And the number itself , A number that cannot be divided by other natural numbers .
One of the ways to find prime numbers : Iterative test from 2 To num/2 Whether all integers can be divisible (num Is the integer to be tested ), If there is no integer that can be divisible , So it's prime .
The code is as follows :
#include <stdio.h>
int main()
{
int i,num;
_Bool flag = 1;
printf(" Please enter an integer :\n");
scanf("%d",&num);
for(i = 2;i < num / 2;i++)
{
if(num % i == 0)
{
flag = 0;
}
}
if(flag)
{
printf("%d It's a prime number !\n",num);
}
else
{
printf("%d It's not a prime number !\n",num);
}
return 0;
}Running results :

4.for Flexible use of sentences
(1) expression 1, expression 2 And expressions 3 It can be omitted as needed ( But the semicolon cannot be omitted ):
- for( ; expression 2 ; expression 3)
- for( expression 1 ; expression 2 ; )
- for( expression 1 ; ; )
- for( ; ; )
- ……
(2) expression 1 And expressions 3 It can be a simple expression , It can also be a comma expression ( That is, separate multiple expressions with commas )
The code for :
#include <stdio.h>
int main()
{
int i,j;
for(i = 0,j = 10;i < j; i++,j--)
{
printf("%d\n",i);
}
return 0;
}Running results :

Add :C99 The new standard
C99 Allow in for Expression of statement 1 Define variables in , Such as :
for (int i = 0; i < 100; i++)
That is, the code of the above example can be written as :
#include <stdio.h>
int main()
{
for(int i = 0,j = 10;i < j; i++,j--)
{
printf("%d\n",i);
}
return 0;
}5. A nested loop
The code for :
#include <stdio.h>
int main()
{
int i,j;
for(i = 0;i < 3;i++)
{
for(j = 0;j < 3;j++)
{
printf("i = %d,j = %d\n",i,j);
}
}
return 0;
}Running results :

6. Print the multiplication table
The code is as follows :
#include <stdio.h>
int main()
{
int i,j;
for(i = 1;i <= 9;i++)
{
for(j = 1;j <= i;j++)
{
printf("%d*%d=%-2d ",i,j,i*j);
}
putchar('\n');
}
return 0;
}Running results :

Four 、break Statement and continue sentence
1.break sentence
Be responsible for jumping out of the loop of the statement at its level , If you want to jump out of the outer cycle, you need to arrange another break sentence .
It is also the example of the prime number mentioned above , If the number entered is 100000000, I added break After the statement , The efficiency of implementation is greatly increased , The code is as follows :
#include <stdio.h>
int main()
{
long long int i,num;
_Bool flag = 1;
printf(" Please enter an integer :\n");
scanf("%lld",&num);
for(i = 2;i < num / 2;i++)
{
if(num % i == 0)
{
flag = 0;
break;
}
}
if(flag)
{
printf("%d It's a prime number !\n",num);
}
else
{
printf("%d It's not a prime number !\n",num);
}
printf("i = %lld\n",i);
return 0;
}Running results :

so , Just execute 2 You can get the result once , But if you will break Comment out the statement , The execution efficiency is greatly reduced , The operation results are as follows :

2.continue sentence
When executed continue When the sentence is , The rest of the loop body will be ignored , Go straight to the next cycle .
The code for :
#include <stdio.h>
int main()
{
int ch;
while((ch = getchar()) != '\n')
{
if(ch == 'C')
{
continue;
}
putchar(ch);
}
putchar('\n');
return 0;
}Running results :

Be careful
- For nested statements ,break Statement and continue Statements can only act on one layer of loops !
- for Statement and while Statements are not completely equivalent , They are different in execution : appear continue When the sentence is ,while Is to skip the statement after the loop body , Go straight to the next cycle .
边栏推荐
- (超详尽版,不懂随时评论)Codeforces Round #804 (Div. 2)C The Third Problem
- 广域网技术实验
- C language implementation of the small game [sanziqi] Notes detailed logic clear, come and have a look!!
- Lora illumination sensor node data acquisition
- RS-485 bus communication application
- HCIA静态路由基础模拟实验
- Dynamic routing ofps protocol configuration
- TCP的三次握手与四次挥手(简述)
- C语言——赋值运算符、复合的赋值运算符、自增自减运算符、逗号运算符、条件运算符、goto语句、注释
- ACM mode input and output exercise
猜你喜欢

有趣的C语言

ACM mode input and output exercise

Esp8266wi fi data communication

About unsafe problems such as fopen and strError encountered in vs2022 or advanced version running environment

数字集成电路:MOS管器件章(一)

全连MGRE与星型拓扑MGRE

Codeforces Round #807 (Div. 2), problem: (C) Mark and His Unfinished Essay

最新京东短信登录+傻妞机器人保姆级部署教程(2022/7/24)

7.16 written examination of Duoyi network

Static comprehensive experiment (comprehensive exercise of static route, loopback interface, default route, empty interface, floating static)
随机推荐
RS-485总线通信应用
Golang implements TCP chat room
WAN technology experiment
OSPF静态大实验
C语言——关系运算符和逻辑运算符、if语句、switch语句、分支结构的嵌套
Esp8266wi fi data communication
CAN总线通信应用
睡不着时闭眼躺着,到底有没有用?
HCIA Basics (1)
Explain exi interrupt through the counting experiment of infrared sensor
js中的数组方法和循环
6.28 flush written test
ospf协议概述以及基础概念
Ogeek meetup phase I, together with cubefs, is hot
Codeforces Round #807 (Div. 2), problem: (C) Mark and His Unfinished Essay
数字集成电路:MOS管器件章(二)
C language - first program, print, variables and constants
First knowledge of Web Design
HCIP第一天静态路由综合实验
Codeforces Round #807 (Div. 2), problem: (C) Mark and His Unfinished Essay