5. (a) Describe the two programming loop structures which allow a set of instruc
ID: 663122 • Letter: 5
Question
5. (a) Describe the two programming loop structures which allow a set of instructions in a computer program to be repeated without writing them out again and again. (b) What limits the number of times that the instructions inside the loop are executed? (c) The number of times the loop is executed may be controlled by an index value which counts the number of loops. In the program structures you have described, is it advisable to modify that index value inside the loop? Why? (d) What is meant by a Boolean expression or logical expression n computer programming? (e) How would you use a Boolean expression to choose a group of instructions which would be executed in a computer program?Explanation / Answer
a)
IN java:
The while Loop:
A while loop is a control structure that allows you to repeat a task a certain number of times.
When executing, if the boolean_expression result is true, then the actions inside the loop will be executed. This will continue as long as the expression result is true.
Here, key point of the while loop is that the loop might not ever run. When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.
Example:
public class Test {
public static void main(String args[]) {
int x = 10;
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print(" ");
}
}
}
This would produce the following result:
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
The do...while Loop:
A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.
Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute once before the Boolean is tested.
If the Boolean expression is true, the flow of control jumps back up to do, and the statements in the loop execute again. This process repeats until the Boolean expression is false.
The for Loop:
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
A for loop is useful when you know how many times a task is to be repeated.
Enhanced for loop in Java:
As of Java 5, the enhanced for loop was introduced. This is mainly used for Arrays.
IN c:
C Programming Loops
Loops cause program to execute the certain block of code repeatedly until test condition is false. Loops are used in performing repetitive task in programming. Consider these scenarios:
These types of task can be solved in programming using loops.
There are 3 types of loops in C programming:
for Loop Syntax
How for loop works in C programming?
The initialization statement is executed only once at the beginning of the for loop. Then the test expression is checked by the program. If the test expression is false, for loop is terminated. But if test expression is true then the code/s inside body of for loop is executed and then update expression is updated. This process repeats until test expression is false.
This flowchart describes the working of for loop in C programming.
for loop example
Write a program to find the sum of first n natural numbers where n is entered by user. Note: 1,2,3... are called natural numbers.
Output
In this program, the user is asked to enter the value of n. Suppose you entered 19 then, count is initialized to 1 at first. Then, the test expression in the for loop,i.e., (count<= n) becomes true. So, the code in the body of for loop is executed which makes sum to 1. Then, the expression ++count is executed and again the test expression is checked, which becomes true. Again, the body of for loop is executed which makes sum to 3 and this process continues. When count is 20, the test condition becomes false and the for loop is terminated.
b)There is no limit for loop to be executed.until the condition is false the loops will continue their execution.
C)Number of times loop is executed is controlled by index value, because if there is no index value the program never terminates. Every time loop is executed index value is either incremented or decremented. So,it is advisiable to modify index value inside a loop.
For example we ill take a for loop example:
Inside the round brackets of the for loop, we have this:
loopVal =0; loopVal < end_value; loopVal++
The first part tells Java at what number you want to start looping. Here, we're assigning a value of zero to the loopVal variable. This will be the first number in the loop. The second part uses some conditional logic:
loopVal < end_value
This says "loopVal is less than end_value". The for loop will then keep going round and round while the value inside the loopVal variable is less than the value in the variable called end_value. As long as it's true that loopVal is less than end_value, Java will keep looping over the code between the curly brackets.
The final part between the round brackets of the for loop is this:
loopVal++
What we're doing here is telling Java how to go from the starting value in loopVal to the next number in the sequence. We want to count from 0 to 10. The next number after 0 is 1. loopVal++ is a shorthand way of saying "add 1 to the value in the variable".
Instead of saying loopVal++ we could have wrote this:
loopVal = loopVal + 1
To the right of the equals sign we have loopVal + 1. Java will then add 1 to whatever is currently stored in the loopVal variable. Once it has added 1 to the value, it will store the result inside of the variable to the left of the equals sign. This is the loopVal variable again. The result is that 1 keeps getting added to loopVal. This is called incrementing the variable. It is so common that the shorthand notation variable++ was invented:
int some_number = 0;
some_number++;
The value of some_number will be 1 when the code above is executed. It is the short way of saying this:
int some_number = 0;
some_number = some_number + 1;
To recap then, our for loop is saying this:
Loop Start value: 0
Keep Looping While: Start value is less than 11
How to advance to the end value: Keep adding 1 to the start value
Inside of the curly brackets of the for loop we have this:
System.out.println("Loop Value = " + loopVal);
Whatever is currently inside of the loopVal variable will be printed out, along with some text.
Run your programme and you should see this in the Output window:
So we've trapped the programme in a loop, and forced it to go round and round. Each time round the loop, 1 gets added to the loopVal variable. The loop keeps going round and round while the value inside of loopVal is less than the value in end_value. Whatever is inside of the loop's curly brackets is the code that will be executed over and over. And that is the whole point of the loop: to execute the curly bracket code over and over.
d)Boolean or Logical Expressions:
In computer science, a Boolean expression is an expression in a programming language that produces a Boolean value when evaluated, i.e. one of true or false. A Boolean expression may be composed of a combination of the Boolean constants true or false, Boolean-typed variables, Boolean-valued operators, and Boolean-valued functions.
The Boolean logical operators are : | , & , ^ , ! , || , && , == , != . Java supplies a primitive data type called Boolean, instances of which can take the value true or false only, and have the default value false. The major use of Boolean facilities is to implement the expressions which control if decisions and while loops.
A basic Boolean expression has this form:
expression relational-operator expression
Java evaluates a Boolean expression by first evaluating the expression on the left, then evaluating the expression on the right, and finally applying the relational operator to determine whether the entire expression evaluates to true or false.
Sample Boolean expressions:
e)
In this below program Boolean expressions is used
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.