Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

SOKOTO STATE UNUVERSITY

DEPARTMENT OF COMPUTER SCIENCE


CSC 201 lecture 4

Conditional structures (Statement)

Conditional Statements are statements that are used to compare and select one or more instances
of a particular argument. The condition may have one or more side of arguments. According to
bohm and jocopinis work, programs can be written in terms of only three control structures- the
sequence structures, selection structures, and repetition structures.

The sequence structure

The sequence structure is built into java, unless specified otherwise the computer executes java
statement one after the other in the order in which they are written.

Selection structure

Java has three selection statement;

The if statement is a single-selection statement because it select or ignores a single action. If


statement either performs an action if the condition is true or skips the condition if it is false.
Below is the pseudocode and UML activity diagram for If statement;

The can be written in java as;

1
The if-else statement is called a double-selection because it selects between 2 different actions.
The if-else performs an action if a condition is true and performs a different action if the
condition id false. Below Is the pseudocode and UML activity diagram for if-else statement

This can be written in java as;

The switch statement is a multiple-selection statement because it select among many different
actions or group of actions depending on the value of an expression.

Repetition statements

Java provide 3 repetition statement (also called iteration or looping statements) that enable
programs to perform statement repeatedly as long as the condition (called loop-continuation
condition) remains true. The repetition statements are the while, do…while, and for. The while
and for statements performs actions (or group of action) in their bodies zero or more time. If the
loop-continuation condition is initially false, it will not execute. The do…while performs an
action (or group of actions) in its body one or more time.

2
EXAMPLES

E.g. 1. “This example allows user to type two integer numbers using scanner class and then
compare the two numbers using if-else statement and return the bigger among the provided
numbers.”

Output

E.g. 2 ““This example accept one integer number from user using scanner class and then
compare the number using if-else statement and return if the number is EVEN or ODD.””

3
Output

E.g. 3. “This example accepts two inputs from users. One of the inputs is the student level which
is declared to be of type int. another input is the department of student which is declared as
string data type. The if statement and some logical operators (.equals and &&) are used to know
if students is offering CSC201 or not.”

Output

4
E.g.4. “This example accept two input from users. One of the inputs is the student level which is
declared to be of type int. another input is the department of student which is declared as string
data type. The if statement and some logical operators (.equal and ||) are used to know if
students is offering CSC201 or not.”

Output

E.g. 5 “This example compare the two numbers using else-if statement and returns the biggest,
the smallest and if they same.”

5
Output

E.g. 6. “This example accept the CA and Exam of students, calculate the total and then return
the grade scored by the students using else-if statement.”

Output

6
Java Switch Statement

Switch is a conditional statement that executes one condition out of multiple conditions. The
switch statement is appropriate to use when there are many options to choice one out.

E.g1.

Output

E.g.2

7
Output

You might also like