You are on page 1of 17

Fundamentals of Programming

Programming Language (JAVA)

6.3
UNIT

PROGRAMMING LANGUAGE (JAVA)


Control Statements - Presentation 2

Control Statements

Presentation 2

6.3-16

Fundamentals of Programming

Programming Language (JAVA)

Slide 1

Programming Language (Java)

Unit 6.3-Control Statements

Presentation 2

Slide 2

Objectives
At the end of this presentation, you will be able to:

Describe the structure and working of a for,


while and do-while loops

Explain the need for break and continue


statements

Start the class by explaining to the students that they will be learning about
the various looping statements supported by Java and the need for break
and continue statements in programs.

Control Statements

Presentation 2

6.3-17

Fundamentals of Programming

Programming Language (JAVA)

Slide 3

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

Teaching Tip
Explain to the students that loops are used when you want the program to
perform the same task repeatedly based on a condition.
Ask the students to perform the Self-Check Exercise 6.3.2 given in the
Student Module.

Slide 4

The 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.

Syntax

for(initialisation;condition; incrementation/
decrementation)
{
//loop statements
}

Teaching Tip
Inform them that the for loop is a well-formatted loop. The number of loops
or the number of times the statements are to be executed is known even
before the first loop.

Control Statements

Presentation 2

6.3-18

Fundamentals of Programming

Programming Language (JAVA)

Slide 5

The 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.

Explain the three parts of the for loop using the given slide.
Note:
The three parts of the for loop are separated by a semicolon.
Slide 6

Hands-On!

The program LoopDemo.java illustrates the


working of a for loop.

Demonstrate the data file LoopDemo.java to explain the working of the for
Loop

Control Statements

Presentation 2

6.3-19

Fundamentals of Programming

Programming Language (JAVA)

Slide 7

Activity 6.3.2
Step 1: Open the file LoopSample.java from the
student data file.
Step 2: Run and observe the output.
Step 3: Draw the appropriate flowchart for the
program.

Ask the students to perform the given activity.


The flowchart is as follows:

Control Statements

Presentation 2

6.3-20

Fundamentals of Programming

Programming Language (JAVA)

Slide 8

Lab Exercise
7.

Write a program to print the following triangle


of numbers:
1
12
123
1234
12345

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

Slide 9

Lab Exercise
8.

Write a program that will accept a number


from the user and print the multiplication
table for that number in the proper format.
Example
If the input is 2, print the 2 tables in the
format:
2* 1=2
..
..
2* 10=20

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

Control Statements

Presentation 2

6.3-21

Fundamentals of Programming

Programming Language (JAVA)

Slide 10

Lab Exercise
9.

Display the following series using for loop:

Series 1
1
12
123
1234

Series 2
1, 2, 3, 5, 7n
Series 3
123
456
789

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

Slide 11

The 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>;
}

Teaching Tip
Tell the students that the while loop is similar to the for loop. They can use
the while loop when the number of loops is not known before the first loop.

Control Statements

Presentation 2

6.3-22

Fundamentals of Programming

Programming Language (JAVA)

Slide 12

Hands-On!

Program WhileDemo.java illustrates the working


of a while loop.

Demonstrate the data file WhileDemo.java to explain the working of a while


loop.

Slide 13

Activity 6.3.3 (a)


Step 1:Open the file oddno.java from the
student data file.
Step 2:Modify the code line 7 and 10 such that
the output of the program is

1 3 5 7 9 11

Ask the students to perform the given activity.


1. Modify line 7 as while(count<=11)
2. Modify line 10 as count =count+2;
3. Save and execute to observe the output.

Control Statements

Presentation 2

6.3-23

Fundamentals of Programming

Programming Language (JAVA)

Slide 14

Activity 6.3.3 (b)


Step 1:Open the file SqNatNo.java from the
student data file.
Step 2:Modify the code line 8 to complete the
condition to repeat the loop till it finds
the square of first five natural numbers
(1 to 5).

1. Ask the students to open the data file SqNatNo.java and run to observe
the output.
2. Modify line 8 as while(num<=5)
3. Modify line 10 as square =num*num;
4. Save and execute to observe the output.

Slide 15

Activity 6.3.3 (b) (Contd)


Step 3:Modify the code line 10 to find the
square of the number.
Step 4:The output of the program should be as
follows:

The square of 1 is: 1


The square of 2 is: 4
The square of 3 is: 9
The square of 4 is: 16
The square of 5 is: 25

This slide is a continuation of the instructions in Activity 6.3.3(b).

Control Statements

Presentation 2

6.3-24

Fundamentals of Programming

Programming Language (JAVA)

Slide 16

Activity 6.3.3(c)
1.
2.

Open the data file PostDecrement.java.


Modify the code line 7 and 10 such that the
output of the program will be 2, 1, 0, -1.

1. Ask the students to open the data file postdecrement.java and run to
observe the output.
2. Modify line 7 as while(count>=-1)
3. Modify line 10 as count =count-1;
4. Save and execute to observe the output.
Slide 17

Lab Exercise
10. Using a while loop generate the series

0, 1, 1, 2, 3, 5, 8

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

Control Statements

Presentation 2

6.3-25

Fundamentals of Programming

Programming Language (JAVA)

Slide 18

The 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);

Explain the do-while loop and its syntax with the help of the given slide.

Slide 19

Hands-On!

Program DoWhileDemo.java illustrates the


working of a do-while loop.

Demonsrtate the data file DoWhileDemo.java to explain the working of a dowhile loop

Control Statements

Presentation 2

6.3-26

Fundamentals of Programming

Programming Language (JAVA)

Slide 20

Activity 6.3.4(a)
Step 1:Open the data file Count_1.java
Step 2: Fill in the blanks with appropriate
code to increment the variable
count.
Step 3: Save, compile and execute the
code.

Ask the students to perform the given activity.

Slide 21

Activity 6.3.4(b)
Step 1:Open the data file Count_2.java.
Step 2: Read the program and write the output.

Ask the students to perform the given activity.

Control Statements

Presentation 2

6.3-27

Fundamentals of Programming

Programming Language (JAVA)

Slide 22

Lab Exercise
11. Write a program that accepts an integer and
print it backwards.
For example,
If the input is 53
Output: 35

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

Slide 23

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

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

Control Statements

Presentation 2

6.3-28

Fundamentals of Programming

Programming Language (JAVA)

Slide 24

break and continue

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.

Explain the usage of the break and continue statements using the given slide.

Slide 25

Hands-On!

Program BreakDemo.java illustrates the use


of break statement.

Demonstrate the data file BreakDemo.java to explain the usage of break


statement.

Control Statements

Presentation 2

6.3-29

Fundamentals of Programming

Programming Language (JAVA)

Slide 26

Hands-On!

Program ContinueDemo.java illustrates the


use of continue statement. The program
displays multiples of 2.

Demonstrate the data file ContinueDemo.java to explain the usage of


continue statement.
Slide 27

Summary
In this presentation, 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.

Summarise the points given in the slide.

Control Statements

Presentation 2

6.3-30

Fundamentals of Programming

Programming Language (JAVA)

Slide 28

Assignment
1. Write short notes on control statements.
2. Write a program to accept three numbers
and find the smallest number.
3. What is the difference between while and
do-while loops?

Answers
1. Program to find the smallest of three numbers.
class smallest
{
public static void main (String args[])
{
int a;
int b;
int c;
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
c = Integer.parseInt(args[2]);
if(a<b)
{
if(a<c)

System.out.println(a + " is the smallest");


else
System.out.println(c + " is the smallest");
}
else
{
if (b<c)

Control Statements

Presentation 2

6.3-31

Fundamentals of Programming

Programming Language (JAVA)

System.out.println(b + " is the smallest");


else
System.out.println(c +" is the smallest");
}
}
}
2. The looping statements available in Java are for, while and do-while.

Control Statements

Presentation 2

6.3-32

You might also like