Professional Documents
Culture Documents
6.3
UNIT
Control Statements
Presentation 2
6.3-16
Fundamentals of Programming
Slide 1
Presentation 2
Slide 2
Objectives
At the end of this presentation, you will be able to:
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
Slide 3
Looping Statements
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
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
Slide 5
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!
Demonstrate the data file LoopDemo.java to explain the working of the for
Loop
Control Statements
Presentation 2
6.3-19
Fundamentals of Programming
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.
Control Statements
Presentation 2
6.3-20
Fundamentals of Programming
Slide 8
Lab Exercise
7.
Slide 9
Lab Exercise
8.
Control Statements
Presentation 2
6.3-21
Fundamentals of Programming
Slide 10
Lab Exercise
9.
Series 1
1
12
123
1234
Series 2
1, 2, 3, 5, 7n
Series 3
123
456
789
Slide 11
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
Slide 12
Hands-On!
Slide 13
1 3 5 7 9 11
Control Statements
Presentation 2
6.3-23
Fundamentals of Programming
Slide 14
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
Control Statements
Presentation 2
6.3-24
Fundamentals of Programming
Slide 16
Activity 6.3.3(c)
1.
2.
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
Control Statements
Presentation 2
6.3-25
Fundamentals of Programming
Slide 18
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!
Demonsrtate the data file DoWhileDemo.java to explain the working of a dowhile loop
Control Statements
Presentation 2
6.3-26
Fundamentals of Programming
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.
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.
Control Statements
Presentation 2
6.3-27
Fundamentals of Programming
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
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
Control Statements
Presentation 2
6.3-28
Fundamentals of Programming
Slide 24
Explain the usage of the break and continue statements using the given slide.
Slide 25
Hands-On!
Control Statements
Presentation 2
6.3-29
Fundamentals of Programming
Slide 26
Hands-On!
Summary
In this presentation, you learnt the following:
Control Statements
Presentation 2
6.3-30
Fundamentals of Programming
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)
Control Statements
Presentation 2
6.3-31
Fundamentals of Programming
Control Statements
Presentation 2
6.3-32