0% found this document useful (0 votes)
5 views

2 - Java Flow Control

Uploaded by

Nguyen Huy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

2 - Java Flow Control

Uploaded by

Nguyen Huy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Java Basic for Tester

Java Flow Control

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 1


Agenda

 Java if...else
 Java switch Statement
 Java for Loop
 Java for-each Loop
 Java while Loop
 Java break Statement
 Java continue Statement

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 2


Java if...else

In computer programming, it's often desirable to execute a certain section


of code based upon whether the specified condition is true or false (which
is known only during the run time).

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 3


Java if...else

Java if (if-then) Statement


In Java, the syntax of the if-then statement is:
if (expression) {
// statements
}
Here expression is a boolean expression. A boolean expression returns
either true or false.

 if the expression is evaluated to true, statement(s) inside the body of if


(statements inside parenthesis) are executed.
 if the expression is evaluated to false, statement(s) inside the body of if
are skipped from execution.
1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 4
Java if...else

Java if (if-then) Statement

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 5


Java if...else

Java if...else (if-then-else) Statement


In Java, the syntax of the if-then-else statement is:

if (expression) {
// codes
}
else {
// some other code
}

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 6


Java if...else

Java if...else (if-then-else) Statement

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 7


Java if...else

Java if..else..if Statement


In Java, we have an if...else...if ladder, that can be used to execute one
block of code among multiple other blocks.
if (expression1) {
// codes
}
else if(expression2) {
// codes
}.
.
else {
// codes
}
1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 8
Java if...else

Java if..else..if Statement

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 9


Java switch Statement

switch (variable/expression) {
case value1:
// statements of case1
break;

case value2:
// statements of case2
break;
.. .. ...
.. .. ...
default:
// default statements
}
1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 10
Java switch Statement

The Java switch statement only works with


 Java Primitive data types: byte, short, char, and int

 Java Enumerated types

 Java String Class

 Java Wrapper Classes: Character, Byte, Short, and Integer.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 11


Java switch Statement

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 12


Java for Loop

In computer programming, loops are used to repeat a specific block of code


until a certain condition is met (test expression is false).
The syntax of for loop in Java is:

for (initialization; testExpression; update)


{
// codes inside for loop's body
}

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 13


Java for Loop

 initialization expression: int i = 1.e


 test expression: i <=10
 update expression: ++i
1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 14
Java for-each Loop

class ForLoop { class AssignmentOperator {


public static void main(String[] args) { public static void main(String[] args) {

char[] vowels = {'a', 'e', 'i', 'o', 'u'}; char[] vowels = {'a', 'e', 'i', 'o', 'u'};

for (int i = 0; i < vowels.length; ++ i) { for (char item: vowels) {


System.out.println(vowels[i]); System.out.println(item);
} }
} }
} }

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 15


Java for-each Loop

The syntax of for each loop in Java is:

for(data_type item : collections) {


...
}

 collection - a collection or array that you have to loop through.


 item - a single item from the collections.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 16


Java while and do...while Loop

Java while Loop


The syntax of while loop in Java is:

while (testExpression) {
// codes inside the body of while loop
}

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 17


Java while and do...while Loop

Java while Loop

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 18


Java while and do...while Loop

Java do...while Loop


The syntax of the do...while loop.

do {
// codes inside body of do while loop
} while (testExpression);

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 19


Java while and do...while Loop

Java do...while Loop

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 20


Java break Statement

While working with loops, it is sometimes desirable to skip some statements


inside the loop or terminate the loop immediately without checking the
test expression.
The break statement in Java
terminates the loop immediately,
and the control of the program
moves to the next statement
following the loop.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 21


Java break Statement

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 22


Java continue Statement

The continue statement in Java skips the current iteration of a loop


(for, while, do...while, etc) and the control of the program moves to the end
of the loop. And, the test expression of a loop is evaluated.

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 23


Java continue Statement

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 24


Thank you

1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 25

You might also like