Programming constructs can be basically
defined as types of statements in a
programming language.
Sequence, Selection and Iteration are the
three types of Programming constructs that
are provided in every programming language
to support the above features.
In a program, statements may be executed
sequentially, selectively or iteratively
(repeatedly).
SEQUENCE VS SELECTION VS ITERATION
SEQUENCE SELECTION ITERATION
Statements are being Determines the execution of Repeats a set of
executed sequentially statements based on a statements
condition. If a condition is true a depending upon a
Eg:
test-condition. A set
class abc set of statements are executed,
of statements are
{
otherwise another set of repeated till the
statements are executed. condition becomes
public static void main()
Eg: true or false.
{ class test
int x=300; {
System.out.println(x); public static void main()
{
}
int age=23;
} if(age>=18)
System.out.println(“Eligible to vote”);
}
}
for loop
Learning Intention: Using a looping construct to repeat
a sequence of instructions in a Java program
Success criteria for the Chapter/Unit:
Students will be able to
identify that the looping construct in Java can be
used to repeat one or more statements in a program.
identify and understand the purpose of the
components in a looping construct
analyse the sequence of operations in a looping
construct by tracing the output of a program using
the dry run technique
identify the errors in a program and correct them
write a program using a looping construct
write simple to complex Boolean expression to
terminate a loop
identify infinite loops and empty loops
Apply the ‘for loop’ construct in a Java
program to repeat a set of instructions
Trace the output of a program that has a ‘for’
looping construct using the dry run technique
Identify errors and correct the program
In order to print the numbers from 1 to 10, the
following statements can be written.
System.out.println(1)
System.out.println(2)
System.out.println(3)
System.out.println(4)
System.out.println(5)
……and so on up to 10
This technique is inefficient, if more numbers are to be
printed or added or more complex operations need to
be performed repeatedly.
A looping construct saves a programmer a lot of trouble
by repeatedly executing a set of instructions a specified
number of times.
A “for loop” is a repetition control structure
that allows you to repeatedly execute one or
more statements a specific number of times.
The syntax of a for loop is:
(1) (2) (4)
for(initialization; boolean_expression; update expression)
{
(3)
Statement;
Statement;
}
Step 1 is executed only once.
Step 2, 3 and 4 keep on repeating until the Boolean
expression becomes false.
1. The initialization step is executed first and only
once. This step allows you to declare and initialize
any loop-control variables.
2. Next, the Boolean expression is evaluated.
3. If it is true, then the body of the loop is executed. If it is
false, then the flow of control jumps to the next
statement past the for loop.
4. After the body of the for loop is executed, the flow of
control jumps back up to the update statement. This
statement allows you to update any loop control
variables.
(1) Write a program to get the following output.
************
************
************
************
************
************
(2) Write a program to display the even
numbers from 2 to 20 as shown below
2 4 6 8 10 12 ……..20
for(int x = 3; x <= 7; x = x+1)
System.out.println(x*x + “ “+ x*x*x);
What would be the output of the above
snippet?
Activity 2: Debug the code
class prog5
{
public static void main()
{
for(int x=10; x>100;x+10)
System.out.print(x);
System.out.println("******");
}//main
}
class prog5
{
public static void main()
{
for(int x=10; x<100;x=x+10)
{
System.out.print(x);
System.out.print("******");
}
}//main
}
Write a program that displays the Buzz
numbers between 1 and 100.
Note: A buzz number is a number that is
either divisible by 7 or has 7 in the unit’s
place.