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

4.3 Use of Control Structures 23

Uploaded by

syahmimoktar2004
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)
57 views

4.3 Use of Control Structures 23

Uploaded by

syahmimoktar2004
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
You are on page 1/ 56

4.

0 Java Language

4.1 4.2 4.4 Arrays 4.5 4.6 Java


Introduction Identifiers, 4.3 Use of
Methods Programs
to Java data types, Control
Program operators Structures
and
expressions
4.3 Use of Control Structures
• Learning Outcomes
At the end of this topic, you should be able to:

a) Construct program segment by using sequence control structure. (T)


b) Construct program segment by using selection control structure. (T)
c) Construct program segment by using repetition (while, for) counter-controlled or
sentinel-controlled structure. (T)
d) Construct program by using sequence control structure. (P)
e) Construct program by using selection control structure. (P)
f) Construct program by using repetition (while, for) counter-controlled or
sentinel-controlled structure. (T)
g) Explain types of programming errors: syntax, run-time and logic. (L)
Sequence Control Structure
• Java program using sequence structure consists a
series of Java statements that will be executed
sequentially from start to the end of program.
• It will not involve any selection or repetition statements
such as if, if-else, if-else-if, while, or for.
Basic Java Statements
• Sequence program involves:
1. Input statement
2. Output statement
3. Assignment statement

* Semicolon (;) which is known as the statement


terminator, must be written at the end of each
Java statements.
Write the program to Exercise
calculate the area of circle import java.util.Scanner;
(area = 3.142 x r x r) class Circle
{
I: radius public static void main (String[]args)
P: Calculate area {
O: area Scanner read = new Scanner (System.in);

Start double r = read.nextDouble();


Input radius double area = 3.142 * r * r;
area = 3.142 x r x r System.out.println("Area = " + area);
Output area }
Stop }
4.3 Use of Control Structures
• Learning Outcomes
At the end of this topic, you should be able to:

a) Construct program segment by using sequence control structure. (T)


b) Construct program segment by using selection control structure. (T)
c) Construct program segment by using repetition (while, for) counter-controlled or
sentinel-controlled structure. (T)
d) Construct program by using sequence control structure. (P)
e) Construct program by using selection control structure. (P)
f) Construct program by using repetition (while, for) counter-controlled or
sentinel-controlled structure. (T)
g) Explain types of programming errors: syntax, run-time and logic. (L)
Selection Control Structure
• Java program using selection structure will execute
certain statement(s) based on a certain condition.
• There are three (3) structures of selection based on the
requirements of the problem:
– Single selection
– Dual selection
– Multiple selection
Selection Statements

• Selection structure can be represented using three


(3) Java statements:
1. single selection → if
2. dual selection → if - else
3. multiple selection → if - else - if
Selection Control Structure and
Java Statements
Control Java Statement
Structure
Single selection if (mark > 40)
System.out.print("PASS");

Dual selection if (mark >= 40)


System.out.print("PASS");
else
System.out.print("FAIL");
Selection Control Structure and
Java Statements
Control Structure Java Statement
Multiple Selection if (number > 0)
System.out.print("positive");
else if (number < 0)
System.out.print("negative");
else
System.out.print("zero");
Using if statement

• General format:

if(condition)
<statements>;
if statement
• Example 1
Create a program segment that • Pseudocode
will accept student’s mark. If Begin
the mark is greater than 40, input mark
print the message "PASS". if (mark > 40)
I: mark print "PASS"
End
P: determine message based
on mark
O:"Pass"
if statement
ALERT!
• Program segment ◼ Condition in if
statement must be
double mark = sc.nextDouble();
written in a bracket

◼ No semicolon in if
if(mark > 40) line (i.e after the
System.out.print("PASS"); condition)

◼ Semicolon at the end


of action statements
mark the end of if
statement
if statement
• Example 2 • Pseudocode
If an employee works more than 40
Start
hours a week, compute his
overtime pay which is half of his input hour , salary
salary. if (hour > 40)
I: hour, salary overtime = ½ x salary
P: determine overtime based on print overtime
hour and salary Stop
O: overTime
if statement
• Program segment

double hour = sc.nextDouble();


double salary = sc.nextDouble();
{ } must be
used when
if(hour > 40) action part has
{ more than one
double overtime = 0.5 * salary; statement
System.out.print(overtime);
}
if statement
• Example 3
• Pseudocode
Calculate area of circle when
Begin
radius is greater than 0 and
display the area. input radius
if (radius > 0)
I: radius area = 3.14 x radius2
P:determine area based display area
on radius End
O: area
if statement
• Program segment

double radius = sc.nextDouble();


{ } must be
if(radius > 0) used when
{ action part
double area = 3.14 * radius * radius; has more
System.out.print(area); than one
} statement
Using if-else statement
• General format:

if(condition)
<statement1>;
else
<statement2>;
end if
if-else statement
• Example 1 • Pseudocode
If a student’s mark is 40 or Start
above, print the message input mark
"PASS". Or else, print the if (mark >= 40)
message "FAIL".
print "PASS"
I: mark
else
P: determine message based
on mark print "FAIL"
O: "PASS" or "FAIL" end if
Stop
if-else statement
• Program segment

double mark = sc.nextDouble();

if(mark >= 40)


System.out.print("PASS");
else
System.out.print("FAIL");
if-else statement
• Example 2
Create a program that will accept • Pseudocode
two numbers from user. Display a
Begin
message "First number is bigger" if
first number is bigger than second input num1, num2
number. Otherwise display a if (num1 > num2)
message "Second number is bigger". print "First number is bigger"
I: num1, num2 else
print "Second number is bigger"
P: determine output based on
input end if
End
O: "First number is bigger"or
"Second number is bigger"
if-else statement
• Program segment

double num1 = sc.nextDouble();


double num2 = sc.nextDouble();
if(num1 > num2)
System.out.print("number 1 is bigger");
else
System.out.print("number 2 is bigger");
if-else statement
• Example 3 • Pseudocode
Calculate area of circle when Begin
radius is greater than 0 and input radius
display the area. If not, display if (radius > 0)
"Invalid radius".
area = 3.14 x radius2
print area
else
print "Invalid radius"
end if
End
if-else statement
• Program segment

double radius = sc.nextDouble();

if(radius > 0)
{
double area = 3.14 * radius * radius;
System.out.print(area);
}
else
System.out.print("Invalid radius");
Using if-else-if statement
• General format:

if(condition1)
<statement1>;
else if(condition2)
<statement2>;
else
<statement3>;
end if
if-else-if statement
• Example 1 • Pseudocode
Create a program to accept a Begin
number from user, and to determine input number
whether the number is positive, if (number > 0)
negative or zero. print "positive"
I:number else if (number < 0)
P:determine message based on print "negative"
number else
O:"positive" or "negative" or print "zero"
"zero" end if
End
if-else-if statement
• Program segment

double number = sc.nextDouble();

if(number > 0)
System.out.println("positive");
else if(number < 0)
System.out.println("negative");
else
System.out.println("zero");
if-else-if statement
• Example 2
The program will be assigned based on
the value of a code. Display the name of • Pseudocode
the program that will be assigned based Begin
on the following table: input code
Code Program if (code = 1)
1 Degree in Multimedia print "Degree in Multimedia"
2 Degree in Networking else if (code = 2)
print "Degree in Networking"
3 Degree in Robotics
else if (code = 3)
print "Degree in Robotics"
I:code end if
P:determine message based on code End
O:"Multi" or "Netw"or "Robo"
if-else-if statement
• Program segment
public static void main(String[] args)
{
int code = sc.nextInt();
if(code==1)
System.out.print("Degree in Multimedia");
else if(code==2)
System.out.print("Degree in Networking");
else if(code==3)
System.out.print("Degree in Robotics");
}
4.3 Use of Control Structures
• Learning Outcomes
At the end of this topic, you should be able to:

a) Construct program segment by using sequence control structure. (T)


b) Construct program segment by using selection control structure. (T)
c) Construct program segment by using repetition (while, for) counter-controlled or
sentinel-controlled structure. (T)
d) Construct program by using sequence control structure. (P)
e) Construct program by using selection control structure. (P)
f) Construct program by using repetition (while, for) counter-controlled or
sentinel-controlled structure. (T)
g) Explain types of programming errors: syntax, run-time and logic. (L)
Repetition Control Structure
• Java program using repetition structure will execute certain
statement(s) repeatedly as long as a certain condition is true.

• There are 2 type of repetition control structure;


1. Counter Controlled – (3, 5, 10 times)
2. Sentinel Controlled – (-1, 999, 0)

• Repetition structure can be represented using two (2) statements:


1. while & for - Counter Controlled
2. while - Sentinel Controlled
Repetition Control Structure
Example 1 (Counter Controlled)
Pseudocode
Calculate the area of 10 rectangles.
Begin
counter = 1
Tips:
while (counter <=10)
- Plan the processes to calculate
area of one rectangle only input length, width
- Repeat the processes 10 times area = length X width
print area
I: length, width counter = counter + 1
P:repeat reading input, calculate repeat while
area, 10 times end while
O: area
End
Counter Controlled - using while statement
import java.util.Scanner;
class JavaProgram {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int counter = 1;
double length=0, width=0, area=0;

while(counter <= 10)


{
length=sc.nextDouble();
width=sc.nextDouble();
area = length * width;
System.out.print(area);
counter = counter + 1;
}
}
}
About while statement

• while statement checks


the condition before
enter the loop.
• If the condition is true,
loop body will be
executed
• If condition is false, loop
will stop
Counter Controlled - using for statement
import java.util.Scanner;
class JavaProgram{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
double length=0, width=0, area=0;

for(int counter=1;counter<=10;counter=counter+1)
{
length=sc.nextDouble();
width=sc.nextDouble();
area = length * width;
System.out.print(area);
}
}
Note:
Counter+1 = Counter++
About for statement i-1 = i--

• for statement combines all the three parts of looping structure in one
statement:

for (int counter=1; counter<=10; counter=counter+1)

Initialization Condition/ Updating/


Evaluation Iteration
About for statement
• for statement checks
the condition before
enter the loop.
• All the operation will be
done in the same
sequence as while
structure;
• hence flow chart the
same.
Exercise 2 - Calculate the total and average of the first 10 odd numbers.
(1,3,5,7,9,11,13,15,17,19)
Pseudocode
Begin
odd = 1
while odd <= 19
total = total + odd
odd = odd + 2
end while
average = total / 10
print total, average
end
Program - using while statement
import java.util.Scanner;
class JavaProgram{
public static void main(String[] args){
int odd=0;
double total=0, average=0;
odd = 1;
while (odd <= 19){
total = total + odd;
odd = odd + 2;
}
average = total / 10;
System.out.println("Total = "+ total);
System.out.println("Average = "+ average);
}
}
Program - using for statement
import java.util.Scanner;
class JavaProgram{
public static void main(String[] args){
int total=0;
double average=0;
for(int odd=1;odd<=19;odd=odd+2){
total = total + odd;
}
average = total / 10;
System.out.println("Total = "+ total);
System.out.println("Average = "+ average);
}
}
Repetition Control Structure
(Sentinel Controlled)
• The loop will stop when a sentinel value is reached.
• sentinel value is a special value (called signal value, dummy value or
flag value) to indicate end of data entry such as: 0, -1, 999, ‘Y’, ‘n’,
‘YES’,...
Repetition Control Structure
(Sentinel Controlled)
Exercise Pseudocode
Begin
Calculate the total and average mark, total, average, i = 0
of marks entered by user until enter mark
the user key in -1. while (mark != -1)
total = total + mark
i = i+1
enter mark
repeat while
end while
average = total/i
display total, average
End
Sentinel Controlled - While Statement
System.out.println ("Enter -1 to stop, enter mark");
mark = read.nextDouble();
while(mark != -1)
{
total = total + mark;
i = i + 1;
System.out.println ("Enter -1 to stop, enter mark");
mark = read.nextDouble();}
average = total/i;
System.out.println("total = " + total);
System.out.println("average = " + average);
}
}
4.3 Use of Control Structures
• Learning Outcomes
At the end of this topic, you should be able to:

a) Construct program segment by using sequence control structure. (T)


b) Construct program segment by using selection control structure. (T)
c) Construct program segment by using repetition (while, for) counter-controlled or
sentinel-controlled structure. (T)
d) Construct program by using sequence control structure. (P)
e) Construct program by using selection control structure. (P)
f) Construct program by using repetition (while, for) counter-controlled or
sentinel-controlled structure. (T)
g) Explain types of programming errors: syntax, run-time and logic. (L)
4.3 Use of Control Structures

- Continue in practical class …

- Practical Test 1
4.3 Use of Control Structures
• Learning Outcomes
At the end of this topic, you should be able to:

a) Construct program segment by using sequence control structure. (T)


b) Construct program segment by using selection control structure. (T)
c) Construct program segment by using repetition (while, for) counter-controlled or
sentinel-controlled structure. (T)
d) Construct program by using sequence control structure. (P)
e) Construct program by using selection control structure. (P)
f) Construct program by using repetition (while, for) counter-controlled or
sentinel-controlled structure. (T)
g) Explain types of programming errors: syntax, run-time and logic. (L)
4.3 Use of Control Structures

- Continue in practical class …

- Practical Test 2
4.3 Use of Control Structures
• Learning Outcomes
At the end of this topic, you should be able to:

a) Construct program segment by using sequence control structure. (T)


b) Construct program segment by using selection control structure. (T)
c) Construct program segment by using repetition (while, for) counter-controlled or
sentinel-controlled structure. (T)
d) Construct program by using sequence control structure. (P)
e) Construct program by using selection control structure. (P)
f) Construct program by using repetition (while, for) counter-controlled or
sentinel-controlled structure. (T)
g) Explain types of programming errors: syntax, run-time and logic. (L)
4.3 Use of Control Structures

- Continue in tutorial class …


4.3 Use of Control Structures
• Learning Outcomes
At the end of this topic, you should be able to:

a) Construct program segment by using sequence control structure. (T)


b) Construct program segment by using selection control structure. (T)
c) Construct program segment by using repetition (while, for) counter-controlled or
sentinel-controlled structure. (T)
d) Construct program by using sequence control structure. (P)
e) Construct program by using selection control structure. (P)
f) Construct program by using repetition (while, for) counter-controlled or
sentinel-controlled structure. (T)
g) Explain types of programming errors: syntax, run-time and logic. (L)
Programming Errors
Means an error which occurs during the development of a
computer program and which would, when in operation, result in
a malfunction or incorrect operation of computer system.

There are 3 types of errors:

1. Syntax / Compile-time Error


2. Run-time Error
3. Logic Error
Syntax / Compile-time Error
• Occurs when the syntax of the programming language is
violated.
• Common examples are:
– Misspelled variable and function names
– Missing semicolons
– Improperly matches parentheses, square brackets, and curly braces
– Incorrect format in selection and loop statements
• statement cannot be translated, and program cannot be
executed.
Run-time Error
• Occurs when a program with no syntax errors asks the computer
to do something that the computer is unable to reliably do.
• Common examples are:
– Trying to divide by a variable that contains a value of zero
– Trying to open a file that doesn't exist
– Infinite loop
Logic Error
• Occurs when there is a design flaw in your program.
• Common examples are:
– Multiplying when you should be dividing
– Adding when you should be subtracting
– Opening and using data from the wrong file
– Displaying the wrong message
Logic Error

• Very difficult to detect - it does not cause run-time error


and does not display error messages.
• The only sign of logic error is incorrect output
• Can be detected by testing the program thoroughly,
comparing its output to manually calculated results
Checkpoint 1

1. What is syntax error?


2. What is run-time error?
3. What is logic error?

You might also like