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”);
Do'stlaringiz bilan baham: