Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 41

Decision Making and Looping

McGraw-Hill | 1
Introduction

• Process of repeatedly executing a block of statements is known


as looping.
• The statements in the block may be executed any number of
times.
• If a loop continues for ever it is called an infinite loop
• A program loop consists of two segments, one known as the
body of the loop and the other is the control statement.
• Control statement tests certain conditions and then directs the
repeated execution of the statements contained in the body of
the loop.
7.1 Introduction

A controlled structure may be classified as the entry-controlled loop or as


exit-controlled loop.
In case of entry-controlled loop, the control conditions are tested before the
start of the loop execution.
• If the conditions are not satisfied , then the body of the loop will not be
executed.
• In case of an exit-controlled loop, the test is performed at the end of the
body of the loop and therefore the body is executed unconditionally for
the first time.
7.1 Introduction

Three Constructs for performing loop operations are:


• While construct
• do construct
• for construct
7.1 Introduction
7.2 while Statement

Simplest of all the looping structures in Java.


Basic format of the while statement:
Initialization:
while(test condition)
{
Body of the loop
}
7.2 while Statement

• It is an entry-controlled loop statement.


• The test condition is evaluated and if the condition is true, the loop is
executed.
• After execution of the body, the test condition is once again
evaluated and if it is true, the body is executed once again.
• The process is repeated till the test condition becomes false.
• Once the loop condition becomes false, the control is transferred out
of the loop.
• The body of the loop may have one or more statements.
• The braces are required only if the body contains two or more
statements.
7.2 while Statement
9
7.3 do Statement

On some occasions, it might be necessary to execute the


body of the loop before the test is performed.
Such statement can be handled with the help of do
statement.
Initialization;
{
Body of the loop
}
while(test condition);
The loop will continue as long as one of the two relations is
true.
7.3 do Statement
7.3 do Statement
7.4 for Statement

Entry-controlled loop that provides a more concise loop


control structure.
General form of loop:
Syntax:
for(initialization;test condition;increment)
{
Body of the loop
}
Execution of for statement is as follows:
• Initialization of the control variable is done first using
assignment statements(i=1 or count=0).
• The value of the control variable is tested using the test
condition that's relational(i<10).
• The control variable determines when the loop will exit.
• After the loop is executed, the control is transferred back
to the for statement.
• The control variable is incremented.
• The new value of the control variable is tested again to
see if it still satisfies the loop condition.
• If yes, the body of the loop will be executed again.
7.4 for Statement

Example:
for loop, to be executed 10 times to print the digits 0 to 9
for(x = 0;x<9; x = x+1)
{
System.out.println(x);
}
7.4 for Statement

The for statement allows for negative increments


for(x = 9; x >= 0; x = x-1)
System.out.println(x);

This loop is also executed 10 times but instead of 0 to 9, 9


to 0 is printed.
17
7.4 for Statement

Comparison of three loops:


7.4 for Statement

Additional Features of for Loop


More than one variable can be initialized at a time in the for
statement.
Example
The statements
p = 1;
for(n = 0; n<17; ++n)
can be rewritten as
for(p = 1, n = 0; n<17; ++n)
7.4 for Statement

One or more sections can be omitted from for statement if necessary


.............
.............
m = 5;
for(;m != 100;)
{
System.out.println(m);
m = m+5;
}
.............
.............
7.4 for Statement
Nesting for Loops

•One for statement within another for statement.

•Loop should be properly indented.


7.5 Jumps in LOOPS

Loops perform set of operations repeatedly until the control


variable fails to satisfy the test condition.
Sometimes, when executing a loop it becomes desirable to
skip a part of the loop or to leave the loop as soon as a
certain condition occurs.
Java permits:
• jump from one statement to the end or beginning of the
loop
• Jump out of the loop
7.5 Jumps in LOOPS

Jumping Out of a Loop


An early exit from a loop can be accomplished by using a
break statement.
When a break statement is encountered inside a loop, the
loop is immediately exited.
After the exit the program continues with the statement
immediately following the loop.
When the loops are nested, the break would only exit from
the loop containing it
In nested loops the break will exit only a single loop.
7.5 Jumps in LOOPS
25
7.5 Jumps in LOOPS

Skipping a part of the loop


continue statement causes the loop to be continued with
next iteration after skipping any statements in between.
27
28
Java Program to Find if a Given Year is a Leap Year
• Every leap year corresponds to these facts :
• A century year is a year ending with 00. A century year is a leap year
only if it is divisible by 400.
• A leap year (except a century year) can be identified if it is exactly
divisible by 4.
• A century year should be divisible by 4 and 100 both.
• A non-century year should be divisible only by 4.

29
30
What is the Fibonacci Sequence

The Fibonacci sequence is a series of numbers that appears in


surprisingly many aspects of nature, from the branching of trees to
the spiral shapes of shells. This series is named after the Italian
mathematician Leonardo Fibonacci.
Moreover, the Fibonacci sequence has practical applications in
various fields, such as stock market analysis and population growth
modeling. Its appearance in so many diverse areas speaks to the
universality and importance of mathematics in understanding the
world around us.

31
32
33
34
35
36
37
7.6 return Statement

• The return is the keyword used a s a control statement under jump


section.
• It is used inside a function definition.
• The function returns to the caller when it reaches the return
statement or all statements are executed.
• The signature of the function indicates the return type.
7.6 return Statement

There are two types of return statements:


• return with some value
• return value;
• The data type of this value should match with the data type of function's
declared return value.

return without a value:


• return;
• return without value is used in functions that are declared void. The
statement below return will never be executed.
7.7 Labelled Loops

Labelled continue
7.7 Labelled Loops

Labelled break

You might also like