1 / 68

CS 141 Computer Programming 1

Control Statements. CS 141 Computer Programming 1. Teacher Assistant AlAnoud Bahomaid. Outline. Control structure in c++ Repetition Structure Counter control loop While repetition structure For repetition structure Optional expressions in the for statement header Common logical error

charo
Download Presentation

CS 141 Computer Programming 1

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. Control Statements CS 141Computer Programming 1 Teacher Assistant AlAnoudBahomaid

  2. Outline Control structure in c++ Repetition Structure Counter control loop While repetition structure For repetition structure Optional expressions in the for statement header Common logical error The counter variable Scope For repetition example Convert for and while Do… while repetition structure Nesting loop Nested control structure Continue Break

  3. Control structures in C++

  4. repetition structure Definition • It’s a control structure that repeats a group of steps in a program • Action repeated while some condition remains true • while • for • do.. while

  5. repetition structure When use repetition structure • ask yourself some of the following questions to determine whether loops will be required in your code: • Were there any steps I repeated as I solved the problem? If so, which ones? • If the answer to question 1 is yes, did I know in advance how many times to repeat the steps? • If the answer to question 2 is no, how did I knowhow long to keep repeating the steps?

  6. counter-controlled loop Definition • a loop whose required number of iterations can be determined before loop execution begins Essentials of counter-controlled repetition requires: The name of a control variable (loop counter) The initial value of the control variable. The loop-continuation condition that test for the final value of the control variable. The increment (or decrement) by which the control variable is modified each time through the loop

  7. Sentinel-controlled loop Definition • A loop whose number of iterations is UNKNOWN. (indefinite repetition). • Use a special value called sentinel value (signal value, dummy value or a flag value). • Indicates the end of the loop.

  8. 1-While Repetition Structure (1-10) Activity Diagram True Action (a) Condition False

  9. 1-While Repetition Structure (2-10) • If the controlling expression( condition) is true, the loop body is then executed before the controlling expression is evaluated once more. • If the controlling expression (condition) is false, i.e. expression evaluates to false, the program goes on to execute the statement following the while loop. • If the body of the counter-controlled repetition contains more than one statement, you should surround its body by braces { }.

  10. 1-While Repetition Structure (2-10) Syntax No semicolon after while statement While( loop repetition condition) statement ; OR While( loop repetition condition) { statements ; . . }

  11. 1-While Repetition Structure (4-10) Question Write a program that print numbers from 1 to 10 using (while) loop

  12. 1-While Repetition Structure (5-10)

  13. 1-While Repetition Structure (6-10)

  14. 1-While Repetition Structure (7-10) • In previous Example: • Control variable name : counter • Initial value : 1 • The loop continuation condition: counter<=10 • The loop counter is incremented : ++counter ;

  15. 1-While Repetition Structure (8-10) Question Write a program to find the average of the students grade for ten subjects in the class.

  16. 1-While Repetition Structure (9-10)

  17. 1-While Repetition Structure (10-10)

  18. 2- for Repetition Structure (1-8) Syntax • for ( initialization ;LoopContinuationTest; increment ) • statement OR for ( initialization ;LoopContinuationTest; increment ) { statementS . . } No semicolon after for statement

  19. (2- for Repetition Structure (2-8 As we said in while, If you need to repeat more than one statement in a program loop, you must place the statements in a block marked by braces { }.

  20. 2- for Repetition Structure (3-8) Question Write a program that print numbers from 1 to 10 using (for) loop

  21. 2-for Repetition Structure (4-8)

  22. 2-for Repetition Structure (5-8)

  23. 2-for Repetition Structure (6-8)

  24. 2-for Repetition Structure(7-8) • Initialization and increment: • For multiple variables, use comma-separated lists for (inti = 0, j = 0; j + i <= 10; j++,i++ ) cout << j + i << endl;

  25. 2-for Repetition Structure (8-8) • Vary control variable from 1 to 5 in increments of 1 • for ( inti = 1; i <= 5; i++ ) • Vary control variable from 5 to 1 in decrements of 1 • for( inti = 5; i >= 1; i-- ) • Vary control variable from 7 to 77 in steps of 7 • for( inti = 7; i <= 77; i += 7 ) • Vary control variable from 20 to 2 in steps of -2 • for( inti = 20; i >= 2; i -= 2 ) • Vary control variable over the sequence: 2, 5, 8, 11, 14, 17, 20 • for( inti = 2; i <= 20; i += 3 ) • Vary control variable over the sequence: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0 • for( inti = 99; i >= 0; i -= 11 )

  26. Optional expressions in the for statement header • All the three expressions in the for statement header are optional . • The two semicolon are required. • Omitting the loopContinuationCondition: • C++ assumes that the condition is true. for ( inti=0 ; ; i++) • Omitting the initialization expression: • The counter variable must be initialized earlier in the program. Inti=0; for( ; i< 10 ; i++) • Omitting increment expression: • The increment is calculated by statement in the body. for ( inti=0 ; i<10 ; ) i++;

  27. Loop Loop Loop loop . . 10. loop Optional expressions in the for statement header Examples: 0123456789 Infinite loop - initialize control variable before the loop - increment control variable in the loop body

  28. Common logical Error • Be sure to verify that a loop’s repetition condition will become false (0); otherwise, an infinite loop may result. This condition will never be false Example • for (int counter=1 ; counter <= 25 ; counter--) • cout << counter << endl; intcounter = 1; while (counter <= 25){ cout << counter << endl; counter -- ; } This condition will never be false

  29. The counter variable Scope If the initialization expression declares the control variable , the control variable can be used only in the body of the for statements. This is what we called variable scope.

  30. for Repetition Example (1-5) Question Write a program that calculate the sum of evenintegers from 2 through 100 . then determine the scope of the control variable .

  31. for Repetition Example (2-5)

  32. for Repetition Example (3-5)

  33. for Repetition Example (4-5)

  34. for Repetition Example (5-5) Thescope of the control variable (number) is: Line 14 and Line 15

  35. Convert for and while • for loops can usually be rewritten as while loops for(initialization; loopContinuationCondition; increment) { Statement } Is Equivlent to: initialization; while (loopContinuationCondition) { statement increment; }

  36. 3-do… while structure Repetition (1-9) • The do…while repetition statement is similar to the while statement. • In the while: • The loop continuation condition test occurs at the beginningof the loop before the body of the loop executes. • In the do … while: • The loop continuation condition test occurs after the loop body executes. • The loop body always executes at least once. • Recommended to use braces in the do.. While to avoid confusing with while statements.

  37. 3-do… while structure Repetition (2-9) Activity Diagram Action (a) True Condition False

  38. 3-do… while structure Repetition (3-9) Syntax Don’t forget semicolon after condition do { statement } while ( condition );

  39. 3-do… while structure Repetition (4-9) Question Write a program that print numbers from 1 to 10 using (do.. while) loop

  40. 3-do… while Repetition Structure (5-9)

  41. 3-do… while Repetition Structure (6-9)

  42. Notice the pre increment in loop-continuation condition. 3-do… while structure Repetition (7-9) Another solution

  43. 3-do… while structure Repetition (8-9) Question Enter a number (less than 1 = quit): 3 Enter a number (less than 1 = quit): 1 Enter a number (less than 1 = quit): 4 Enter a number (less than 1 = quit): 0 The average is: 2 Press any key to continue . . . Enter a number (less than 1 = quit): 0 No average Press any key to continue . . . Write a program that request the user to type positive numbers (one each time), and calculate the average when the user enter a number less than one.

  44. 3-do… while structure Repetition (9-9) Solution

  45. Nesting Loops (1-7) Loops can be nested, that is, the loop body can also contain a loop.

  46. Nesting Loops (2-7) Question • Write a program that print this using Loop

  47. Nesting Loops (3-7)

  48. Nesting Loops (4-7)

  49. Nesting Loops (5-7) Question • Write a program that print this using Loop

  50. Nesting Loops (6-7)

More Related