Presentation is loading. Please wait.

Presentation is loading. Please wait.

The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x<0 y= 0 x=0 y= 1 x>0 B. y= 0 x<0 y= -1 x=0 y= 1 x>0 C. y= 1 x<0.

Similar presentations


Presentation on theme: "The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x<0 y= 0 x=0 y= 1 x>0 B. y= 0 x<0 y= -1 x=0 y= 1 x>0 C. y= 1 x<0."— Presentation transcript:

1 The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x<0 y= 0 x=0 y= 1 x>0 B. y= 0 x<0 y= -1 x=0 y= 1 x>0 C. y= 1 x<0 y= -1 x=0 y= 0 x>0 D. y= -1 x<0 y= 1 x=0 y= 0 x>0

2 What is the output of the following statements int x=10, y=20, z=30; if ( x > y ) z = x; x = y; y = z; A.10,20,30 B.20,30,30 C.20,30,10 D.20,30,20

3 What is the function of the following statements main() { int x, y; printf (“Input x,y:”); scanf(“%d %d”,&x,&y); if( x % y ) printf (“No\n”); else printf (“Yes\n”); }

4 Chapter6 Decision Making and Looping Outline 6.1Introduction 6.2 The while Statement 6.3The do Statement 6.4The for Statement 6.5 Jumps in Loops

5 Objectives ♣To master the use of while, for, do loops. ♣To be able to choose the correct type of loop for a given problem. ♣To be able to construct nested loops. ♣To master the statements of break and continue ♣To master some algorithms

6 6.1 Introduction sum = 0; n = 1; loop: sum = sum + n*n; if ( n = =10 ) goto print; else { n = n+1; goto loop; } print: printf (“sum=%d”,sum);

7 6.1 Introduction ♣Two segments of a loop ♥body of the loop ♥control statement ♣Depending on the position of the control statement in the loop, a control structure may be classified ♥entry-controlled loop ♥exit-controlled loop

8 flow chart of Entry control loop False True test condition ? Body of the loop Entry

9 Flow chart of exit control loop Entry False True test condition ? Body of the loop

10 6.1 Introduction ♣ A looping process, in general, would include the following four steps: ♥ (1) Setting and initialization of a condition variable. ♥ (2) Execution of the statements in the loop. ♥ (3) Test for a specified value of the condition variable for execution of the loop. ♥ (4) Incrementing or updating the condition variable.

11 6.1 Introduction ♣Counter-controlled loops: ♥We know in advance exactly how many times the loop will be executed. ♥We use a counter as control variable. The counter must be initialized before loop, tested and updated within the loop. ♣Sentinel-controlled loops ♥A special value called sentinel value is used to change the loop control expression from true to false.

12 6.1 Introduction ♣ The C language provides for three constructs for performing loop operations. They are: ♣ (1) The while statement. ♣ (2) The do statement. ♣ (3) The for statement.

13 6.2 The while statement ♣The basic format of the while statement is while ( test condition) { body of the loop } ♣The while is an entry-controlled loop statement.

14 6.2 The while statement ♣Evaluate the sum of the square of 1 to 10 with while statement ♥flow chart ♥program ♣Input a character from the keyboard until the character is equal to ‘Y’ character=‘ ‘; /* character=getchar(); */ while (character != ‘Y’) { character=getchar(); putchar(character); }

15 ♣The segment is equivalent the following statements while ((character=getchar()) != ‘Y’) { putchar(character); } character=‘ ‘; /* character=getchar(); */ while (character != ‘Y’) { character=getchar(); putchar(character); }

16 A program to evaluate the equation y = x n ♥counter loop or sentinel loop? ♥How many times will the loop be executed? ♥Initialization the control variable. ♥Initialization y ♥Test control variable ♥Update control variable ♥Flow chart Example 6.1

17 What is the output of the following program #include int main( ) { int n=0; while( n++ < =3) printf (″%d\t″,n); return 0; }

18 What is the output of the following program #include int main( ) { int n=0; while( n < =3) printf (″%d\t″,n); n++; return 0; }

19 What is the output of the following program if we input 12345 #include main() { int n,s = 0; scanf("%d",&n); while(n) { s += n % 10; n /= 10; } printf("s=%d\n",s); }

20 What is the output of the following program if we input abcde? #include int main( ) { char c; int s=0; c=' '; while (c!='?') { c=getchar(); putchar(c); s++; } printf("\ns=%d\n",s); return 0; }

21 6.3 The do statement ♣The do statement takes the form: ♣The do statement construct provides an exit- controlled loop and therefore the body of the loop is always executed at least once. do { body of the loop } while ( test condition); Entry False True test condition ? Body of the loop

22 6.3 The do statement ♣Rewrite the program that evaluates the sum of the square of 1 to 10 with do-while statement ♣Input a character from the keyboard until the character is equal to ‘Y’

23 6.4 The for statement ♣The for loop is another entry-controlled loop that provides a more concise loop control structure. The general form of the for loop is ♣The execution of the for statement for ( initialization; test-condition; increment ) { body of the loop }

24 Flowchart False True Test- condition Body of the loop Initialization of the control variables increment Next statement for ( initialization; test-condition; increment ) { body of the loop } The execution of the for statement only once

25 6.4 The for statement ♣Rewrite the program of sum of the square of 1 to 10 with for statement

26 for(x=2;i<=13; x+=2) { printf(“%d\n”,x); } for(x=5;i<=22; x+=7) printf(“%d\n”,x); for(x=6;i<=5; x+=7) printf(“%d\n”,x);

27 6.4 The for statement ♣The for statement and its equivalent of while and do statements are shown below. forwhiledo for( n=1;n<=10;++n) { …… } n = 1; while( n<=0 ) { …… n=n+1; } n = 1; do { …… n=n+1; }while(n<=10);

28 6.4 The for statement ♣Print the “powers of 2” table for the power 0 to 20, both positive and negative. ♣We will print ♥p=2 n ♥q=2 -n = 1/p ♥consider data type of p and q ♠p: long int ♠q: double ♥print the header of table ♥use for loop to program Example 6.3

29 6.4 The for statement ♣The initialization and increment section may have more than one part. For example, for( n=1, m=50 ; n<=m; n=n+1, m=m-1) { p=m/n; printf(“%d %d %d\n”, n, m, p); }

30 6.4 The for statement ♣One or more sections in for loop can be omitted, if necessary. For example, ♣Note, the semicolons separating the sections must remain. m = 5; for( ; m!=100 ; ) { printf(“%d\n”, m); m = m + 5; }

31 Nesting of loops ♣A nested loop is one loop inside another loop. for( ; ; ) { …… while( ) { …… } …… } do { …… while( ) { …… } …… }while( ); for( ; ; ) { …… for( ; ; ) { …… } …… }

32 Nesting of loops CorrectWrong

33 Nesting of loops ♣A common use for nested loops is to display data in rows and columns. ♣One loop can handle, say, all the columns in a row, and the second loop handles the rows.

34 main( ) { int i,j; for ( i=0 ; i<3 ; i++) { printf(“i= %d:”,i); for( j=0 ;j<4 ; j++) printf(“j=%-4d”,j ); printf(“\n”); } }

35 ♣A program multiplication table from 1*1 to 12*10 Example 6.2

36 ♣A class of n students take an annual examination in m subjects. Write a program to read the marks obtained by each student in various subjects and to computer and print the total marks obtained by each of them. ♣Input m, n ♣Reading m marks of ith student ( 1<=i <=n) ♣Evaluate the total of ith student ♣print the total of ith student Example 6.4

37 T F Input n,m i <= n begin i =1 Evaluate total of ith student input m marks of ith student end print total of ith student i = i+1 Flowchart

38 main() { int m, n,i, j ; float mark, total; printf(“ Input number of student\n”); scanf(“%d”,&n); pritnf(“Input number of subject\n”); scanf(“%d”,&m); for(i =1 ; i<=n; i++) { /* input m marks of ith student evaluate total of ith student */ printf(“total = %f\n”,total); } }

39 input m marks of ith student T j=1 j <= m j++ input mark of jth subject F next statement

40 Evaluate sum of ith student T total=0 j <= m j++ total = total + mark F next statement

41 input m marks of ith student, evaluate total of ith student for(j=1;j<=m;j++) { printf(“Please input mark of %d subject:”, j); scanf(“%f”,&mark); total += mark; }

42 Selecting a loop ♣Analyze the problem and see whether it required a pre-test or post-test loop. ♣If it requires a post-test loop, then we can use only one loop, do while. ♣If it requires a pre-test loop, then we have two choices: for and while. ♣Decide whether the loop termination requires counter- based control or sentinel-based control. ♣Use for loop if the counter-based control is necessary. ♣Use while loop if the sentinel-based control is required. ♣Note that both the counter-controlled and sentinel- controlled loops can be implemented by all the three control structures.

43 What would be the function of the following segments? sum=0; for(count=1; count<=100; count++) { scanf(“%d”,&number); sum += number; if(sum>=3000) count=100; }

44 What would be the output of the following segments? main() { int k=0 ; char c=‘B' ; switch(c) { case 'A': k++ ; break ; case 'B': k-- ; case 'C': k+=2 ; break ; case 'D': k=k%2 ; break ; default: k=k/3 ; } printf("k=%d\n",k) ; }

45 What would be the output of the following segments? main() { int k=0 ; char c=‘B' ; switch(c) { case 'A': k++ ; break ; case 'B': k-- ; case 'C': k+=2 ; break ; case 'D': k=k%2 ; break ; default: k=k/3 ; } printf("k=%d\n",k) ; } exit from switch.

46 6.5 Jumps in Loops ♣The break statement provides an early exit from for, while, and do, just as from switch. ♣When a break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop. ♣When the loops are nested, the break would only exit from the loop containing it.

47 6.5 Jumps in Loops break True False True test condition1? statements1 Entry test condition2 False statements2

48 What is the output of the following statements for (i=1; i<11; i++) { if(i%3==0) break; printf("%d",i); }

49 ♣Write a program to determine whether a given number is a prime number. Example

50 T F T F begin input m i=2 i<=sqrt(m) i++ m%i m is a primem is not a prime end

51 T F i>sqrt(m) T F F T begin input m i=2 i<=sqrt(m) i++ m%i m is a primem is not a prime end

52 #include main() { int m, i; printf ("Enter a number:"); scanf ("%d",&m); for(i=2;i<=sqrt(m);i++) { if(m%i==0) break; } if(i>sqrt(m)) printf("%d is a prime number\n",m); else printf("%d is not prime number.\n",m); }

53 #include main() { int m, i, flag=1; printf ("Enter a number:"); scanf ("%d",&m); for (i=2;i<=sqrt(m);i++) { if(m%i==0) { flag=0; break; } if(flag) printf("%d is a prime number\n",m); else printf("%d is not prime number.\n",m); }

54 Skipping a Part of a Loop ♣Like the break statement, C supports another similar statement called the continue statement. ♣The continue tells the compiler, “skip the following statements and continue with the next iteration”.

55 Skipping a Part of a Loop continue True False True test condition1? statements Entry test condition2 False statements

56 while (……) { …… if ( condition ) continue; …… } ……

57 for (i=1;i<=100; i++) { if (i%5==0) continue; printf(“% d ”,i); }

58 #include main() { int i, n; for ( i=1 ; i <=5 ; i++ ) { printf(“Please enter n:”); scanf(“%d”,&n); if(n<0) break; printf(“n=%d\n”,n); } printf(“Program is over!\n”); } What is the function of the following statements

59 #include main() { int i, n; for ( i=1 ; i <=5 ; i++ ) { printf(“Please enter n:”); scanf(“%d”,&n); if(n<0) continue; printf(“n=%d\n”,n); } printf(“Program is over!\n”); } What is the function of the following statements

60 ♣The program evaluates the square root of numbers and prints the results. The process stops when the number 9999 is typed in. If the number is a negative, the program prints a message saying that the number is negative and count the number of negative. ♣The final output includes the number of positive values evaluated and the number of negative items. Example 6.7

61 #include main() { int count,negative; double number,sqroot; printf("Enter 9999 to stop\n"); count=0; negative=0; while(count<=100) { printf("Enter a number: "); scanf("%lf",&number); if(number==9999) break; if(number<0) { printf("Number is negative\n\n"); negative++; continue;/* skip rest of the loop */ } sqroot=sqrt(number); printf("Number=%lf\n Square root=%lf\n\n",number,sqroot); count++; } printf("Number of items done=%d\n",count); printf("\n\nNegative items=%d\n",negative); printf("End of data\n"); }

62 Just Remember ♣Read “just remember” on page 167

63 Case Study ♣Table of binomial coefficients 二项式系数 ♣Histogram 柱状图 ♣Minimum Cost 最小代价 ♣Plotting of two functions 两个函数图

64 Assignment ♣Programming Exercises ♥6.1 ♥6.2 ♥6.4 ♥6.8 ♥6.10 ♥6.14 (a) or (b) or (c)


Download ppt "The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x<0 y= 0 x=0 y= 1 x>0 B. y= 0 x<0 y= -1 x=0 y= 1 x>0 C. y= 1 x<0."

Similar presentations


Ads by Google