1 / 41

Repetition Statements

305171 Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University. Repetition Statements. Control Structures Revisit. Control structures control the flow of execution in a program or function.

orinda
Download Presentation

Repetition Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 305171 Computer Programming RattapoomWaranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University Repetition Statements

  2. Control Structures Revisit • Control structures control the flow of execution in a program or function. • Instructions are organized into 3 kinds of control structures • Sequence • Selection (decision, condition) • Repetition (looping)

  3. Sequence Structure • a series of actions are performed in sequence { int a,n; n = 7; printf("n = %d\n",n); printf("a = ++n\n"); a = ++n; printf("n = %d, a = %d\n",n,a); }

  4. n = 13? n = n + 1 Decision Structure get n • One of two possible actions is taken, depending on a condition. No Yes display“Lucky You” : : scanf(“%d”, n); if (n == 13) { printf(“Lucky you!”); n++; } printf(“You entered %d.”,n); : : display“You entered”and n

  5. YES x < y? A Repetition Structure • A repetition structure represents part of the program that repeats. This structure is commonly known as a loop. • In the flowchart segment, the question “is x < y?” is asked. If the answer is yes, then Process A is performed. The question “is x < y?” is asked again. Process A is repeated as long as x is less than y. When x is no longer less than y, the repetition stops and the structure is exited.

  6. Conditions • A program choose among alternative statements by testing the value of key variables. • Most conditions that we use to perform comparisons will use relational operators. • With logical operators, we can form more complicated conditions or logical expressions.

  7. Loops • Loops are used to repeat a block of code. • Being able to have your program repeatedly execute a block of code is one of the most basic but useful tasks in programming. • There are three types of loops: while, for, and do..while. Each of them has their specific uses.

  8. While statement • while loops are very simple. The basic structure is while ( condition ) statement; • If the condition is true (non-zero), the statement will be executed until the condition is false (zero). • Multiple statements can be grouped by putting them inside curly braces {, }.

  9. x < 10? x = x + 1 while statement x = 0 • Example : : int x = 0; while ( x < 10 ) { printf( "%d\n", x ); x++; } printf (“end\n”); : : No Yes displayx display“end”

  10. While Statement • Exercise 1: Basic while • Start and prepare to run a new project in VC++ Express • Copy the code from while.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code.

  11. While Statement • Exercise 2: Summation using while • Start and prepare to run a new project in VC++ Express • Copy the code from sumscore.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code. • Modify the code to compute mean of the score.

  12. While Statement • Exercise 3: Summation using while 2 • Start and prepare to run a new project in VC++ Express • Copy the code from sumscore2.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code. • Modify the code to compute mean of the score.

  13. While Statement • Exercise 4: Factorial • Start and prepare to run a new project in VC++ Express • Copy the code from factorial.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code.

  14. While Statement • Exercise 5: Yes or No • Start and prepare to run a new project in VC++ Express • Copy the code from yesno1.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code. • Modify the original code to cover small letters.

  15. While Statement • Exercise 6: Infinite loop • Start and prepare to run a new project in VC++ Express • Copy the code from infinite.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code. • Modify the code so that it no longer an infinite loop.

  16. Infinite Loops • Infinite loops are loops that never end. • They are created when a loop’s expression is never set to exit the loop. • Every programmer experiences an infinite loop at least once in his or her career.

  17. do...while • The do-while loop is similar to the while loop except the relational test occurs at the bottom (rather than top) of the loop. • This ensures that the body of the loop executes at least once. • As long as the condition is True (non zero), the body of the loop continues to execute.

  18. do...while • The format of the do-while is do    statement; while ( condition ); • The condition must be enclosed within parentheses, just as it does with a while statement. • Notice the semi-colon at the end of while condition.

  19. x < 10? x = x + 1 while statement x = 10 • Example : : int x = 10; while ( x < 10 ) { printf( "%d\n", x ); x++; } printf (“end\n”); : : No Yes displayx display“end”

  20. x = x + 1 x < 10? do...while statement x = 10 • Example : : int x = 10; do { printf( "%d\n", x ); x++; } while ( x < 10 ); printf (“end\n”); : : displayx No Yes display“end”

  21. do...While Statement • Exercise 7: do...while • Start and prepare to run a new project in VC++ Express • Copy the code from dowhile.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code.

  22. do...While Statement • Exercise 8: Yes or No 2 • Start and prepare to run a new project in VC++ Express • Copy the code from yesno2.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code. • Modify the original code to cover small letters.

  23. do...While Statement • Exercise 9: Check for a negative number • Start and prepare to run a new project in VC++ Express • Copy the code from checknegative.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code. • Modify the code to check for positive value, even or odd number.

  24. for statements • The for loop enables you to repeat sections of your program for a specific number of times. • Unlike the while and do-while loops, the for loop is a determinate loop. This means when you write your program you can usually determine how many times the loop takes place.

  25. For Statements • The format of the for loop is for (start_expr; condition; count_expr)   statement; • C evaluates the start expressionbefore the loop begins. Typically, the start expression is an assignment statement, but it can be any legal expression. C looks at and evaluates start expression only once, at the top of the loop. • Every time the body of the loop repeats, thecount expressionexecutes, usually incrementing or decrementing a variable. The conditionevaluates to True (nonzero) or False (zero), and then determines if the body of the loop repeats again.

  26. x < 10? x = x + 1 while statement x = 0 • Example : : int x = 0; while ( x < 10 ) { printf( "%d\n", x ); x++; } printf (“end\n”); : : No Yes displayx display“end”

  27. for Statement • Exercise 10: for loop • Start and prepare to run a new project in VC++ Express • Copy the code from for.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code. • Modify the code to use other increment or initialization methods. • Modify the code to use only while loop.

  28. for Statement • Exercise 11: Range • Start and prepare to run a new project in VC++ Express • Copy the code from range.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code. • Modify the code to count the number of integers between two inputs. • Modify the code to calculate the sum of all integers between A and B, and also find the average.

  29. for Statement • Exercise 12: Even and odd revisit • Start and prepare to run a new project in VC++ Express • Copy the code from evenodd.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code. • Modify the code to let the user input the minimum and maximum numbers. • Modify the code to count the integers between A and B that are divisible by 5.

  30. for Statement • Exercise 13: Factorial revisit • Start and prepare to run a new project in VC++ Express • Copy the code from factorial.c and paste into the code editor. • Modify the code to use only for loop.

  31. while and for equivalence for (start_expr; condition; count_expr)   statement; is equivalent to start_expr; while ( condition ) statement; count_expr;

  32. Break statement • The break statement is used to manipulate program flow in structures such as loops. • When a break statement is executed in a loop, the loop is terminated and program control returns to the next statement following the end of the loop.

  33. Break Statement • Exercise 14: break statement • Start and prepare to run a new project in VC++ Express • Copy the code from break.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code. • Modify the code to use while loop.

  34. continue statement • The continue statement is used to manipulate program flow in a loop structure. • When executed, any remaining statements in the loop are passed over and the next iteration of the loop is sought.

  35. Continue Statement • Exercise 15: continue statement • Start and prepare to run a new project in VC++ Express • Copy the code from continue.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code. • Modify the code to use while loop.

  36. Nested loops • Any C statement can go inside the body of a loop—even another loop. When you put a loop within a loop, you are creating a nested loop. • Each time the outer loop is repeated, the inner loops are reentered, their loop control expressions are reevaluated, and all required iterations are performed.

  37. Nested loops • Exercise 16: Nested loops • Start and prepare to run a new project in VC++ Express • Copy the code from nestedfor.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code. • Modify the code to print more than one rectangle.

  38. j < row? i = i + 1 Nested Loops j = 0 • Example : : for(j = 0; j < row; j++) { for(i = 0; i < col; i++) { printf("*"); } printf("\n"); } : : No Yes j = j + 1 i = 0 i < col? No new line Yes display*

  39. Nested loops • Exercise 17: Multiplication table • Start and prepare to run a new project in VC++ Express • Copy the code from mtable.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code. • Modify the code to allow a user to specify the first and the last tables.

  40. Some common math Functions int abs (int x); IxIdouble fabs (double x); IxI double sin (double x); sin xdouble cos (double x); cos xdouble tan (double x); tan x double asin (double x); sin-1 x double acos (double x); cos-1 x double atan (double x); tan-1 x double exp (double x); exdouble log (double x); ln xdouble log10 (double x); log xdouble pow (double x,double y); xydouble sqrt(double x); x

  41. Math function • Exercise 17: Multiplication table • Start and prepare to run a new project in VC++ Express • Copy the code from distance.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code. • Modify the code to compute an angle forms by the line that pass these points and x-axis.

More Related