Sie sind auf Seite 1von 31

FUNDAMENTAL OF JAVA PROGRAMMING

CONTROL STRUCTURES

Course Learning Outcomes :


Upon completion of this course, the students should be able to:
1. Describe the features and basic concepts of Java language. 2. Write, compile Java source code and interpret Java byte code using Java Development Kit (JDK). 3. Implement the key of OOP concepts: classes and objects, inheritance and polymorphism.

4. Incorporate exception handling in Java Programming.

Objectives :
Explain selection statements in Java programs if if.else nested if switchcase Explain looping statements in Java programs while for do-while Write a program using control structures

Introduction

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:
1.
2.

Selection statement
Looping statement

Selection Statement

Used to evaluate expressions and direct the program execution based on the result. The selection statements supported by Java are:

if switch

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.

Activity 1 :

Draw the appropriate flowchart for the program :

The program checks whether the given number is greater than 100.
int num; num = Integer.parseInt(args[0]); if(num<100) { System.out.println("Number is less than 100"); }

Activity 2 :

Draw the appropriate flowchart for the program :

This program checks whether the marks is between 90 and 100 to print the Grade.

int marks; marks = Integer.parseInt(args[0]); if ((marks>90) && (marks<100)) System.out.println("Grade is A");

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.
if (<condition>) { <Conditional statements1>; } else { <Conditional statements2>; }

Syntax

Activity 3 :

Draw the appropriate flowchart for the program : This program accepts a number, checks whether it is less than 0 and displays an appropriate message.
int num; num=Integer.parseInt(args[0]); if (num<0) System.out.println("Negative"); else System.out.println("Positive");

Lab Execise :
1.

Accept two numbers from the user and find the highest among them.
Accept a number from the user and check whether it is divisible by 5. Hint: Use the modulus operator, %, to check the divisibility. Accept a number from the user and check whether the number is an odd number or an even number. Hint: Use the modulus operator, %, to check the divisibility.

2.

3.

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

Activity 4:

Draw the appropriate flowchart for the program : The program accepts three integers from the user and finds the highest of the three.
if(x>y) { if(x>z) System.out.println(x + " is the highest"); else System.out.println(z + " is the highest"); } else { if (y>z) System.out.println(y + " is the highest"); else System.out.println(z +" is the highest"); }

Lab Execise :
4. 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 Discounted price = 2000-(2000*5/100) = 1900

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.

Activity 5:

Draw the appropriate flowchart for the program : In the program, the switch takes an integer value as input and displays the month based on the integer entered.
int month=Integer.parseInt(args[0]); switch (month) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 5: System.out.println("May"); break; case 6: System.out.println("June"); break; default: System.out.println("wrong choice"); }

Activity 6:

Draw the appropriate flowchart for the program : The program checks whether the given character is a vowel.
char ch = 'i'; switch (ch) { case 'a': case 'A': System.out.println("It break; case 'e': case 'E': System.out.println("It break; case 'i': case 'I': System.out.println("It break; default: System.out.println("It is not a vowel }

is a vowel");

is a vowel");

is a vowel");

");

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

To be continued

LOOPING STATEMENT

Looping Statements

Used to execute a set of instructions repeatedly, as long as the specific condition is satisfied. The looping statements available in Java are:

for while do-while

for Loop

This looping statement enables you to repeat a set of instructions based on the condition. Used when the number of loops is known before the first loop.
for(initialisation;condition; incrementation/ decrementation) { //loop statements }

Syntax

for Loop(contd)

The for loop has three parts:

Initialisation: The initial value of the loop control variable is set here. Condition: The loop condition is specified and the loop statements are executed as long as this condition is satisfied. Incrementation / Decrementation: The initial value of the loop control variable is either incremented or decremented each time the loop gets executed.

Activity 7: for loop


Draw the appropriate flowchart for the program : Write a possible output from this snippet code.
int value =3; int num,multiple; System.out.println("Multiples of 3:\n"); for (num = 1; num <= 10; num++) { multiple=num*value; System.out.println("Multiples of 3:"+multiple+" "); }

for(int j=0;j<5;j++) { System.out.println(j ); } System.out.println("Outside the for loop");

while Loop

Is a looping statement that enables you to repeat a set of instructions based on a condition. Used when the number of loops is not known before the first loop.
Syntax
<Initialise variable>; while(condition) { //loop statements <Increment/decrement variable>; }

Activity 8: while loop


Draw the appropriate flowchart for the program : Write a possible output from this snippet code.
int sum=0; int i=0; while(i<10) { sum=sum+i; i++; } System.out.println("The total of first 10 numbers is: " +sum);

Using a while loop generate the series 0, 1, 1, 2, 3, 5, 8

do-while Loop

The do-while loop is similar to the while loop. The difference only in the location where the condition is checked. In do-while, condition is checked at the end of loop.

Syntax do { //loop statements } while(condition);

Activity 8: do-while loop


Draw the appropriate flowchart for the program : Write a possible output from this snippet code.
int x=20; do { System.out.print(x); x=x+1; } while(x<15);

Write a program that reads an integer and prints the sum. For example, If the input is 53 Output 8

break and continue statement

The break and continue statements in Java allow transfer of control from one statement to another. The break statement causes termination of the loop and transfers the control outside the loop. The continue statement will transfer the control to the beginning of the loop.

Activity 9: break statement


Draw the appropriate flowchart for the program : Write a possible output from this snippet code.
int i; System.out.println(Getting into the loop); for(i=1;i<50;i++) { if(i==10) break; // exit from for loop System.out.print(i+" "); } System.out.println(Coming out of the loop);

Activity 9: continue statement


Draw the appropriate flowchart for the program : Write a possible output from this snippet code.
for(int i=1;i<=20;i++) { if(i%2!=0) continue; //next iteration System.out.print(i+" "); }

Summary
In this session, you learnt the following:

In Java, the looping statements include while, do-while and for statements. The break statement will transfer the control to the statement outside the loop. The continue statement will transfer the control to the beginning of the loop.

Das könnte Ihnen auch gefallen