programming in ANSI C-Chapter 6 Decision Making and Looping_第1页
programming in ANSI C-Chapter 6 Decision Making and Looping_第2页
programming in ANSI C-Chapter 6 Decision Making and Looping_第3页
programming in ANSI C-Chapter 6 Decision Making and Looping_第4页
programming in ANSI C-Chapter 6 Decision Making and Looping_第5页
已阅读5页,还剩37页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Chapter 6Decision Making and Looping,PROGRAMMING IN ANSI C,Question,Questions: How do we calculate the sum from 1 to 5? How do we calculate the sum from 1 to 100?,main() int sum=0; sum=sum+1; sum=sum+2; sum=sum+3; sum=sum+4; sum=sum+5; printf(sum=%d,sum); ,main() int sum=0; sum=sum+1; sum=sum+2; sum

2、=sum+3; sum=sum+100; printf(sum=%d,sum); ,Lets find out the law of such question:,When i=1,2,3,100 do: sum = sum + i;,Loop construct!,Flow chart,In a looping, a sequence of statements is repetitively executed until the loop test condition cant be satisfied. A loop statement consists of 2 segments: T

3、he loop control statement The loop body,Chapter 6,In this chapter, we will learn: while statement do.while statement for statement Assisted control statements: break and continue Looping consists of if and goto,while Statement,The form of while statement: while ( test condition ) loop body,The test

4、condition is evaluated first, and if it is true, the loop body is executed.,while Statement,Program to calculate the sum from 1 to 100.,main() int i=1, sum=0; while ( i = 100 ) sum = sum + i; i+; printf(%d, sum); ,If the loop body consists of more than one statement, we should use compound statement

5、.,while Statement,Program to calculate the sum from 1 to 100.,main() int i=1, sum=0; while ( i = 100 ) sum = sum + i; i+; printf(%d, sum); ,infinite loop,We can use ctrl+break to stop the execution of program.,main() int i=1, sum=0; while ( i = 100 ) sum = sum + i; i+; printf(%d, sum); ,while Statem

6、ent 2 Segments,while Statement 2 Segments,The loop body can be any type statement. The loop body should be a single statement or a compound statement. The loop body must contain some statements making the loop tend to end. Avoid infinite loop.,do.while Statement,The form of do.while statement: do lo

7、op body while ( test condition );,The loop body is executed first, and then the test condition is evaluated.,In do.while statement, the loop body is always executed at least once.,do.while Statement,do.while statement can change to while statement:,do.while Statement,do.while statement can change to

8、 while statement:,do loop body while ( test condition );,loop bodywhile ( test condition ) loop body,do.while Statement,Program to calculate the sum from 1 to 100.,main() int i=1, sum=0; do sum=sum+i; i+; while ( i = 100 ); printf(%d,sum); ,Like while statement, if the loop body consists of more tha

9、n one statement, we should use compound statement.,do.while Statement 2 Segments,The loop body can be any type statement. The loop body should be a single statement or a compound statement. The loop body must contain some statements makeing the loop tend to end. Avoid infinite loop.,Pay attention to

10、 this semicolon!,while scanf ( %d, ,main() int i, sum=0; scanf ( %d, ,1 5050,1 5050,101 0,101 101,while the test condition is evaluated first do.while the loop body is evaluated first while the times of execution of loop body0 dowhile the times of execution of loop body1,for Statement,The form of fo

11、r statement: for ( exp1; exp2; exp3 ) loop body,exp1, exp2 and exp3 are any type expressions, and they all can be omitted. But the separator semicolon cant be omitted. for ( ; ; ) is equivalent to while (1) infinite loop for statement can be changed to while statement,for Statement,The form of for s

12、tatement: for ( exp1; exp2; exp3 ) loop body The general application form of for statement: for ( initialization; test condition; increment ) loop body e.g.for( sum = 0, i = 1; i = 100; i + ) sum = sum + i;,is equivalent to: sum = 0; i = 1; while ( i = 100 ) sum = sum + i; i +; ,for Statement,The fo

13、rm of for statement: for ( exp1; exp2; exp3 ) loop body exp1 and exp3 can be comma expression. e.g. for( sum = 0, i = 1; i = 100; sum += i, i+ );,is equivalent to: sum = 0; for( i = 1; i = 100; i+ ) sum += i;,for Statement,Program to calculate the sum from 1 to 100.,main() int i, sum; sum = 0; for (

14、 i = 1; i = 100; i+ ) sum = sum + i; printf ( %d, sum ); ,i = 1; for ( ; i = 100; i+ ) sum = sum + i;,i = 1; for ( ; i = 100; ) sum = sum + i; i+; ,for ( i = 1; i = 100 ; sum += i, i+ );,Nesting of Loops,Nesting of loops means: A loop body contains another complete loop construct. These 3 loop const

15、ructs can nest each other, without limitation of the layers.,while ( ) while ( ) . ,do do while ( ); . while ( );,while ( ) do while ( ); . ,for ( ; ; ) do while ( ); while() . ,for ( ; ; ) do while() while ( ); . ,O,break Statement,Form:break; Function: Exit from the loop containing it, causing thi

16、s loop to be terminated. Exit form the switch statement containing it, causing this switch statement to be terminated.,One break can exit only one single loop, the nearest loop. The break can only be used in a loop or a switch statement.,break Statement,break;,main() int i, sum = 0; for ( i = 1; ; i

17、+ ) if ( i 100 ) break; sum = sum + i; printf ( %d, sum ); ,i = 1; for ( ; ; ) if ( i 100 ) break; sum = sum + ( i+ ); ,break Statement,Program to calculate the sum from 1 to 100.,continue Statement,Form:continue; Function: Skip the following statements in this loop, causing the test condition of th

18、e next loop to be judged.,The continue can only be used in loops.,continue Statement,continue;,continue Statement,Output the numbers, between 100 and 200, which cant be divided exactly by 3.,main() int n; for ( n = 100; n = 200; n+ ) if ( n % 3 = 0) continue; printf ( %5d, n ); ,Loops Program 1,Outp

19、ut the former 40 numbers of the Fibonacci sequence. Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, F1 = 1 (n = 1) F2 = 1 (n = 2) Fn = Fn-1 + Fn-2 (n3),f1 f2 ? ? ?,Loops Program 1,main() long f1 = 1, f2 = 1; int i; for ( i = 1; i = 20; i+ ) printf ( %12ld %12ld, f1, f2 ); if ( i % 2 = 0 ) printf (

20、 n ); f1 = f1 + f2; f2 = f2 + f1; ,i=1; while ( i = 20 ) printf ( %12ld %12ld, f1, f2 ); if ( i % 2 = 0 ) printf( n ); f1 = f1 + f2; f2 = f2 + f1; i +; ,i=1; do printf ( %12ld %12ld, f1, f2 ); if ( i % 2 = 0 ) printf ( n ); f1 = f1 + f2; f2 = f2 + f1; i +; while ( i = 20 );,Loops Program 2,Judge whe

21、ther an integer is a prime number or not. Make m be divided by all the numbers between 2 and the square root of m. If m can be divided exactly by a certain number in those, m is not a prime number, otherwise it is.,Loops Program 2,#include main() int m, i, k; scanf ( %d, ,If m can be divided exactly

22、 by the number between 2 and sqrt(m), m is not a prime number, so the loop will be terminated by break statement, and at that time, i must be less or equal to k. Otherwise m is a prime number, and after the last loop, i is equal to k+1.,Loops Program 2,#include main() int m, i, k; scanf ( %d, ,do if

23、( m % i = 0 ) break; i +; while ( i = k );,for ( i = 2; i = k; i+ ) if ( m % i = 0 ) printf ( %d is not a prime number, m ); break; else if ( i = k ) printf ( %d is a prime number, m );,Cant judge 1, 2, 3 !,Loops Program 3,Output all the primes between 100 and 200.,#include main() int m, i, k, n = 0

24、; for ( m = 101; m k ) printf ( %5d, m ); n = n + 1; if ( n % 10 = 0)printf ( n ); ,Try to use while and dowhile statement to rewrite this program.,Loops Program 4,Read in a positive integer, and output it in reversed order. For example: read in 12345, output 54321.,#include main() int n; printf ( I

25、nput a positive integer: ); scanf ( %d, /* number is decreased by 10 times */ ,Loops Program 5,Output the multiplication table.,i represents the line: (1i9) j represents the column: (1ji),Loops Program 5,main() int i, j; for ( i = 1; i 10; i+ ) for ( j = 1; j = i; j+ ) printf ( %d*%d=%dt, j, i, i*j

26、); printf ( n ); ,Try to use other nesting of loops to rewrite this program.,Loops Program 6,Read in integer n, and output n factorial (n!).,main() int n; int m=1; printf ( Please input a number: ); scanf ( %d, ,Please input a number:,5,printf ( %d!=, n );,printf ( %d, m );,0!=120,5!=120,Please input a number:,8,8!=-25216,long,printf ( %ld, m );,8!=40320,goto Statement,Used to: Jump unconditionally from one point to another in a program. General forms: goto label; label: statement; or label: statem

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论