SOLENT UNIVERSITY
In programming, loops are essential to repeat the same code. The code might need to print a sequence of
numbers or multiplication table, so instead of writing the code for ten or more times, loops repeat that but
let’s have a look below in the specific. This Assessment will talk about three types of Loops; While loops,
For loops, Do while loops and Nested loops.
While loops
While loops is used when code needs to be repeat a not definite number of times, in a true condition.
Otherwise pass to End While in a false condition. This is checked before the While loops start and continues
till remains true but if the condition is false on the beginning the code run, the while doesn’t start at all.
Example:
int option;
(while option == true){ // this is telling the code to repeat the while loops, till option is true.
printf(“Run always when true”);
option++;} // option repeating plus 1 till condition remains true.
// if false the code pass to End While.
For loops
For loops is used when the code needs to be repeat a definite number of times.
Example:
for(int a=1;a<=10;a++){ //the datatype is inside the loops with a start value of 1( that it print first),
the int a is less than or equal to 10 and a++ it tell the loops to repeat each
time plus one (1+1=2+1=3 etc.) for how many times is true. Simply the
code will check from 1 if less than or equal to 10 till 10 that is equal to
10.When the code will check 11<=10 it will end the loop. Also it can be
a<11 that include 10 but exclude 11.
printf(“%d\t”,a);} //for a user-friendly way it used \t to give a space between numbers.
Outcome
1 2 3 4 5 6 7 8 9 10 // outcome of the code.
Do while loops.
Do while loops is used to repeats, an indefinite number of times, a block of statement, in a condition that is
true or until the condition becomes true. (Gookin, 2004)
Example:
int n, sum= 0; //Integer ‘n’ is equal to number, integer sum is the addition of ‘n’. Sum start
from 0.
do { //This is the start of Do while loop, the User will enter numbers that will be
add each other till the condition is true.
sum =n+n; //Addition between Numbers.
printf("Enter a number: ");
scanf("%d", &n);
} while (n != 0); //That the condition, till the User enter any number positive or negative
different than 0 the statement is true and run.
printf("The total is: %d", sum);//Print the total of the addition.
Nested Loops
Nested loops is used when the code required more than one loop inside another.
Example:
Please see grades below.
Grade D
In Grade D the User is required to enter a ‘n’ number, where the code will print a right-angle triangle with
only odd numbers, also the program will inform the User about the total of odd numbers found in the last
18
, SOLENT UNIVERSITY
line. It has been used For loop in a Nested loop. The code will check the number enter from the user and with
a nested loop print a sequence with starting from one and add plus two till the condition remain true. If the
User enter 4, the code will start the count and take 1 is less than 4, yes and store 1.Then 1 plus 2 position is 3,
3 is less than 4 and storage that too but when reach 5 that is no less than 4 it will print, since the code use a
nest loop it will print in this way 1 (true), 1 and 3 (true), also for print like a right-angle triangle with add an
escape sequence \n before to print the last statement. On the middle the loops there is a count equal to zero.
Since the program needs to count the last line, count will always start from zero and it will not add the previ -
ous count of loops.
#include <stdio.h>
void gradeD() {
int i, j, n, count;
printf("Please enter a number: ");
scanf("%d", &n);
for (i = 1; i <= n; i = i + 2) {
count = 0;
for (j = 1; j <= i; j = j + 2) {
count = count + 1;
printf("%d ", j);
}
printf("\n");
}
printf("\nOdd number found: %d ", count);}
In figure 25 the User enter an even number, 12, then the code will start to compare each odd number till
reach 13 and print all the true condition in a nest loop. The code found, also, the correct odd number for just
the last line.
Fig25-Task3-GradeD
In figure 26 the User enter an odd number, 21, the code compare each odd number till reach 23 that is bigger
than 21(false) and print all the true condition in a nest loop. The code found, also, the correct odd number for
just the last line.
Fig26-Task3-GradeD
19