What is an INTJ loop

Entry into computer science with Java

Transcript

1 1/41 Introduction to computer science with Java Further instructions Gerd Bohlender Institute for Applied and Numerical Mathematics

2 Outline 2/41 1 Overview 2 Compound statement 3 Conditional statement 4 Selection statement 5 for loop 6 while loop 7 do loop 8 break statement 9 continue statement 10 Empty statement

3 Outline 3/41 1 Overview 2 Compound statement 3 Conditional statement 4 Selection statement 5 for loop 6 while loop 7 do loop 8 break statement 9 continue statement 10 Empty statement

4 4/41 Overview This chapter describes further instructions for a Java program. Further statements Compound statements, conditional statements, loops, ... Examples for-loop, do-loop, while-loop

5 Outline 5/41 1 Overview 2 Compound statement 3 Conditional statement 4 Selection statement 5 for loop 6 while loop 7 do loop 8 break statement 9 continue statement 10 Empty statement

6 6/41 Compound statement Syntax {} statement 1 ... statement n Using the pair of curly brackets {}, several statements can be combined to form a compound statement. The compound statement can be used anywhere where, for syntactic reasons, only one statement is permitted. The instructions are carried out in the specified order.

7 7/41 Compound instruction, example Example {} x = 1; y = 2; z = 3; Caution The semicolons in this compound statement belong to the expression statement, so they do not appear in the syntax!

8 Outline 8/41 1 Overview 2 Compound statement 3 Conditional statement 4 Selection statement 5 for loop 6 while loop 7 do loop 8 break statement 9 continue statement 10 Empty statement

9 9/41 Unilateral conditional statement Syntax: Unilateral conditional statement if (condition) statement1 The statement1 can be any statement. In most cases it is a compound statement, then the syntax looks like this: Syntax: ... with compound statement if (condition) {statement1 ... statementn}

10 10/41 Unilateral conditional instruction, sequence The condition in the round brackets is a Boolean expression (comparison, logical expression with logical operators). The statements are arbitrary statements. If the value of the expression is true, the instruction (instruction1) or the compound instruction (instruction1 ... instructions) is executed. If the value of the expression is false, nothing is carried out.

11 11/41 Two-sided conditional statement Syntax: Two-sided (double-sided) conditional statement if (condition) statement1 else statement2 The statement1 or statement2 can be any statement. In most cases these are compound statements, then the syntax is as follows: Syntax: ... with compound statements if (condition) {statement11 ... statement1n} else {statement21 ... statement2n}

12 12/41 Two-sided conditional statement, sequence The condition in the round brackets is a Boolean expression (comparison, logical expression with logical operators). The statements are arbitrary statements. If the value of the expression is true, the instruction (instruction1) or the compound instruction (instruction11 ... instruction1n) is executed. If the value of the expression results in false, the second instruction (instruction2) or compound instruction (instruction21 ... instruction2n) is executed.

13 13/41 Conditional statement, example Example Determination of the maximum of two whole numbers x and y. i n t x = 21, y = 12, max; i f (x> y) max = x; else max = y; / / e r g i b t: max = 21

14 Conditional statement, Caution 14/41 Caution If you want to execute several statements in the if or else branches instead of a single statement, you use a compound statement! Example Swap two whole numbers x and y if they are in the wrong order. i n t x = 21, y = 12, hi l f; i f (x> y) {hi l f = x; x = y; y = h i l f; }

15 Conditional statement, assignment if-else 15/41 If there are no brackets, each else is always assigned to the next preceding if. This assignment can be changed using suitable brackets. Example i n t x = 5, y = 6; z = 4; i f (x> y) i f (x> z) max = x; else max = z; / / Assignment to the second i f ... i f (x> y) {i f (x> z) max = x; } else max = z; / / Assignment to the first i f

16 Outline 16/41 1 Overview 2 Compound statement 3 Conditional statement 4 Selection statement 5 for loop 6 while loop 7 do loop 8 break statement 9 continue statement 10 Empty statement

17 17/41 Selection statement Syntax switch (expression) {case constant expression1: statement sequence1 case constant expression2: statement sequence2 ... default: statement sequence} The expression may be of the type byte, short, int or char; the constant expressions must be are different from each other assignment-compatible constants. If the expression matches a constant expression, the corresponding sequence of statements is continued and the following sequences of statements are processed sequentially. The default branch is optional. This is carried out if no constant expression matches.

18 Selection instruction, break Caution The individual cases do not have to be mutually exclusive! In order to prevent the sequential processing, the instruction sequences i. generally follow a break instruction (see below)! Example switch (x) {case 1: case 2: x = 4; case 3: x = 8; case 4: x = 16; break; default: x = 2; } For x = 1 the value is 512, for x = 2 the value 1024, for x = 3 the value 384, for x = 4 the value 64 and for all other values ​​of x the value 2 * x. 18/41

19 Outline 19/41 1 Overview 2 Compound statement 3 Conditional statement 4 Selection statement 5 for loop 6 while loop 7 do loop 8 break statement 9 continue statement 10 Empty statement

20 20/41 for loop Syntax for (initialization; expression; update) instruction The initialization part is executed once and is used to declare local variables. It can contain a list of expressions for initializing counters (all separated by commas). The expression part is of type boolean. As long as the value is true, the instruction (usually a compound instruction) and then the update part are repeated. The loop ends when the expression is false. The update part contains a list of expressions (all separated by commas) which are executed in sequence.

21 21/41 for loop, optional parts All three parts (initialization, printout, update) are optional. However, there must always be two semicolons in the round brackets. If the initializer is missing, then i.a. the counter variable must be initialized before the loop. If the expression part is missing, the condition is always considered to be fulfilled. The result is an infinite loop that must be exited with a break statement! If the update is missing, i.a. the counter variable within the instruction can be changed.

22 for loop, examples 22/41 i n t i; for (i = 0; i <10; ++ i) System. out. p r i n t l n (i); / /, Var. i g i l t beyond the end of the loop. for (i n t j = 0; j <10; ++ j) system. out. p r i n t l n (j); / /, Variable j applies only until the end of the loop. for (i n t j = 0; j <100; j + = 2) system. out. p r i n t l n (j); / / for (i n t j = 9; j> = 0; j) system. out. p r i n t l n (j); / / for (i n t j = 2, k = 3; j + k <27; j = 2, k + = 3) system. out. p r i n t l n (j); / / confusing but correct. / / 2 4 8

23 for loop, examples with parts left out 23/41 i n t i = 5; for (; i <10; ++ i) System. out. p r i n t l n (i); / /, Var. i g i l t beyond the end of the loop. for (i n t j = 0;; ++ j) i f (j <10) system. out. p r i n t l n (j); else break; / /, Variable j applies only until the end of the loop. for (i n t j = 0; j <10;) system. out. p r i n t l n (++ j); / / i n t k = 5; for (;;) i f (k <10) system. out. p r i n t l n (k ++); else break; / /

24 24/41 for loop, example with double Caution The following example should be used with caution! for (double x = 0; x <= 1; x + = 0.1) {system. out. p r i n t l n (x); } The incremental increase in steps of 0, 1 can lead to rounding errors. It is therefore not clear here whether the number one is reached or not!

25 Outline 25/41 1 Overview 2 Compound statement 3 Conditional statement 4 Selection statement 5 for loop 6 while loop 7 do loop 8 break statement 9 continue statement 10 Empty statement

26 26/41 while loop Syntax while (condition) statement If the condition in the round brackets is true, the statement is repeated until the condition is false. Caution The instruction can of course again be a compound instruction! The following for loop is equivalent: for (; condition;) statement

27 while loop, example checksum of a natural number 27/41 import java. u t i l. ; public class checksum {public s t a t i c void main (S t r i n g [] args) {scanner sc = new scanner (system. i n); i n t n, sum = 0; System. out. p r i n t (n =); n = sc. n e x t I n t (); while (n> 0) {sum + = n% 10; n / = 10; } System. out. p r i n t l n (checksum = + sum); }}

28 Outline 28/41 1 Overview 2 Compound statement 3 Conditional statement 4 Selection statement 5 for loop 6 while loop 7 do loop 8 break statement 9 continue statement 10 Empty statement

29 do loop Syntax do statement while (condition); In principle like the while loop, but here the statement (usually a compound statement) is executed first and then the condition is checked. This means that with a do loop, in contrast to a while loop, the statement is always executed at least once. If the condition is true, the loop is repeated; if it is false, then it is canceled. Caution In some other programming languages ​​this is exactly the opposite! 29/41

30 30/41 Comparison of do and while loops Example with do loop Comparison of do and while loops. i n t n = 5; do n / = 2; while (n> = 10); / / e r g i b t n = 2 Example with while loop i n t m = 5; while (m> = 10) m / = 2; / / e r g i b t m = 5

31 31/41 Typical application of a do-loop Example reading in a positive number. do {system. out. p r i n t l n (please enter p> 0); p = sc. n e x t I n t (); } while (p <= 0);

32 Outline 32/41 1 Overview 2 Compound statement 3 Conditional statement 4 Selection statement 5 for loop 6 while loop 7 do loop 8 break statement 9 continue statement 10 Empty statement

33 33/41 break statement Syntax break; The break statement is inside the selection statement or inside for, while and do loops. These are exited immediately when the break statement is reached. There is also a variant with an associated brand, i. H. when the break instruction is reached, the marked instruction is exited. This variant can also be used for other instructions (e.g. compound instructions).

34 34/41 break statement, example example outside: / / set mark for (inti = 0; i <10; ++ i) / / outer loop for (intj = 0; j <100; ++ j) { / / inner loop if (...) break; / / v e r l a e s t inner S c h l e i f e i f (...) break outside; / / L e s ts the outside c ou s t marked with outside / /} Caution This programming style is confusing and quickly makes the source text confusing and prone to errors!

35 Outline 35/41 1 Overview 2 Compound statement 3 Conditional statement 4 Selection statement 5 for loop 6 while loop 7 do loop 8 break statement 9 continue statement 10 Empty statement

36 36/41 continue statement Syntax continue; The continue statement is only used within for, while and do loops. When the continue statement is reached, the current loop run is ended immediately and the next one begins. Analogous to the break statement, there is also a variant with an associated label to which the program jumps when the continue statement is reached, i.e. H. the current loop cycle of the marked loop is aborted and the next one begins.

37 continue statement, example 37/41 example for (i n t i = 0; i <10; ++ i) {System. out. p r i n t (a =); i n t a = sc. n e x t I n t (); System. out. p r i n t (b =); i n t b = sc. n e x t I n t (); i f (b == 0) continue; System. out. p r i n t l n (a / b); }

38 38/41 continue statement, example continue with label outside: for (inti = 1; i <= 4; ++ i) {for (intj = 1; j <= 4; ++ j) {if (j = = 2) continue; i f (i == 2) continue outside; System. out. p r i n t l n (i j); // Output:}} Caution Like the use of the break statement, the use of the continue statement is very confusing! Both instructions should be used very sparingly, if at all!

39 Outline 39/41 1 Overview 2 Compound statement 3 Conditional statement 4 Selection statement 5 for loop 6 while loop 7 do loop 8 break statement 9 continue statement 10 Empty statement

40 40/41 Empty instruction syntax; // Expression statement without an expression {} // Compound statement without statements Empty statements have no effect. They are sometimes useful for syntactic reasons.

41 Empty instruction, caution! Attention sometimes empty statements lead to hidden errors, as in the following examples: if (2> 3); System.out.print ("nanu"); Output: nanu if (2 <3) System.out.print ("clear ..."); else; System.out.print ("nanu"); Output: clear ... nanu int i = 33; for (i = 0; i <10; i ++); System.out.print (i); Edition: / 41