You are on page 1of 8

INTRODUCTION TO COMPUTER SCIENCE

INTRODUCTION TO COMPUTER
SCIENCE
Topic 9: introduction to selection control

Topic 09 – Learning Outcomes

• To convert the algorithm to appropriate java program


• Use if and if-else statements to make choices.
• Construct appropriate conditions for control statements sing
relational operators.

KHE-LCD-SGD-00190
INTRODUCTION TO COMPUTER SCIENCE

Introduction
• Statements in Java can be either simple statements or compound
statements.
• Simple statements, such as assignment statements and subroutine call
statements, are the basic building blocks of a program.
• Compound statements, such as while loops and if statements, are used to
organize simple statements into complex structures, which are called
control structures because they control the order in which the statements
are executed.
• Control statements are the statements that define the flow of programs.
• There are 3 types of control statements in Java: Selection, iteration, and
jump statements.
• In this topic, we are going to convert all the selection pseudocode exercises
we have done in the topic 4 to java programs.

The Program Development Cycle

• Programs must be carefully designed before they are written.


• During the design process, programmers use tools such as pseudocode
and flowcharts to create models of programs.
• After designing the program, the programmer begins writing code in a high-
level language.
• Virtually all code contains syntax errors when it is first written, so the
programmer will typically spend some time correcting these.
• Once all of the syntax errors and simple typing mistakes have been
corrected, the program can be compiled and translated into a machine
language program (or executed by an interpreter, depending on the
language being used).

KHE-LCD-SGD-00190
INTRODUCTION TO COMPUTER SCIENCE

Let Us Convert All The Pseudocode Example


To Java Program;
• Example1: Simple Selection with null false branch (null Else statement)

Example2: Simple Selection (Simple IF


Statement)

Output for the condition true:

KHE-LCD-SGD-00190
INTRODUCTION TO COMPUTER SCIENCE

Example 3: Nested If Selection Structure

Example 3: Nested If Selection Structure

KHE-LCD-SGD-00190
INTRODUCTION TO COMPUTER SCIENCE

• If we follow the flow of execution, we see that the condition parcel


weight < 4.5 is tested. If the condition is true, then it will calculate
the shipping charge and display the display charge. However, if the
condition is false, it will test the next condition and this condition is
false then it will calculate the shipping charge of last option and
display shipping charge.
• After completing the program we need to test the program for the
test data which we had tested in desk checking algorithm (topic 3 –
page 13) shown below:

Example 4: Switch…case Structure


• Let us try to write java program for the following pseudocode that
uses case of structure.

10

10

KHE-LCD-SGD-00190
INTRODUCTION TO COMPUTER SCIENCE

Java Program

11

11

Java Program

• The case structure is a multiple alternative decision structure.


• It allows you to test the value of a variable or an expression and
then use that value to determine which statement or set of
statements to execute.
• The above java program shows an example of how a case structure
written in java.
• If the code is 1, addition is executed. If code is 2, subtraction is
executed is executed. If the code is 3, multiplication is executed.
• If the code is 4, division is executed. If the code contains none of
these values, the statement labelled default is executed.
• In this case, the statement sets message to Invalid code.

12

12

KHE-LCD-SGD-00190
INTRODUCTION TO COMPUTER SCIENCE

Java Program : Testing


• After writing the program, we test the program using the data we
tested in desk checking:

13

13

Java Program: Testing


• Java program output:

14

14

KHE-LCD-SGD-00190
INTRODUCTION TO COMPUTER SCIENCE

THE END

15

15

KHE-LCD-SGD-00190

You might also like