Vous êtes sur la page 1sur 15

Fundamentals of Programming

Programming Language (JAVA)

UNIT

6.3
PROGRAMMING LANGUAGE (JAVA)
Control Statements - Presentation 1

Control Statements

Presentation 1

6.3-1

Fundamentals of Programming

Programming Language (JAVA)

Slide 1

Programming Language (Java)

Unit 6.3-Control Statements

Presentation 1

Slide 2

Revision

1. List the 8 basic primitive data types.


2. Define variables.
3. List any 5 keywords used in Java
program.
4. List the two types of Typecasting.
5. Define operators.
6. List the three types of expression

Answers
1. The eight basic primitive data types byte, short, int, long, float, double,
char and Boolean.
2. Variable is the name given to the memory location where a value is
stored.
3. The five keywords used in Java are, class, byte, int, long and float.
4. The two types of Typecasting are Implicit and Explicit conversion.
5. An operator is a symbol that instructs Java compiler to perform an
operation, or action. Operators are used to compute, compare values,
and test logical conditions.

Control Statements

Presentation 1

6.3-2

Fundamentals of Programming

Programming Language (JAVA)

6. The three types of expression are,

Numerical Expressions

Assignment Expressions

Logical Expression

Slide 3

Objectives
At the end of this presentation, you will be able to:
Define branching statements and list their types
Define looping statements and list their types
Describe the structure and working of simple if
statements
Describe the structure and working of nested if
statements
Describe the structure and working of switch
statements

Start the class by explaining to the students that they will be learning about
the various branching and looping statements supported by Java.

Control Statements

Presentation 1

6.3-3

Fundamentals of Programming

Programming Language (JAVA)

Slide 4

Introduction Control Statements

Control statements are used to change the


sequential flow of a program.

These set of statements are executed based


on a condition.

Java supports two types of control statements:

Branching statement
Looping statement

Teaching Tip
Tell the students that in day-to-day life, decisions are made based on certain
condition and certain actions are performed. Give some examples of real-time
decisions. For example, if you miss the bus, you go by taxi.
Here the decision to go by taxi is taken if the bus is missed (condition). Ask
the students to state some similar examples.
Similarly, in programs, control statements are used to execute some
statements based on a condition.
Slide 5

Branching Statements

Used to evaluate expressions and direct the


program execution based on the result.

The branching statements supported by Java


are:
if
switch

Explain the branching statements and the branching statements available in


Java.

Control Statements

Presentation 1

6.3-4

Fundamentals of Programming

Programming Language (JAVA)

Ask the students to perform the Self-Check Exercise 6.3.1 given in the
Student Module.

Slide 6

Single if Statement

Used to execute a set of statements when the


given condition is satisfied.
Syntax
if (<condition>)
{
<Conditional statements>;
}

Conditional statements within the block are


executed when the condition in the if
statement is satisfied.

Before explaining the single if statement, ask the students to perform the SelfCheck Exercise 6.3.1 given in the Student Module.
Teaching Tip
Explain the single if statement with the following example.
If (miss the bus)
{
take a taxi;
}
Explain the best programming practice given under the single if statement

Control Statements

Presentation 1

6.3-5

Fundamentals of Programming

Programming Language (JAVA)

Slide 7

Hands-On!

Program InputValue.java illustrates the


execution of a simple if statement. The
program checks whether the given number is
greater than 100.

Demonstrate the data file InputValue.java to explain the if statement.


Slide 8

Hands-On!

Program Marks.java illustrates the execution


of a simple if statement based on two
conditions. This program checks whether the
marks is between 90 and 100 to print the
Grade.

Demonstrate the data file Marks.java to explain the if statement. This


program prints the grade.

Control Statements

Presentation 1

6.3-6

Fundamentals of Programming

Programming Language (JAVA)

Slide 9

The if-else Statement

Executes the set of statements in if block,


when the given condition is satisfied.

Executes the statements in the else block,


when the condition is not satisfied.

Syntax
if (<condition>)
{
<Conditional statements1>;
}
else
{
<Conditional statements2>;
}

Teaching Tip
Explain if-else with the following example.
if(miss bus)
{
go by taxi
}
else
{
go by bus
}
Here if you miss the bus, the decision is to go by taxi. Otherwise you can go
by bus itself.
Explain the Best programming practice for if-else given for Student
Module.
Explain the Note for if-else given for Student Module.

Control Statements

Presentation 1

6.3-7

Fundamentals of Programming

Programming Language (JAVA)

Slide 10

Hands-On!

Program Sample.java illustrates the use of the


if-else statement. This program accepts a
number, checks whether it is less than 0 and
displays an appropriate message.

Demonstrate the data file Sample.java to explain the if-else statement.


Slide 11

Activity 6.3.1
Step 1: Open the data file InpSample.java.
Step 2: Run and observe the output.
Step 3: Draw the appropriate flowchart for the
program.

Ask the students to perform the given activity.

Control Statements

Presentation 1

6.3-8

Fundamentals of Programming

Programming Language (JAVA)

Slide 12

Lab Exercise
1.

Accept two numbers from the keyboard and


find the highest among them.

Refer to the solution for Lab Exercise in the Teacher Module.

Control Statements

Presentation 1

6.3-9

Fundamentals of Programming

Programming Language (JAVA)

Slide 13

Lab Exercise
2.

Accept a number from the keyboard and


check whether it is divisible by 5.
Hint: Use the modulus operator, %, to
check the divisibility.

Refer to the solution for Lab Exercise in the Teacher Module.

Slide 14

Lab Exercise
3.

Accept a number from the keyboard and


check whether the number is an odd
number or an even number.
Hint: Use the modulus operator, %, to
check the divisibility.

Refer to the solution for Lab Exercise in the Teacher Module.

Control Statements

Presentation 1

6.3-10

Fundamentals of Programming

Programming Language (JAVA)

Slide 15

Nested if Statement

The if statements written within the body of


another if statement to test multiple conditions
is called nested if.
Syntax
if (<Condition 1>)
{
if (<Condition 2>)
{
<Conditional statements1>;
}
else
{
<Conditional statements2>;
}
}
else
{
<Conditional statements3>;
}

Inner if
condition

Outer if
condition

Teaching Tip
In the example given in the slide, there are two if conditions, inner if and outer
if condition. The control moves to the inner if, when Condition 1 is satisfied.
When Condition 1 is not satisfied, Conditional statements 3 are executed.
Explain the Note for Nested if statement given Student Module.

Slide 16

Hands-On!

The program Highest.java illustrates the use of


nested if statements. The program accepts
three integers from the keyboard and finds
the highest of the three.

Demonstrate the data file Highest.java to explain the nested if statement.


Type java Highest 8 2 4 in the command prompt, for example. Ask the students
to observe the output.

Control Statements

Presentation 1

6.3-11

Fundamentals of Programming

Programming Language (JAVA)

Slide 17

Lab Exercise 4

Accept three numbers from the keyboard


and find the greatest among them.

Refer to the solution for Lab Exercise in the Teacher Module.

Slide 18

Lab Exercise
5.

During a special sale at a mall, a 5% discount


is given on purchases above RM100.00.
Write a program to accept the total purchase
amount and calculate the discounted price.

Example

Enter the amount of purchase in RM: 2000


Discount price = 2000-(2000*5/100) = 1900

z
Refer to the solution for Lab Exercise in the Teacher Module.

Control Statements

Presentation 1

6.3-12

Fundamentals of Programming

Programming Language (JAVA)

Slide 19

The switch Statement

Is a multi-way branching statement.

Contains various case statements.

The case statements are executed based on


the value of the expression.

A break statement passes the control outside


switch structure.

Teaching Tip
Tell the students that an integer or a character constant follows the keyword
case. Based on the value returned from the expression in the switch, the
control is passed to the respective matching case.
Explain the Note for the break and switch statement given in the Student
Module.

Slide 20

Hands-On!

Program SwitchDemo.java illustrates switch


case execution. In the program, the switch
takes an integer value as input and displays
the month based on the integer entered.

Demonstrate the data file SwitchDemo.java to explain the switch statement.


Typing java SwitchDemo 5 for example.

Control Statements

Presentation 1

6.3-13

Fundamentals of Programming

Programming Language (JAVA)

Slide 21

Hands-On!

The program SampleSwitch.java illustrates


switch case execution. The program checks
whether the given character is a vowel.

Demonstrate the data file SampleSwitch.java to explain switch case


execution.

Slide 22

Lab Exercise
6.

Write a program to accept the day of the


week and print the day.

Example
1 Sunday
5 Thursday

Refer to the solution for Lab Exercise in the Teacher Module.

Control Statements

Presentation 1

6.3-14

Fundamentals of Programming

Programming Language (JAVA)

Slide 23

Summary
In this presentation, you learnt the following:
Control statements are used to change the
sequential flow of a program.
The two types of control statements are
branching statements and looping statements.
Branching statements are used to evaluate
expressions and direct the program execution
based on the result.
In Java, the branching statements include if
and switch statements.

Summarise the points given in the slide.

Slide 24

Assignment
1. Write short notes on control statements.

Answer
1. Control statements are used to change the sequential flow of a
program. Java supports two types of control statements. They are used
to alter the flow of the program.

Branching statement

Looping statement

Control Statements

Presentation 1

6.3-15

Vous aimerez peut-être aussi