170 likes | 285 Views
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. Control Flow Statements.
E N D
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
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 • if-then • if-then-else • switch • Looping statements • 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: • 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) }
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
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); } } }
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) }
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 }
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. • 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)
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
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 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.
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