Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent http://paultennent.wordpress.com/G50PRO.html [email protected] Room C7 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 Control Flow Statements Decision-making statements Looping statements if-then if-then-else switch for While do-while Branching statements break continue return 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) 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: for (initialization; Condition; increment) { statement(s) } keep in mind that: initialization expression 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. Loops: for Statement 1 for (int i=0; i<2; i++){ Initialization Statement (s); 8 } 2 5 Condition Evaluation true 3 6 Statement (s) 4 7 Increment ii = 0 =2 1 false for Statement class ForCount { public static void main(String[] args){ for(int i=1; i<5; i++){ System.out.println("Count is: " + i); } } } The output of this program is: Count is: 1 Count is: 2 Count is: 3 Count is: 4 while Statement The while statement continually executes a block of statements while a particular condition is true. while (expression) { statement(s) } 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 Statement boolean found = false; while (!found) { //code to search for a value in list //set found = true to exit } Condition Evaluation true The statement(s) is executed over and over until the condition becomes false statement(s) is executed Zero or more times Statement (s) false 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 do-while Statement The while statement continually executes a block of statements while a particular condition is true. do { statement(s) }while (expression) 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-while Statement Statement (s) boolean exit = false; do { statement(s) //set exit = true to exit } while (!exit); The statement(s) is executed over and over until the condition becomes false statement(s) is executed at least once true Condition Evaluation false 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 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); } Notes If what you really want is to execute the loop 10 times, write the condition Number < 10 and not as Number <= 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 not from 1 to 10, but from 0 to 9. All counting in Java tends to start at zero rather than one. 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 Summary Control Flow Statements Looping statements for While do-while