the for loop is basically a repitation or traversing loop means one's to write his name 100 times he use the for loop
example
for(i=0;i<=100;i++)
{
printf("my name is AA");
}
the for loop checks the condition until i<=100 repeates the name or check the condition upto 100 times and then terminates.
Chat with our AI personalities
A for loop is a loop of the form:
for (/*starting conditions/variable initialisation*/;/*condition*/;/*changes to variables*/) {}
The most basic example would be:
for (int i=0;i<10;i++) {
prinf("%i",i);
}
This code can be represented as the following algorithm:
The standard algorithm, for any 4 statements:
for (/*EXPRESSION 1*/;/*EXPRESSION 2*/;/*EXPRESSION 3*/) {
/*STATEMENT 4*/
}
would be:
No. An algorithm is a procedure or formula for solving a problem: a finite series of computation steps to produce a result. Some algorithms require one or more loops, but it is not true that every algorithm requires a loop.
n-1 times
n=100 loop until n = 9 print n n = n -1 end loop
The three primitive logic structures in programming are selection, loop and sequence. Any algorithm can be written using just these three structures.
The LCM can be calculated without using any loop or condition as follows: int lcm (int a, int b) { return a / gcd (a, b) * b; } The problem is that the typical implementation for the GCD function uses Euclid's algorithm, which requires a conditional loop: int gcd (int a, int b) { while (b!=0) b ^= a ^= b ^= a %= b; return a; } So the question is really how do we calculate the GCD without a conditional loop, not the LCM. The answer is that we cannot. There are certainly alternatives to Euclid's algorithm, but they all involve conditional loops. Although recursion isn't technically a loop, it still requires a conditional expression to terminate the recursion.