1 / 18

Engineering Computing I

Engineering Computing I. Chapter 3 Control Flow. Chapter 3 - Control Flow. The control-flow of a language specify the order in which computations are performed. Statements and Blocks. Statement. x = 0;. { int y; x = 0; i ++; printf (...); }. Block. No semicolon.

ansel
Download Presentation

Engineering Computing I

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. Engineering Computing I Chapter 3 Control Flow

  2. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed Chapter 3

  3. Statements and Blocks Statement x = 0; { int y; x = 0; i++; printf(...); } Block No semicolon Variable declared Chapter 3

  4. if-else if (expression) statement1 else statement2 if (n > 0) if (a > b) z = a; else z = b; if (n > 0) { if (a > b) z = a; } else z = b; if (n > 0) if (a > b) z = a; else z = b; Chapter 3

  5. else If Chapter 3

  6. Exercise Using else if statement, write a program that analyzes the variable SeaTemp (initialized to 75) and decides the sea-water condition based on the following table: Then set SeaTemp to different values to test your program. Chapter 3

  7. Switch Chapter 3

  8. Switch Chapter 3

  9. Exercise Using a switchstatement, write a program that analyzes the variable SeaTemp (initialized to 75) and decides the sea-water condition based on the following table: Then set SeaTemp to different values to test your program. Chapter 3

  10. Loops - While Evaluate expression loop NO Expression = 0 YES exit Exit the Loop and Continue Execute statement Chapter 3

  11. Exercise – while loop • Read characters from the keyboard and copy to the monitor until pattern ‘##’ is entered! Chapter 3

  12. Loops - While and For Loop index manipulation Condition Initialization Chapter 3

  13. Exercise – for loop • Write a program to print out the squares and cubes of the first 100 positive integers. Display a proper heading for the generated table. Chapter 3

  14. Nested for loops Show the output of the following program by manually going through the code. Chapter 3

  15. Loops - Do-While Chapter 3

  16. Break and Continue • The breakstatement provides an early exit from for, while, and do, just as from switch. • A break causes the innermost enclosing loop or switch to be exited immediately Chapter 3

  17. Break and Continue • The continuestatement causes the next iteration of the enclosing for, while, or do loop to begin. • In the while and do, this means that the test part is executed immediately; in the for, control passes to the • increment step. • The continue statement applies only to loops, not to switch. Chapter 3

  18. Goto and labels • The gotostatementbranches to a statement designated by a label • Formally, the goto statement is never necessary, and in practice it is almost always easy to write code without it • There are a few situations where gotos may find a place. The most common is to abandon processing in some deeply nested structure, such as breaking out of two or more loops at once. The break statement cannot be used directly since it only exits from the innermost loop Label Chapter 3

More Related