Download presentation
Presentation is loading. Please wait.
Published byLouisa McCoy Modified over 8 years ago
1
Introduction to Programming G50PRO University of Nottingham Unit 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: 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. for (initialization; Condition; increment) { statement(s) }
6
Loops: for Statement Initialization Condition Evaluation Statement (s) Increment true false 1 2 3 4 for (int i=0; i<2; i++){ Statement (s); } i = 0 i = 1 5 6 7 8 i = 2
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
while Statement Condition Evaluation Statement (s) true false boolean found = false; while (!found) { //code to search for a value in list //set found = true to exit } The statement(s) is executed over and over until the condition becomes false statement(s) is executed Zero or more times
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 Condition Evaluation Statement (s) true false 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
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 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.
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.