0% found this document useful (0 votes)
9 views20 pages

Java Control Statements and Loops

Uploaded by

aodgamerthai
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views20 pages

Java Control Statements and Loops

Uploaded by

aodgamerthai
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1

Chapter 3: Control
Statements and Iterations
Lecture By: Amonrat Prasitsupparote
Outline 2

• Decision control statements


• Iteration control statements
• Summarize of Java Loop
• Arrays and ArrayList
If-else 3

Criteria Grade
if (condition){
80 <= score A
statement;
70 <= score < 80 B
} else if (condition){ 60 <= score < 70 C
statement; 50 <= score < 60 D
score < 50 E
} else {
statement;
}
Switch 4

switch (expression){
case value:
statement;
break;
case value:
statement;
break;
default:
statement;
}
While and do-while 5

while (condition){
statement;
}

do {
statement;
} while (condition);
For 6

for (initialization; condition; increment/decrement){


statement;
}
Summarize of Java Loop 7

Image Source: [Link]


Arrays 8

int[] x; or int []x; or int x[];

• Declare an array without reserve memory space.


• All elements are initialized with zero.
• Declare an array with reserve memory space 100 times of integer
size.
int[] x = new int[100];
• “new” use to create an array object.
• The length cannot change, otherwise
use array lists instead. int[] x = { 2, 3, 5, 7};
x = new int[] { 2, 3, 5, 7};
For-each Loop for Java Array 9

for (data_type variable : array){


statement;
}

• Print each of the array elements one by one.


Multi-Dimensional Arrays 10
ArrayList 11

• A resizable array, which is in [Link] package

• Add items – [Link](“VALUE”);


• Access an item – [Link](INDEX);
• Change an item – [Link](INDEX, “VALUE”);
• Remove an item – [Link](INDEX);
• Remove all items – [Link]();
• Size of an ArrayList – [Link]();
• Sort an ArrayList – [Link](ArrayList), which is in Collections class.
ArrayList (Cont.) 12
Practice 3-1 13

• Write a program to calculate the sum of scores with the following


conditions.
• The input is any number (shaded part).
• Repeat to enter the input until the input is 0.
• Display the error word in red when the input is not between 1 and 100.
• Display the summation of the score between 1-100 only when the input is 0.
Homework 3-1 14

• Write a program to calculate a commission from the net sale using


the following table, then display the output as shown in the following.
This program repeats to enter salary and net sale as inputs (shaded
part) until the input is 0.
Net Sale Commission
More than 200,000 15%
100,000 – 199,999 10%
50,000 – 99,999 5%
0-49,999 None
Homework 3-2 15

• Write a program that gets an integer as the input (shaded part) until
the user types “n” and then displays all numbers in ascending and
descending order.
• The [Link]() does not read a newline character in your input, thus
you should use [Link]() instead and convert it to the integer.
Homework 3-3 16

• Write a program to calculate a flat rate interest for a personal loan as


shown in the following. The input is the loan amount, interest rate,
and loan period (shaded part).
Homework 3-4 17

• Write a program to calculate a car parking charge


with the conditions in the following table. The input
is the in and out time (shaded part).
Time Charges per hour
Less than or equal to 15 minutes Free
More than 15 minutes but less than or equal to 3 hours 10
More than or equal to 3 hours but less than or equal to 6 hours 20
More than 6 hours 200 per day
Feedback 18
19
19

References
• C. S. Horstmann, Core Java Volume I--Fundamentals, 1, 11th edition. Pearson,
2020.
• K. Sierra and B. Bates, Head First Java, 2nd Edition, 2nd edition. Sebastopol, CA:
O’Reilly Media, 2005.
• K. Sierra and B. Bates, Head First Java: A Brain-Friendly Guide, 2nd edition.
O’Reilly Media, 2005.
• J. Bloch, Effective Java, 3rd edition. Boston: Addison-Wesley Professional, 2017.
• H. Schildt, Java: A Beginner’s Guide, Eighth Edition, 8th edition. New York:
McGraw-Hill Education, 2018.
• “Java OOPs Concepts - Javatpoint,” [Link].
[Link] (accessed Oct. 10, 2021).
• “Java Tutorial.” [Link] (accessed Oct. 10,
2021).
20

End of Chapter 3

You might also like