Title: bung Softwareentwicklung 1 fr Wirtschaftsinformatik bung 5
1Übung Softwareentwicklung 1für
Wirtschaftsinformatik Übung 5
- Ismail Khalil Ibrahimismail_at_tk.uni-linz.ac.atht
tp//www.tk.uni-linz.ac.at/teaching/
2Control Structures
- In any programming language, there are three
fundamental patterns for controlling the flow of
the program execution called control structures - sequence
- program statements are executed sequentially
- also called straight-line programming
- decision (a.k.a selection)
- The flow of the program statements is altered
depending on one or more conditions - It is implemented in Java using if, if/else, and
switch statements - iteration
- Is a repetition control structure
- It is implemented in Java using while, do/while,
and for statements
3The if Statement
test expression
if statements
true
if
false
Boolean decision operation
Processing Statements
next statements
if (test expression) statement
1 statement 2 //COMPOUND STATEMENTS statem
ent n //END IF
4Example
if ((xlty) (y!10)) int sum
xy System.out.println(x x) System.out.print
ln(y y) System.out.println(sum sum)
5The if/else Statement
if statements
true
test expression
next statements
if
false
else statements
else
6The if/else Statement
if (test expression) Statement
1 Statement 2 Statement n //END
IF else Statement 1 Statement
2 Statement n //END ELSE
7Example
String s1Andy String s2Janet if
(s1.equals(s2)) System.out.println(the strings
are equal) else System.out.println(the strings
are not equal)
8Nested ifs
temp gt 0
true
false
ice
temp gt 100
false
true
steam
water
9Example
if (year 1) System.out.println(Freshman) if
(year 2) System.out.println(Sophomore) if
(year 3) System.out.println(Junior) if
(year 4) System.out.println(Senior) if
(year gt 4) System.out.println(Graduate)
if (year gt 4) System.out.println(Graduate) else
if (year gt 3) System.out.println(Senior) else
if (year gt 2) System.out.println(Junior) else i
f (year gt 1) System.out.println(Sophomore) else
System.out.println(Freshman)
10Example
if (year gt 1) if (year gt 2) if (year gt 3)
if (year gt 4) System.out.println(Gradu
ate) else System.out.println(Senior)
else System.out.println(Junior) else
System.out.println(Sophomore) else
System.out.println(Graduate)
11The switch statement
selector case 1
true
case 1 statements
switch
break
false
selector case 2
true
case 2 statements
break
false
selector case n
true
case n statements
break
false
optional default statements
next statements
12The switch statement
switch(selector variable) case case 1
value case 1 statements break case case 2
value case 2 statements break case
case n value case n statements break //EN
D SWITCH
13Example
switch(letterGrade) case A
System.out.println(Excellent) break case
B System.out.println(Superior) break cas
e C System.out.println(Average) break ca
se D System.out.println(Poor) break case
E System.out.println(Try Again) break
14The default option
switch(selector variable) case case 1
value case 1 statements break case case 2
value case 2 statements break case
case n value case n statements break defaul
t default statements //END SWITCH
15Example
switch(letterGrade) case a case A
System.out.println(Excellent) break case
b case B System.out.println(Superior) b
reak case c case C System.out.println(Aver
age) break case d case D
System.out.println(Poor) break case
e case e System.out.println(Try
Again) break default System.out.println(No
match found for letterGrade)
16Looping Operations Iteration
- Iteration control structure causes the program
flow to go around in a loop - To prevent infinite looping, all iteration
control structures test a condition to determine
when to exit the loop - There are three kinds of tests
- Pretest test a condition before before each loop
is executed - Posttest test a condition after each loop is
executed - Fixed repetition the loop is executed for a
predetermined number of times
17The while loop
test expression
false
while
true
loop body
while (test expression) statement
1 statement 2 //LOOP BODY statement
n //END WHILE
18Example
int number 5 int sum 0 while(numbergt0) sum
number --number System.out.println(the sum
is sum)
19The do/while loop
do statement 1 statement
2 //LOOP BODY statement n //END
DO/WHILE while (test expression)
loop body
do
while
test expression
false
true
20Example
int number 5 int sum 0 do sumnumber --n
umber while(numbergt0) System.out.println(the
sum is sum)
21The for loop
test expression on counter
initialize counter value
false
for
true
Loop body
for (int counter initial value counter test
expression increment/decrement counter)
statement 1 statement 2 //LOOP
BODY statement n //END FOR
Increment/ decrement counter
22Example
System.out.println(\n\tNumber\tSquare\tCube) Sy
stem.out.println(\t------\t------\t------) for(i
nt count 1 count lt11 count) System.out.print
ln(\t count \t countcount \t
countcountcount)
23The break statement
while (test expression) statement
1 Statement 2 if (test expression) break
statement n //END WHILE NEXT STATEMENT
AFTER WHILE
24The break statement
int number 1 while (number lt 11) if (number
5) break System.out.println(number now is
number) number System.out.println(The
loop is terminated and the number is number)
25The continue statement
for (counter initial value counter test
expression increment/decrement counter)
statement 1 statement 2 if (test
expression) continue statement n //END
FOR NEXT STATEMENT AFTER FOR
26Example
int value 0 for(int number1numberlt11number
) if(number5) continue System.out.println(nu
mber now is number) valuenumber System.out.
println(The loop is terminated and the number
is number)
27(No Transcript)