Answer:
The correct answer to this question is given below in the explanation section.
Explanation:
There are three types of loops that are used almost in all programming languages today. Loop is used when you want to execute many times the same lines of codes. These loops are given below:
For-loop
While-loop
Do-while loop
The syntax of these loops is given below. However, it is noted that these syntax are based on C++ language. :
1-For-loop:
<em>for ( init; condition; increment/decrement ) {
</em>
<em> statement(s);
</em>
<em>}</em>
init: this is executed first and only once, this allows to initialize and declare the loop control variable.
Condition: next condition is evaluated, if the condition is true then the loop body will get executed. And, if it gets false, the loop will get terminated.
increment: this will increment/decrement the counter (init) in the loop.
for example: to count number 1 to 10, the for-loop is given below:
<em>int total=0;</em>
<em>for (int i=0; i>10;i++)</em>
<em>{</em>
<em>total=total + i;</em>
<em>to</em>
<em>}</em>
2-While loop:
Repeats a statement or group of statements in the body of a while-loop while a given condition is true. It tests the given condition before executing the loop body.
syntax:
<em>while(condition) {
</em>
<em> statement(s);
</em>
<em>}</em>
For example: To count number 0 to 10.
<em>int a = 0; </em>
<em>int total =0;</em>
<em> // while loop execution
</em>
<em> while( a < 11 ) {
</em>
<em> total = total + a</em>
<em> a++;
</em>
<em> }</em>
<em />
3- do-while loop:
Do-while works like a while statement, while it tests the condition at the end of the loop body.
Syntax:
<em>do {
</em>
<em> statement(s);
</em>
<em>} </em>
<em>while( condition );</em>
<em />
For example:
<em>int a = 0; </em>
<em>int total =0;</em>
<em> // while loop execution
</em>
<em> do {
</em>
<em> total = total + a</em>
<em> a++;
</em>
<em> }while( a < 11 )</em>
<em />