answersLogoWhite

0


Best Answer

importance of loops in any programming language is immense, they allow us to reduce the number of lines in a code, making our code more readable and efficient, many problems exist which cannot be solved without the looping construct. Think if you had 10000 student records, and you wanted to find the students with the highest marks, it would be difficult for you to go and check each record individually, therefore loops are used to make our task easier and efficient.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

8y ago

It is a statement that will keep repeating whatever is in the loop. So one possible loop you could have in your code would be a loop to keep asking for an input until the correct input is given. Such as prompting the user to enter the person's age, and if the person enters a letter, it will ask for it again, and again until they enter a acceptable input.

Example:

for

while

do-while

for (initialization; test condition; increment)

{

body of the loop

}

while (test condition)

{

body of the loop

}

Do

{

statement;

}

while(expression);

for(counter=0;counter<10;counter++)

printf("%d",a[counter]);

while(counter<10)

{

printf("%d",a[counter]);

counter+=1;

}

do

{

printf("%d",counter);

}while(counter>0);

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Loops allow your programs to execute the same sequence of commands over and over. All loops must have an exit condition, otherwise they would never end. This includes so-called infinite loops, which simply means that the number of iterations cannot be predetermined. Loops can be formed in four ways: for, while, do-while and goto.

The goto loop is a procedural loop, where code jumps back to a label that was declared before the goto was encountered. The repeated section of code must also have a conditional expression that will jump out of the loop at the appropriate time. Although frowned upon in a structured programming language, goto loops are nonetheless useful for simple loops.

The for loop is the most common type of loop structure and is typically used when the number of iterations can be predetermined, whether statically or dynamically. The for statement has three parts: the initialisation, the exit condition, and the operation, all of which are optional. The initialisation section is typically used to initialise the loop control variables, such as a counter variable. The exit condition is evaluated before each iteration of the loop (including the first iteration). If the evaluation is false, execution passes to the code immediately following the loop body. If the loop is a counting loop, then the exit condition will use the counter as part of its expression. However, any valid expression can be used as an exit condition. Finally, the operation is executed at the end of each iteration, immediately before evaluating the exit condition. In counting loops, the operation will typically increment the counter variable.

The while loop is a simpler version of a for loop, where the exit condition is the only requirement. The body of the loop will perform any necessary operations upon any control variables used by the exit condition, while any required initialisation is handled before the loop is encountered.

The do-while loop is a special type of loop that always executes at least once. It is similar to a while loop, but the required exit condition is evaluated at the end of each loop.

All loops other than the goto loop can also make use of continue and break statements. A break statement can be used to prematurely terminate the loop, whether the exit condition is met or not. Break statements are typically used in conjunction with an if or a switch statement, such as when there is more than one exit condition, or to provide the exit condition for an infinite loop. The continue statement begins a new iteration and, like break, is typically used in conjunction with an if or switch statement. In for loops, the continue statement executes the operations section before evaluating the exit condition, just as it would had the end of the loop body been encountered (where a continue statement is implied). Similarly, in while and do-while loops, the exit condition is evaluated.

Infinite loops can be formed as follows:

for( ;; ) {}

while(true) {}

do {} while(true);

again:

{}

goto again;

Any code within the braces (the loop body) is the code that will be iterated, but there must be an exit condition specified within the body otherwise the loop would be perpetual (never ending), rather than infinite. In goto loops, the curly braces are not required. Curly braces are also not required if the loop body contains a single statement (or a comma-separated statement).

Counting loops are formed as follows:

for(int x=0; x<10; ++x ) {

std::cout<<x<<std::endl;

}

int x=0;

while( x<10 ) {

std::cout<<x++<<std::endl;

}

int x=0;

do {

std::cout<<x++<<std::endl;

} while( x<10 );

int x=0;

again:

std::cout<<x++<<std::endl;

if( x<10 )

goto again;

Note that all four loops will print the values from 0 to 9. Although you will typically use a for loop for this type of loop, the while and do-while loops are useful when the increments are complex or more variable. Ultimately, the type of loop you use is determined by which loop makes your code more readable or is more applicable to the type of algorithm you are encoding. In terms of efficiency, there should be no discernible difference in these examples, but more complex loops may require closer inspection of the assembly instructions to be sure.

The for loop is also useful for scoping control and counter variables to the loop itself (they can be declared in the for statement itself, and will fall from scope when the loop ends). With while and do-while loops, control variables must always be declared outside of the loop and therefore remain in scope when the loop ends.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

looping means doing the same operations over and over again

#include <stdio.h>

#include <stdlib.h>

int main(){

int i;

for(i=0; i <10;i++)

{

printf("ive looped %d times\n", i);

}

exit(0);

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

loops repeat the set of statements until the condition is true... which makes program less complex ... saves the time and instruction space...

This answer is:
User Avatar
User Avatar

Sushma Marabi

Lvl 1
2y ago
Kyse

User Avatar

Wiki User

14y ago

loop is computer language statement which allow us to avoid repetition of some specific task. some example of loop is (1) for, (2) do,(3) while etc

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is importance of loop in c language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Syntax of for loop in c language?

for(i=0;i&lt;=0;i++)


Easy to use loop in c language?

Yes.


What is difference between select Case statement and nested loop in c programming in UNIX?

UNIX has no bearing on the C language; it is cross-platform. There is no select/case in C, you probably meant switch/case. However, a switch/case is a conditional jump while a nested loop is a loop within a loop. Besides the C language they have nothing in common with each other.


Does C language has a interpreter?

There might be C language interpreters, but they have no or minimal importance in actual programming.


Why you use FOR LOOP in C language?

For LOOP is used, when you want to execute a specific block of code for specific number of times.


What is importance of c language?

The importance of C language is that it is the building block for many of the worlds computer applications and problems. It is a programing language which creates programs and allows them to be nearly infinitely modified.


What is iterations in c language?

An iteration is an instance of a structured loop statement (for, while or do-while).


For loop in c language?

for(assigning initial value;condition;increment/decrement) { statement; }


Write 8085 assembly language program for BCD up counter and display using 8279?

loop: mvi c,59 dcr c mov a,c daa movc,a jnz loop end


How to draw Flowchart to print prime numbers from 1 to 100 using while loop in c language?

c the book mastering c


Which loop is also called an exit-condition loop in C programming?

The do while loop is also called an exit condition loop in c, c++, and java.


How do you use a C-continue statement in a block- without any looping or conditional statements?

In the C language, the continue statement can only be used within a loop (for, while, or do). Upon execution, control transfers to the beginning of the loop.