L12 - CompSci 101 Lecture 12 1 Computer Science 1 1 SS C...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CompSci 101 Lecture 12 1 Computer Science 1 1 SS C Lecture 12 Contents Loops: initialisation, condition, body, increment while loops for loops Coursebook: 12 2 Control Structures It is important to understand how the computer works its way through a program, Fnding which instruction to execute next. Control structures allow us to change the ow of statement execution in our programs. 3 Selection statements The use of selection statements means that certain statements may be left unexecuted. Selection statements are also called branches, as, when during its execution the program arrives at an if statement, control will "branch" off into one of two or more "directions". 1 2 3 4 5 6 String s1 = "hello"; if ( s1.length() > 3 ) { System.out.println(s1.substring(3)); } else { System.out.println(s1); } 4 Iteration Using iteration, blocks of statements are executed a Fnite number of times (or not at all), until the loop condition evaluates to false. The syntax for the while loop is shown below: while ( condition ) { } statement 1; ... statement n; 5 while loop irst, the condition is tested. If the condition is true, then the statements (known as the body of the loop) are executed. After the statements are executed, control returns to the top of the loop, and the condition is tested again. As long as the condition is true, the statements will be executed. statementsBeforeLoop while ( condition ) { } statementsAfterLoop statement 1; ... statement n; 6 Loops This diagram shows the ow of control of a loop: Is the condition true? Statement before the loop Body of the loop Statement after the loop YES NO CompSci 101 Lecture 12 2 7 Designing Loops Is the condition true? Statement before the loop Body of the loop Statement after the loop YES NO initialisation condition body of the loop increment 8 Designing Loops - initialisation Any work that needs to be done before the loop starts. Usually you will need to initialise a loop variable to its start value. 1 2 3 4 5 int sum = 0; int i = 2; . while ( ) { } 6 7 8 9 10 String words = "I must not "; int lineNumber = 1; . while ( ) { } 9 Designing Loops - body The body will contain the statements which need to be done over and over (or not at all). 1 2 3 4 5 int sum = 0; int i = 2; sum = sum + i; while ( ) { } 6 7 8 9 10 11 12 String words = "I must not "; int lineNumber = 1; while ( ) { } System.out.println(words + "swear."); System.out.println(words + "be messy."); System.out.println(lineNumber); 10 Designing Loops condition How many times (if any) should the loop...
View Full Document

Page1 / 5

L12 - CompSci 101 Lecture 12 1 Computer Science 1 1 SS C...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online