0% found this document useful (0 votes)
2 views12 pages

Java Looping Control Structures Guide

This document provides an overview of control structures in programming, specifically focusing on looping mechanisms such as while, do-while, and for loops. It explains the differences between entry and exit controlled loops, the importance of initialization, testing, and changing variables within loops, and includes examples and practice exercises. Additionally, it discusses nested loops and the use of break and continue statements to control loop execution.

Uploaded by

msameenkalmunai
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

Java Looping Control Structures Guide

This document provides an overview of control structures in programming, specifically focusing on looping mechanisms such as while, do-while, and for loops. It explains the differences between entry and exit controlled loops, the importance of initialization, testing, and changing variables within loops, and includes examples and practice exercises. Additionally, it discusses nested loops and the use of break and continue statements to control loop execution.

Uploaded by

msameenkalmunai
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MIT 11053: Fundamentals of Programming More Notes for Chapter 04

By: S. Sabraz Nawaz, Senior Lecturer in MIT, Dept. of MIT, Faculty of Management and Commerce, SEUSL

CONTROL STRUCTURES: LOOPING


Looping in programming languages is a feature which facilitates the execution of a set of instructions/functions
repeatedly while some condition evaluates to true.
Java provides three ways for executing the loops. While all the ways provide similar basic functionality, they
differ in their syntax and condition checking time.
1. The while loop
2. The do while loop
3. The for loop

Based on how the condition is checked, these loops can be categorized into two;
1. Entry Controlled Loop: if condition is not satisfied body of the loop is never executed.

2. Exit Controlled Loop: if the body of the loop is executed unconditionally for the 1st time.

A loop consists of Body of the loop and Control statement

Important Parts in a Loop


• Initialization: The variables tested in the condition must be initialized to some values. If the condition
is false at the outset, the loop is never entered.
• Testing: The condition is tested before each iteration. If false, the program continues with the first
statement after the loop.
• Change: At least one of the variables tested in the condition must change within the body of the loop.

MIT11053 – Fundamentals of Programming © S. Sabraz Nawaz


1
MIT 11053: Fundamentals of Programming More Notes for Chapter 04
By: S. Sabraz Nawaz, Senior Lecturer in MIT, Dept. of MIT, Faculty of Management and Commerce, SEUSL

The while Loop:


A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean
condition. The while loop can be thought of as a repeating if statement. The while loop is a pretest loop
(entry controlled), which means that it will test the value of the condition prior to executing the loop. While
the condition is true, the statements will execute repeatedly.

While loop starts with the checking of condition. If it evaluated to true, then the loop body statements are
executed otherwise first statement following the loop is executed. For this reason it is also called Entry
control loop. Once the condition is evaluated to true, the statements in the loop body are executed.
Normally the statements contain an update value for the variable being processed for the next iteration.
When the condition becomes false, the loop terminates which marks the end of its life cycle.
Care must be taken to set the condition to false somewhere in the loop so the loop will end; otherwise it
will become as an infinite loop. A while loop executes 0 or more times. If the condition is false, the loop will
not execute.
Example: The following program displays numbers from 1 to 10 on the screen

MIT11053 – Fundamentals of Programming © S. Sabraz Nawaz


2
MIT 11053: Fundamentals of Programming More Notes for Chapter 04
By: S. Sabraz Nawaz, Senior Lecturer in MIT, Dept. of MIT, Faculty of Management and Commerce, SEUSL

Practice 01
Write a program that uses a while loop to print the sum of integers from 1 to 10.

Practice 02
The Class Exercise is modified using shorthand assignment operator

MIT11053 – Fundamentals of Programming © S. Sabraz Nawaz


3
MIT 11053: Fundamentals of Programming More Notes for Chapter 04
By: S. Sabraz Nawaz, Senior Lecturer in MIT, Dept. of MIT, Faculty of Management and Commerce, SEUSL

The do-while Loop:


The do while loop is similar to while loop with only difference that it checks for condition after executing
the statements, and therefore is an example of Exit Control Loop.
The do while loop starts with the execution of the statement(s). There is no checking of any condition for
the first time. After the execution of the statements, and update of the variable value, the condition is checked
for true or false value. If it is evaluated to true, next iteration of loop starts. When the condition becomes
false, the loop terminates which marks the end of its life cycle. It is important to note that the do-while loop
will execute its statements at least once before any condition is checked, and therefore is an example of exit
control loop.

Practice 3
Write a program that uses a while loop to print the sum of integers from 1 to 10.

Practice 4

• Write a program that uses do-while loop to print the sum of squares of integers from 1 to 10.

MIT11053 – Fundamentals of Programming © S. Sabraz Nawaz


4
MIT 11053: Fundamentals of Programming More Notes for Chapter 04
By: S. Sabraz Nawaz, Senior Lecturer in MIT, Dept. of MIT, Faculty of Management and Commerce, SEUSL

Activity 5
• In this example you can see that although the expression evaluates to false, the code inside the loop
block is executed once; this is because do-while is exit controlled loop

The for Loop:


The for loop provides a concise way of writing the loop structure. Unlike a while loop, a for statement
consumes the initialization, condition and increment/decrement in one line thereby providing a shorter, easy
to debug structure of looping.
• The for loop is a pre-test loop
• The for loop allows the programmer to initialize a control variable, test a condition, and modify the
control variable all in one line of code

MIT11053 – Fundamentals of Programming © S. Sabraz Nawaz


5
MIT 11053: Fundamentals of Programming More Notes for Chapter 04
By: S. Sabraz Nawaz, Senior Lecturer in MIT, Dept. of MIT, Faculty of Management and Commerce, SEUSL

• Initialization condition: Here, we initialize the variable in use. It marks the start of a for loop.
An already declared variable can be used or a variable can be declared, local to loop only.
• Testing Condition: It is used for testing the exit condition for a loop. It must return a boolean
value. It is also an Entry Control Loop as the condition is checked prior to the execution of the
loop statements.
• Statement execution: Once the condition is evaluated to true, the statements in the loop body
are executed.
• Increment/ Decrement: It is used for updating the variable for next iteration.
• Loop termination: When the condition becomes false, the loop terminates marking the end of
its life cycle.

Activity 6
• The following example displays numbers from 1 to 10

Activity 7
• Write a program that uses for loop to print the sum of squares of integers from 1 to 10.

MIT11053 – Fundamentals of Programming © S. Sabraz Nawaz


6
MIT 11053: Fundamentals of Programming More Notes for Chapter 04
By: S. Sabraz Nawaz, Senior Lecturer in MIT, Dept. of MIT, Faculty of Management and Commerce, SEUSL

Activity 8
• Display the even numbers between 0 and 20

Nested Loops
• Like if statements, loops can be nested.

MIT11053 – Fundamentals of Programming © S. Sabraz Nawaz


7
MIT 11053: Fundamentals of Programming More Notes for Chapter 04
By: S. Sabraz Nawaz, Senior Lecturer in MIT, Dept. of MIT, Faculty of Management and Commerce, SEUSL

• If a loop is nested, the inner loop will execute all of its iterations for each time the outer loop executes
once.
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
loop statements;
}
}
• The loop statements in this example will execute 100 times.

Activity 9
Develop a 10 x 10 multiplication table

break and continue

MIT11053 – Fundamentals of Programming © S. Sabraz Nawaz


8
MIT 11053: Fundamentals of Programming More Notes for Chapter 04
By: S. Sabraz Nawaz, Senior Lecturer in MIT, Dept. of MIT, Faculty of Management and Commerce, SEUSL

The break Statement - jumping out of a loop


• It is possible to force an immediate exit from a loop, bypassing any remaining code in the body of the
loop and the loop’s conditional test, by using the break statement
• When a break statement is encountered inside a loop, the loop is terminated and program control
resumes at the next statement following the loop
(It is considered bad form to use the break statement in this manner)

Loop Break 02

MIT11053 – Fundamentals of Programming © S. Sabraz Nawaz


9
MIT 11053: Fundamentals of Programming More Notes for Chapter 04
By: S. Sabraz Nawaz, Senior Lecturer in MIT, Dept. of MIT, Faculty of Management and Commerce, SEUSL

• Go through the code and understand what’s going on


• The use of break statement

Loop Break 03
• Go through the code and understand what’s going on
• The use of break statement

The continue Statement - skipping a part of a loop


• The continue statement forces the next iteration of the loop to take place, skipping any code between
itself and the conditional expression that controls the loop

MIT11053 – Fundamentals of Programming © S. Sabraz Nawaz


10
MIT 11053: Fundamentals of Programming More Notes for Chapter 04
By: S. Sabraz Nawaz, Senior Lecturer in MIT, Dept. of MIT, Faculty of Management and Commerce, SEUSL

• Like the break statement, the continue statement should be avoided because it makes the code hard to read
and debug

Loop Continue
• In the above example, the continue statement causes the next iteration of the for loop
• Variable i takes the value from 0 – 15 but only even numbers are displayed on the screen

MIT11053 – Fundamentals of Programming © S. Sabraz Nawaz


11
MIT 11053: Fundamentals of Programming More Notes for Chapter 04
By: S. Sabraz Nawaz, Senior Lecturer in MIT, Dept. of MIT, Faculty of Management and Commerce, SEUSL

MIT11053 – Fundamentals of Programming © S. Sabraz Nawaz


12

You might also like