Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Decision Making and Looping

Similar presentations


Presentation on theme: "Chapter 6 Decision Making and Looping"— Presentation transcript:

1 Chapter 6 Decision Making and Looping
PROGRAMMING IN ANSI C

2 Let's find out the law of such question:
12/2/2018 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+100; printf("sum=%d",sum); } main() { int sum=0; sum=sum+1; sum=sum+2; sum=sum+3; sum=sum+4; sum=sum+5; printf("sum=%d",sum); } Let's find out the law of such question: When i=1,2,3,…,100 do: sum = sum + i; Loop construct!

3 12/2/2018 Flow chart In a looping, a sequence of statements is repetitively executed until the loop test condition can’t be satisfied. A loop statement consists of 2 segments: The loop control statement The loop body end begin i=1, sum=0; i<=100 Y N i=i+1; output: sum sum = sum+i;

4 Chapter 6 In this chapter, we will learn: while statement
12/2/2018 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

5 12/2/2018 while Statement The form of while statement: while ( test condition ) loop body test condition loop body true (not 0) false (0) The test condition is evaluated first, and if it is true, the loop body is executed.

6 12/2/2018 while Statement If the loop body consists of more than one statement, we should use compound statement. Program to calculate the sum from 1 to 100. end begin i=1, sum=0; i<=100 Y N i=i+1; output: sum sum = sum+i; main() { int i=1, sum=0; while ( i <= 100 ) { sum = sum + i; i++; } printf("%d", sum); test condition loop body

7 while Statement Program to calculate the sum from 1 to 100.
12/2/2018 while Statement Program to calculate the sum from 1 to 100. We can use ctrl+break to stop the execution of program. infinite loop end begin i=1, sum=0; i<=100 Y N i=i+1; output: sum sum = sum+i; main() { int i=1, sum=0; while ( i <= 100 ) sum = sum + i; i++; printf("%d", sum); } main() { int i=1, sum=0; while ( i <= 100 ) { sum = sum + i; i++; } printf("%d", sum);

8 while Statement – 2 Segments
12/2/2018 while Statement – 2 Segments while ( test condition ) loop body if (sum==50) printf("i=%d",i); { sum = sum+i; i = i+1; } printf("sum=%d", sum=sum+i); ; statment type examples expression statement printf("sum=%d", sum=sum+i); void statement ; control statement if (sum==50) printf("i=%d",i); compound statement { sum=sum+i; i=i+1; }

9 while Statement – 2 Segments
12/2/2018 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. while ( test condition ) loop body

10 12/2/2018 do...while Statement The form of do...while statement: do loop body while ( test condition ); test condition loop body true false 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.

11 12/2/2018 do...while Statement do...while statement can change to while statement: test condition loop body true false loop body test condition loop body true false

12 12/2/2018 do...while Statement do...while statement can change to while statement: do loop body while ( test condition ); loop body while ( test condition ) loop body

13 12/2/2018 do...while Statement Like while statement, if the loop body consists of more than one statement, we should use compound statement. Program to calculate the sum from 1 to 100. end begin i=1, sum=0; i<=100 Y N i=i+1; output: sum sum = sum+i; main() { int i=1, sum=0; do { sum=sum+i; i++; } while ( i <= 100 ); printf("%d",sum); loop body test condition

14 do...while Statement – 2 Segments
12/2/2018 do...while Statement – 2 Segments do loop body while ( test condition ); 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 this semicolon!

15 while & do...while Statement
12/2/2018 while & do...while Statement Compare while with do...while statement. 101 1 5050 1 5050 101 101 main() { int i, sum=0; scanf ( "%d", &i ); while ( i <= 100 ) { sum = sum + i; i++; } printf ( "%d", sum ); main() { int i, sum=0; scanf ( "%d", &i ); do { sum = sum + i; i++; } while ( i <= 100 ); printf ( "%d", sum ); } while —— the test condition is evaluated first do...while —— the loop body is evaluated first while —— the times of execution of loop body≥0 do……while —— the times of execution of loop body≥1

16 12/2/2018 for Statement exp2 exp1 true false loop body exp3 The form of for 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 can't be omitted. for ( ; ; ) is equivalent to while (1) – infinite loop for statement can be changed to while statement

17 12/2/2018 for Statement The form of for statement: 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 ++; }

18 12/2/2018 for Statement The form 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;

19 for Statement Program to calculate the sum from 1 to 100. main()
12/2/2018 for Statement Program to calculate the sum from 1 to 100. end begin i=1, sum=0; i<=100 Y N i=i+1; output: sum sum = sum+i; main() { int i, sum; sum = 0; for ( i = 1; i <= 100; i++ ) sum = sum + i; printf ( "%d", sum ); } i = 1; for ( ; i <= 100; ) { sum = sum + i; i++; } for ( i = 1; i <= 100 ; sum += i, i++ ); i = 1; for ( ; i <= 100; i++ ) sum = sum + i;

20 O Nesting of Loops external loop
12/2/2018 Nesting of Loops external loop Nesting of loops means: A loop body contains another complete loop construct. These 3 loop constructs can nest each other, without limitation of the layers. for ( ; ; ) { …… do while() } while ( ); } …... for ( ; ; ) { …… do } while ( ); while() } …... O while ( ) { …… do } while ( ); …... } while ( ) { …… } …... do { …… } while ( ); …... internal loop

21 12/2/2018 break Statement Form: break; Function: Exit from the loop containing it, causing this 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.

22 break Statement break; for while do exp1 false false exp exp2 …… true
12/2/2018 break Statement break; exp2 …… break; false true for exp1 exp3 false exp …… break; true while do …… break; exp false true while

23 break Statement Program to calculate the sum from 1 to 100. main() {
12/2/2018 break Statement Program to calculate the sum from 1 to 100. end begin i=1, sum=0; i<=100 Y N i=i+1; output: sum sum = sum+i; main() { int i, sum = 0; for ( i = 1; ; i++ ) { if ( i > 100 ) break; sum = sum + i; } printf ( "%d", sum ); i = 1; for ( ; ; ) { if ( i > 100 ) break; sum = sum + ( i++ ); }

24 continue Statement Form: continue; Function:
12/2/2018 continue Statement Form: continue; Function: Skip the following statements in this loop, causing the test condition of the next loop to be judged. The continue can only be used in loops.

25 continue Statement continue; exp2 …… continue; …... false true for
12/2/2018 continue Statement exp2 …… continue; …... false true for exp1 exp3 continue; true exp false while …… continue; false true do …… continue;…... exp while

26 12/2/2018 continue Statement Output the numbers, between 100 and 200, which can't be divided exactly by 3. main() { int n; for ( n = 100; n <= 200; n++ ) { if ( n % 3 == 0) continue; printf ( "%5d", n ); }

27 12/2/2018 Loops – Program 1 Output the former 40 numbers of the Fibonacci sequence. Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, …… F1 = (n = 1) F2 = (n = 2) Fn = Fn-1 + Fn-2 (n≥3) f1 = 1, f2 = 1 for i = 1 to 20 Output: f1, f2 f1 = f1 + f2 f2 = f2 + f1 f1 f2 ? ? ? f2 f1 f1…

28 Loops – Program 1 main() { long f1 = 1, f2 = 1; int i;
12/2/2018 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 ( "\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 );

29 Loops – Program 2 Judge whether an integer is a prime number or not.
12/2/2018 Loops – Program 2 Judge whether 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. Read in m i=2 when i ≤ k m%i==0? true false break; i = i + 1 i > k m is a prime m isn't a prime k = (int) sqrt(m)

30 12/2/2018 If m can be divided exactly 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 <math.h> main() { int m, i, k; scanf ( "%d", &m ); k = sqrt ( m ); for ( i = 2; i <= k; i++ ) if ( m % i == 0 ) break; if ( i > k ) printf( "%d is a prime number.", m); else printf( "%d is not a prime number.", m); }

31 Loops – Program 2 #include <math.h> main() { int m, i, k;
12/2/2018 Loops – Program 2 #include <math.h> main() { int m, i, k; scanf ( "%d", &m ); k = sqrt ( m ); i = 2; while ( i <= k ) { if( m % i == 0 ) break; i ++; } if ( i > k ) printf( "%d is a prime number.", m); else printf( "%d is not a prime number.", m); } Can't judge 1, 2, 3 ! 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 ); do { if( m % i == 0 ) break; i ++; } while ( i <= k );

32 Loops – Program 3 Output all the primes between 100 and 200.
12/2/2018 Loops – Program 3 Output all the primes between 100 and 200. Try to use while and do…while statement to rewrite this program. #include <math.h> main() { int m, i, k, n = 0; for ( m = 101; m <= 200; m = m + 2 ) { k = sqrt ( m ); for ( i = 2; i <= k; i++ ) if ( m % i == 0 ) break; if ( i > k ) { printf ( "%5d", m ); n = n + 1; } if ( n % 10 == 0) printf ( "\n" ); }

33 12/2/2018 Loops – Program 4 Read in a positive integer, and output it in reversed order. For example: read in 12345, output #include <math.h> main() { int n; printf ( "Input a positive integer:" ); scanf ( "%d", &n ); while ( n != 0 ) { printf ( "%d", n % 10 ); n = n / 10; /* number is decreased by 10 times */ }

34 Loops – Program 5 Output the multiplication table.
12/2/2018 Loops – Program 5 Output the multiplication table. "i" represents the line: (1≤i≤9) "j" represents the column: (1≤j≤i) j i …… 5×5=25 5×4=20 5×3=15 5×2=10 5×1=5 4×4=16 4×3=12 4×2=8 4×1=4 3×3=9 3×2=6 3×1=3 2×2=4 2×1=2 1×1=1 …… 5×5=25 5×4=20 5×3=15 5×2=10 5×1=5 4×4=16 4×3=12 4×2=8 4×1=4 3×3=9 3×2=6 3×1=3 2×2=4 2×1=2 1×1=1

35 Loops – Program 5 main() { int i, j; for ( i = 1; i < 10; i++ )
12/2/2018 Loops – Program 5 main() { int i, j; for ( i = 1; i < 10; i++ ) for ( j = 1; j <= i; j++ ) printf ( "%d*%d=%d\t", j, i, i*j ); } printf ( "\n" ); Try to use other nesting of loops to rewrite this program.

36 Loops – Program 6 Please input a number: Please input a number:
12/2/2018 Loops – Program 6 Please input a number: Please input a number: Read in integer n, and output n factorial (n!). 8 5 8!=-25216 5!=120 8!=40320 0!=120 main() { int n; int m=1; printf ( "Please input a number:" ); scanf ( "%d", &n ); while ( n >= 1 ) { m *= n; n -- ; } printf ( "%d!=%d", n, m ); long printf ( "%d!=", n ); printf ( "%d", m ); printf ( "%ld", m );

37 12/2/2018 goto Statement Used to: Jump unconditionally from one point to another in a program. General forms: goto label; label: statement; …… or …… label: statement; goto label; …… ……

38 goto Statement About label:
12/2/2018 goto Statement About label: A label must be a valid identifier, don’t use an integer as a label. A label can only mark executable statement, and can’t mark declaration statement. A label is unique in a function. Don’t use the same label to mark different statements.

39 if and goto Program to calculate the sum from 1 to 100. main() {
12/2/2018 if and goto Program to calculate the sum from 1 to 100. end begin i=1, sum=0; i<=100 Y N i=i+1; output: sum sum = sum+i; main() { int i=1, sum=0; loop: if ( i <= 100 ) sum += i; i++; goto loop; } printf ( "%d", sum );

40 goto Statement goto Statement can: Form looping with if statement;
12/2/2018 goto Statement goto Statement can: Form looping with if statement; Jump out of looping. while() { …… { goto label; …… } label: statement

41 goto Statement It is a good practice to avoid using goto statement!
12/2/2018 goto Statement It is a good practice to avoid using goto statement! goto statements: Making the compiler generate less efficient code; Making the program logic more complicated; Making the program more unreadable; Even breaking down the whole system.

42 Homework Review Questions P174
12/2/2018 Homework Review Questions P174 6.1, 6.2, 6.9, 6.11~6.13, 6.14(a) (Write down in your exercise book.) Programming Exercises


Download ppt "Chapter 6 Decision Making and Looping"

Similar presentations


Ads by Google