1Explain what is meant by the term conditionally executed 2Y
Solution
Ans)
1)Explain what is meant by the term conditionally executed.
Ans)The term conditionally executed means the set of statements should be executed based on the condition specified.
2)You need to test a condition and then execute one set of statements if the condition is true. If the condition is false, you need to execute a different set of statements. What structure will you use? (Write out a sample code)
Ans)If--else loop is best exampke for this type of situations
Ex:
if(age > 18){
System.out.println(\"eligible for voting\");
}else{
System.out.println(\"Not eligible for voting\");
}
3)Briefly describe how the AND operator works.
Ans) AND operator is used in such a cases that all the conditions are satisfied then the loop should be executed.
4). Briefly describe how the OR operator works.
Ans) OR operator is used in such a cases the if any one the statements are true then the condition should be executed.
5)When determining whether a number is inside a range, which logical operator is it best to use?
Ans)Lessthan(<) operator is used to compare number is insid ea range.
6)Why should you indent the statements in the body of a loop or IF statement?
Ans)We should indent the statements in the body of a if loop because if there is no statements there is no use of if loop. In order to execute the if loop successflly the statements should be included.
7)Describe the difference between pretest loops and posttest loops.
Ans)pretest loop condition is tested before execution of the code and in the posttest loop condition is tested after the execution of the loop.Example of pretest loop is while loop where the condition is tested first and then the execution takes place. The example of a posttest loop is do-while loop where the loop executes atleast once before the condition is tested.
8)What is a condition-controlled loop?
Ans)Condition-controlled loop is that the loop executes continuously until the condition is satisfied.It will exit after the condition is satisfied.
ex: while(a >10){
printf(\"number greater than 10\");
}
The above example is a conditional controlled loop here a is the condition.
9)What is a count-controlled loop?
Ans) A count controlled loop will perform iterations until the certain count value is reached.It will exit the loop until certain numberof count operations are performed.
Ex: For loop
10)What three actions do count-controlled loops typically perform using the counter variable?
Ans) The three actions the count controlled loop performed are
1.Initialization: initializes the value
2.Condition: checks for condition
3.increment or decrement :increment or decrement the value based on count.
11)What is an infinite loop? Write the code for an infinite loop.
Ans) An ini=finite loop is a loop that executes infinitely without terminating;
ex:
int i =10;
while(i > 0){
System.out.println(\"Example of infinite loop\");
}
12)Why is it critical that counter, or accumulator variables are properly initialized?
Ans)Counter:
Each time you find a value, you increment the counter by one. At the end, the value of the counter will be equal to the number of items found
Accumulator:
Each time you find a value, you add it to the accumulator variable. At the end, the accumulator variable will contain the sum of all the values found.
So the values should be properly initialized or else the counter value or the accumulator value may changes.So it is critical to choose.
13)What is the advantage of using a sentinel?
Ans) The advantage of the sentinal loop is that there is not limit to how many iterations to be done and it exits automatically when the condition is satisfied.
14)Why must the value chosen for use as a sentinel be carefully selected?
Ans)The value choosen for a sentinal should be carefully selected because if the value is not given properly the loop will be execute dinfinetely. so the value should be choosen carefully.


