1 / 52

Control Statements

Control Statements. Ch 5, 6. Control Structures. Sequence Statements executed one by one { … } // block Selection One of several statements is selected if ( if … else ), switch Repetition (Loop) Same statements executed multiple times while , do … while , for ( foreach ). Block.

egibson
Télécharger la présentation

Control 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. Control Statements Ch 5, 6

  2. Control Structures • Sequence • Statements executed one by one • { … } // block • Selection • One of several statements is selected • if (if…else), switch • Repetition (Loop) • Same statements executed multiple times • while, do…while, for (foreach)

  3. Block • Several statements bracketed by { and } • Statements within a block executed sequentially • Treated as a single statement • Can be used wherever one statement is legal

  4. Selection Statements • if • Single-selection • if…else • Double-selection • switch • Multiple-selection

  5. ifExamples if(grade >= 60) Console.WriteLine("Passed");

  6. UML Activity Diagram • Describes execution sequence

  7. if…else Examples if(grade >= 60) Console.WriteLine("Passed"); else Console.WriteLine("Failed");

  8. UML Activity Diagram

  9. ifSyntax if (<boolean_expression>) <yes_statement>else <no_statement> • Each alternative is only ONEstatement! • To have multiple statements in either branch, use a block • else is optional

  10. Boolean Expression • Gives a Boolean result (true or false) • A value of type bool bool isGraduate = true; • Comparison operation grade >= 60 • Logical operation grade >= 60 && isGraduate

  11. Comparison Operators

  12. Logical Operators • Combine multiple Boolean expressions VB C# Example in C# && grade >= 60 && isGraduate AND OR || m=="MIS" || m=="CS" NOT ! ! isGraduate

  13. Nested if Statement if(grade >= 90) Console.WriteLine("A"); else if(grade >= 80) Console.WriteLine("B"); else if(grade >= 70) Console.WriteLine("C"); else Console.WriteLine("F");

  14. Equivalent To // Nested if in a different form if(grade >= 90) Console.WriteLine("A"); else if(grade >= 80) Console.WriteLine("B"); else if(grade >= 70) Console.WriteLine("C"); else Console.WriteLine("F");

  15. Not Equivalent To Multiple if if(grade >= 90) Console.WriteLine("A"); if(grade >= 80) Console.WriteLine("B"); if(grade >= 70) Console.WriteLine("C"); else Console.WriteLine("F");

  16. switch Statement

  17. switch Example

  18. More on switch • Needs a break before end of each case • The default is optional • Must use exact values in cases • Can't test ranges, such as >=93, 90 to 93 • Can every switch be translated into if? • Can every if be translated into switch?

  19. Select Case in VB is more general! SELECT CASE grade CASE >= 93 letterGrade = "A" CASE 90 to 93 letterGrade = "A-" … CASE ELSE letterGrade = "F" END SELECT

  20. Loop Statements • while, for (foreach) • Loop body executed ZERO or many times • do…while • Loop body executed ONE or many times

  21. whileExample // find the first power of 3 // larger than 100 int product = 3; while (product <= 100) product = 3 * product;

  22. UML Activity Diagram

  23. Formulating Algorithms with Loops • Counter-controlled loop • Use a variable (called counter) to control the number of iterations • Sentinel-controlled loop • Use a special sentinel value to indicate "end of data entry"

  24. Counter-Controlled Loop(Fig. 5.7)

  25. Results

  26. Sentinel-Controlled Repetition(Fig. 5.8)

  27. Results

  28. Three Key Items of a Loop • Initialization • Set initial state of the data before the loop • Where to start • Loop Continuation Condition • Determine whether continue to next iteration • When to stop • Update • During each iteration, some data must change • How to approach the end

  29. Infinite Loop • A loop that can never terminate • A common error • Carefully design the three key items to avoid it

  30. for Loop for (initialization; loopContinuationCondition; increment) statement;

  31. while and for • The previous for is equivalent to the following while, unless there is a continue initialization; while ( loopContinuationCondition ) { statement; increment; }

  32. Exercise • Translate the previous while examples into for

  33. do…while Loop • Similar to while • Tests loop-continuation after performing body of loop • Loop body executes at least once • Can the previous while examples be changed to do…while?

  34. Alter the Flow of Control • break • Forces loop to exit immediately • continue • Forces loop to skip rest of loop body and start another iteration immediately

  35. A break Example

  36. Result

  37. A continue Example

  38. Result

  39. Summary • Control structures • Sequence, selection, repetition • Selection • if (if…else), switch • switch can't test ranges • Repetition (Loop) • while, for (foreach) • do…while must go thru the loop body at least once • Alter the flow of control • break, continue

  40. Aside: Other Topics • Integer Division • Type Casting • Conditional Operator • Compound Assignment Operators • Increment and Decrement Operators

  41. Integer Division • Results are integers. • Remainder discarded ! Not rounded ! 7 / 5→1, not 1.4, not 2. • % returns the remainder 7 % 5→2 • m / n * n + m % n→ m 7 / 5 * 5 + 7 % 5→7

  42. Division in VB / C# VB C# Division (real) / / Division (integer) \ / Modulus MOD %

  43. Division Example (by Instructor) Enter first real number: 7 Enter second real number: 5 x = 7, y = 5 x / y = 1.4 x / y * y = 7 Enter first integer: 7 Enter second integer: 5 m = 7, n = 5 m / n = 1 m % n = 2 m / n * n = 5 m / n * n + m % n = 7

  44. Type Conversion • Explicit conversion • Explicitly request type conversion average = (double)total / gradeCounter; • Implicit conversion • Performed implicitly when needed • Promotion (from a smaller type to a larger type) only

  45. Conditional Operator • The only ternary operator Console.WriteLine(grade >=60 ? "Passed": "Failed"); • A Boolean expression and two possible values • If the Boolean expression is true, take the first value; • Otherwise, take the second value

  46. Compound Assignment • Shorthand of assignment expression

  47. Increment/Decrement

More Related