1 / 17

Introduction to Control Flow Statements in Programming

Learn about control flow statements in programming, including decision-making, looping, and branching. Understand how to use for, while, and do-while loops effectively.

Download Presentation

Introduction to Control Flow Statements in Programming

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. Introduction to ProgrammingG50PROUniversity of NottinghamUnit 6 : Control Flow Statements 2 Paul Tennent http://paultennent.wordpress.com/G50PRO.html Paul.tennent@nottingham.ac.uk Room C7

  2. Control Flow Statements • The statements inside your source files are generally executed from top to bottom • Control flow statements, break up the flow of execution by employing decision making, looping, and branching

  3. Control Flow Statements • Decision-making statements • if-then • if-then-else • switch • Looping statements • for • While • do-while • Branching statements • break • continue • return

  4. Why Looping? • To automate the repetition of instructions. • To iterate through data and test for certain condition • To keep attempting for some operation (such as obtaining data from a remote computer over a network)

  5. Loops: for Statement • for statement provides a compact way to iterate over a range of values. • Repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows: • keep in mind that: • initializationexpression initializes the loop; it's executed once, as the loop begins. • When the Condition is checked before each iteration through the loop. When it evaluates to false, the loop terminates. • increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value. for (initialization; Condition; increment) { statement(s) }

  6. Initialization ConditionEvaluation false true Statement (s) Increment Loops: for Statement 1 for (int i=0; i<2; i++){ Statement (s); } 8 2 5 3 6 i = 0 i = 2 i = 1 4 7

  7. for Statement • The output of this program is: • Count is: 1 • Count is: 2 • Count is: 3 • Count is: 4 class ForCount { public static void main(String[] args){ for(int i=1; i<5; i++){ System.out.println("Count is: " + i); } } }

  8. while Statement • The while statement continually executes a block of statements while a particular condition is true. • The expression must return a boolean value • The while statement continues testing the expression and executing its block until the expression evaluates to false while (expression) { statement(s) }

  9. ConditionEvaluation false true Statement (s) while Statement • The statement(s) is executed over and over until the condition becomes false • statement(s) is executed Zero or more times boolean found = false; while (!found) { //code to search for a value in list //set found = true to exit }

  10. while Statement class Count { public static void main(String[] args){ int count = 1; while (count < 5) { System.out.println("Count is: " + count); count++; } System.out.println(“Outside while loop"); }//main end }//class end • The output of this program is: • Count is: 1 • Count is: 2 • Count is: 3 • Count is: 4 • Outside while loop

  11. do-while Statement • The while statement continually executes a block of statements while a particular condition is true. • The expression must return a boolean value • do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once do { statement(s) }while (expression)

  12. do-while Statement Statement (s) • The statement(s) is executed over and over until the condition becomes false • statement(s) is executed at least once boolean exit = false; do { statement(s) //set exit = true to exit } while (!exit); ConditionEvaluation true false

  13. do-while Statement class Count { public static void main(String[] args){ int count = 10; do{ System.out.println("Count is: " + count); count++; } while (count < 5) System.out.println(“Outside while loop"); }//main end }//class end • The output of this program is: • Count is: 10 • Outside while loop

  14. Infinite Loops • infinite loop, will execute until the user interrupts the program. • This is a common type of logical error.always double check your loop condition. for(int i = 2; i > 1; i++){ System.out.println("Count is: " + i); }

  15. Notes • If what you really want is to execute the loop 10 times, write the condition • Number < 10 and not asNumber <= 9 • In general, specific values such as "10" should not appear within the body of your program. You should declare them as finals at the top of the program • static final int COUNT = 10; • In Java generally you would more likely want to loop notfrom 1 to 10, but from 0 to 9. All counting in Java tends to start at zero rather than one.

  16. Course Work 2.1 Notes • 11 marks for correct execution and meeting all requirements • 4 marks on the Quality of your code • Follow a logical Structure • Define local Variables at the start of main method • Use constants when needed • Correct indentation • Good use of comments

  17. Summary • Control Flow Statements • Looping statements • for • While • do-while

More Related