Write about Decission making & looping in Java With Example?
https://www.computersprofessor.com/2017/01/write-about-decission-making-looping-in.html
Decision making & looping:
The process of repeatedly executing a
block of statement is known as looping.
A looping process is general would
include the following for steps.
® setting
and initialization of a counter.
®
execution of the statements in the loop.
® test
for a specified condition for execution of the loop.
®
increment the counter.
The java language provides three
constructs for performing loop operation.
They are:
1. while construct
2. do construct
3. for construct
While construct:
The simplest of all the looping
structures in java in the while statement. The basic format of the while
statement is
Syntax:
Initialization;
while(test condition)
{
Body of the loop;
Increment/decrement;
}
Statement-x;
The while loop is an entry control
loop statement. The test condition is evaluated and if the condition is true
then the body of the loop is executed. After execution of the body of the text
condition is once again evaluated and if it is true the body is executed once
again. This process of repeated execution of the body continuous until the test
condition finally becomes false, and the control is transferred out of the
loop. On exist the program continuous with the statement immediately after the
body of the loop.
Eg sum of squares of 10 numbers
class Sample
{
public static void main(String a[ ])
{
int i=1, sum=0;
while (i < =10)
{
sum=sum+i*I;
i++;
}
System.out. println(“sum of squares
of 10 no”+sum);
}
}
Do construct:
On some occasions it might be
necessary to execute the body of the loop before the test is performed. Such
situations can be handled with the help of the do statement. This takes the
form.
Syntax:
initialization;
do
{
Body of the loop;
Increment/decrement;
}
while(test condition);
On reaching the do statement the
program proceeds, to evaluate the body of the loop first, at the end of the
loop the test condition on the while statement is evaluated. If the condition
is true the program continues to evaluate the body of the loop once again. This
process continuous on long as the condition is true. When the condition becomes
false the loop will be terminated and the control goes to the statement that
appears immediately after the whole statement.
Since the test condition is evaluated
at the bottom of the loop the do while construct provides on end control loop
and therefore the body of the loop is always executed at least once.
Eg:
class Odd
{
public static void main(String a[ ])
{
int i=1, sum=0;
do
{
i=i+2;
System. out. println(“odd numbers
upto 50 is”+i);
sum=sum+i;
}
while(i <=50);
System. out. println(“ sum of 50
is”+sum);
}
}
The for statement:
The for loop is another entry controlled loop,
that provides a more concise loop control structure. The general form of the
for loop is:
for(initialization; condition ; increment/decrement)
{
-------
Body of the loop;
----------
}
The execution of the for statement is
as follows:
1. Initialization of the control
variable is done first, using assignment statement.
2. The value of the control variable
is tested using the test condition. The best condition is a relational
expression that determines when the loop will exit. If the condition is true
the body of the loop is executed. Otherwise the loop is terminated and the
execution continuous with the statement that immediately follows the loop.
3.
When the body of the loop is executed the control variable is
incremented. The new value of control variable is again tasted to see whether
it satisfies the loop conditions. If the condition is satisfied, the body of
the loop is again executed. This process continuous till the value of the
control variable fails to satisfy the test condition.
The time sections enclosed with in
parenthesis must be separated by semi colon(;).
class Sample
{
Public static void main(String args[
])
{
int sum=0;
for(int i=1; i < = 10; i++)
{
sum=sum+i *i;
}
System. out. println(“sum of squares
of 10 no. s”+sum);
}
}
The enhanced for loop:
The enhanced for loop also called for
each loop. This feature helps us to retrieve the array of elements efficiently
rather than using array indexes. We can also this feature to eliminate the
iterations in a for loop, and to retrieve the elements from a collection the
enhanced for loop takes the following form.
Syntax:
for(type identifier : expression)
{
Statement;
}
Where type represents the datatype
(or) object used.
Identifier refers to the name of a
variable and expression in an array (or) a collection
Eg:
import java. util.*;
class Sample
{
public static void main(String a[ ] )
{
String str[ ]={“RJY”, “TNK”,
NDD”,”ND”};
for(string i: str)
{
System.out.println(“names”+i);
}
}
}