Page 181 - Cs_withBlue_J_C11_Flipbook
P. 181
temp=temp/10;
}
if((s*p) == n)
System.out.println(n+ "is a sum product number");
else
System.out.println(n+ "is not a sum product number");
Since the condition is checked before the execution of the while loop, we can call it an “Entry Controlled Loop”.
The do-while Loop
The Java do-while loop executes a set of statements repeatedly until the specified condition is true. It is used when
the number of iterations is not fixed but the loop needs to be executed at least once irrespective of the test condition.
This is because the test condition is checked after the body of the loop.
The syntax of the do-while loop is:
Initialisation;
do
{
// body of the loop
increment or decrement;
} while (test condition);
For example:
To check whether the given number is a palindrome number or not (Palindrome number is a number that remains the
same on reversing its digits, e.g. 121)
int n=121, r, temp, s=0;
temp=n;
do
{
r=temp%10;
s=s*10+r;
temp=temp/10;
}
while(temp>0);
if(n == n)
System.out.println(n+ "is a palindrome number");
else
System.out.println(n+ "is not a palindrome number");
Since the do-while loop executes at least once, we can term it an Exit Control Loop.
Different Terms Related to Loops
1. Infinite Loop: A loop that never ends is called an Infinite Loop. It is also called Endless Loop.
For example:
for(i=10; i>=1; i++)
{
System.out.println(i);
}
Here, the program will print values 10, 11, 12, …… and so on and will never end.
Similarly, we can also use the while loop and do-while loop as an infinite loop.
179
Statements and Scope 179

