Decision and repetition structures are important topics to learn in Java so that
ID: 3879238 • Letter: D
Question
Decision and repetition structures are important topics to learn in Java so that programmers can improve their coding solutions better than just a sequencial execution of those Java statements.
Explain what is meant by the term "conditionally executed".
Briefly describe how the && operator and the || operator works.
Describe the different between the "while" loop and the "do-while" loop.
Can you use the loop structure to find the sum from 1 to 100? If no, please explain why. If yes, please show the code.
Do the following:
Answer the 4 points with only one paragraph each.
Explanation / Answer
Hey, please follow below,
1) "Conditionally exected" means if a particular condition is met then only a certain piece of code will be executed, In Java, we can define conditions inside if, for, while loops, also we can use conditional operator in java, which is a substitute for if else statements.
2) both && and || are logical operators in java, In case of && operator, if all the operands inside a if condition are non zero, then only the condition becomes true. In case of || operator, if any of the operand inside an if condition is non zero then the overall condition becomes true.
if( i> j && i>k && i> l) {
...some code // it will be executed only when i is greater than j and k and l.
}
if(i>j || i>k || i>l) {
... some code // it will be executed if i is greater than j, or if i is greater than k or if i is greater than l
}
3) In case of while loop the condition is first tested and then the loop runs, so if the condition fails for the very first time only, the code inside of while will not get executed even once. But in case of do-while loop first the code inside the loop is executed, and then the condition is checked, so even if the condition fails for the very first time only, the code inside the do-while loop will get executed once.
program to calculate sum of 1 to 100.
public class App{
public static void main(String[] args) {
int sum = 0;
int i = 1;
while (i <= 100) {
sum = sum + i;
i++;
}
System.out.println("Sum of numbers from 1 to 100 is : " + sum);
}
}
Thanks!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.