Core java interview Questions You'll Most Likely Be Asked (Second Edition)


What are the looping constructs in Java?


Download 1.52 Mb.
Pdf ko'rish
bet22/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   18   19   20   21   22   23   24   25   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

What are the looping constructs in Java?
Answer:
Looping constructs are also known as iteration statements. They
can be used to repeatedly execute a block of code. Java has the
following looping constructs:
while: The while statement checks a condition and repeats a
block of code as long as the condition is true. Its syntax is as
follows:
while (condition){
//body of the loop
}
do while: A do while loop also repeats a block of code as
long as a condition is true but it checks the condition at the


c.
d.
052.
end of the loop, so the body of the loop is executed at least
once. Its syntax is as follows:
do{
//body here
} while(condition);
for: A for loop can be used to iterates over a range of values.
It continues the iteration until a condition is true, after which
the loop is exited. Its syntax is as follows:
for(initialization;condition;iteration){
//body of the loop
}
for each: The for each loop is used to cycle through an array
or a collection of objects. In each iteration of the loop, it
fetches the next element from the array or collection and
executes the body of the loop for that element
for(datatype var: collection) {
//body of the loop
}
Explain the syntax of the if–else statement with an
example.
Answer:
The following is an example of an if–else statement:
int iValue = 10;
if(iValue > 1000) {
System.out.println(“iValue is > 1000”);


053.
}
else {
System.out.println(“iValue is < 1000”);
}
The if statement needs to be followed by a condition. If the
condition evaluates to true, the code following the if statement is
executed. If the condition evaluates to false, the code following
the else keyword is executed. Here, the variable iValue has the
value 10, so the condition within the if statement evaluates to
false and so the code within the else block gets executed. So,
this code prints the following output:
System.out.println(“iValue is < 1000”);

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   18   19   20   21   22   23   24   25   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling