C语言编程学习课件 (25).pdf
Programming In CProgramming In C Flow control statement When solving practical problems,we often need to When solving practical problems,we often need to exit a loop early,or we want to skip some exit a loop early,or we want to skip some statements in a loop,so we need some special ways statements in a loop,so we need some special ways to control program flow.to control program flow.break statement continue statement break statement Syntax of break statement:break;Function of break statement:Exit from a switch or a loop.Pay attention to:(1)The break statement can only be used in switch statement or loop statements.(2)The break statement can only be used in switch statement or loop statements.continue statement Syntax of continue statement:continue;Function of break statement:The continue statement only works in loops.It is used to interrupt the current execution of the loop body,that is skipping statements that have not yet been executed in the loop body,and start the next repetition immediately.(1)The continue statement is usually used with the if statement.It can only end the current repetition but cannot terminate the entire loop.(2)If the continue statement is written in a for loop,the increment in the for statement is still executed.Pay attention to:break#include main()int i,n;for(i=1;i=5;i+)printf(Please enter n:);scanf(%d,&n);if(n 0)break;printf(n=%dn,n);printf(Program is over!n);n-10 Running result:Please enter n:10 n=10 Please enter n:6 n=6 Please enter n:-10 Program is over!continue#include main()int i,n;for(i=1;i=5;i+)printf(Please enter n:);scanf(%d,&n);if(n 0)continue;printf(n=%dn,n);printf(Program is over!n);n 30 Running result:Please enter n:10 n=10 Please enter n:-10 Please enter n:20 n=20 Please enter n:-20 Please enter n:30 n=30 Program is over!Question 1 Enter a positive integer m to check whether it is a prime number.The prime numbers are positive integers that can only be divided by 1 and itself.1 is not a prime number while 2 is.#include#includestdio.hstdio.h#include#includemath.hmath.h voidvoid main()main()intint i,m,n;i,m,n;scanf(%d,&m);scanf(%d,&m);n=(int)sqrt(m);n=(int)sqrt(m);forfor(i=2;i(i=2;in)printf(%d is a prime number!(in)printf(%d is a prime number!n,m);n,m);elseelse printf(%d is not a prime number!printf(%d is not a prime number!n,m);n,m);Running result:Question 2 Output a number in the range of 11000 that can be divided by 3,5,7.#include void main()int n;for(n=1;n=1000;n+)if(n%3!=0)continue;if(n%5!=0)continue;if(n%7!=0)continue;printf(%d ,n);printf(n);Running result:Programming In CProgramming In C