Loops in C

Categories: C Programming language

while Loop

The simplest of all looping structure in C is the while statement. The general structure of the while statement is:

 while (test condition){body of the loop} 

Here in the above example compiler check the condition if the condition is true then the body of the loop is executed else the control switch out of this block.

do-while Loop

do while loop is similar to the while loop. In do while loop first the loop will be executed and then the condition will be checked.

 do{statement;} while( condition);  

Here the statement is executed, and then condition checked. If thecondition is true then the body is executed again a. When the test condition becomes false the loop terminates.

break

In C language allow to the control is go to on statement to an other statement within the loop or out side the loop. The break statement is the feature of c that allows us to accomplish this. A break causes the innermost enclosing loop or switch to be exited immediately.

break;

continue statement

In c the continue statement is similar as the break statement. In the continue the loop continued after jumping any statement for the next statement .The continue with the next iteration the format of the continue statement is simply.

continue;

For loop

for (initialization condition; loop condition; increment expression){}

There are three parts which is separated by semi-colons in control block of the for loop. initialization condition is executed before execution of the loop starts. This is typically used to initialize a counter for the number of loop iterations. You can initialize a counter for the loop in this part.

The execution of the loop continues until the loop condition is false. This expression is checked at the beginning of each loop iteration. The increment condition, is usually used to increment the loop counter. This is executed at the end of each loop iteration.

Example

//Here is an example of using for loop statement to print an integer five times#include <stdio.h>void main(){// using for loop statementint max = 10;int i = 0;for(i = 0; i < max;i++){printf("%d\n",i);}}

return

Return statement can return a value with it to the calling function if the function is declared non - void such as int ,float ,char etc. The functions declared declare as void is not to return any value and hence a return statement with value when encountered in such a function generate to an error by compiler. Also a function declared non-void should always return a value of the respective type.

Some compiler allow a relaxation which could lead to severe problems, they return garbage value if a return statement containing the value is not found in the function. General form of return statement is - return expression;

Example

#include <stdio.h>int function (void){int a;b = scanf ("%d", &a);return a; // returns the value of a to the calling function.}int main (){printf ("\n%d", function ());return 0;}

goto

goto statement is a jump statements .goto is used for jumping from one point to another point in your function means goto statement is work with in the function for change the control in one statement to an other statement. through goto statement its not possible that the control switch to one function to another function. Jump points or way points for goto are marked by label statements.

Label statement can be anywhere in the function above or below the goto statement. Special situation in which goto find use is nested loops or if - else.

General form of goto statement is

..goto label1;....label1 :...label2 :....goto label2;

Example

#include <stdio.h>int main ()// Print value 0 to 9{int count = 1; loop:; // label stamentprintf ("\n%d",count );a++;if (count  < 10) goto loop ; // jump statement retrun 0;}

Note: Avoid the use of goto.


Top Blogs
Introduction to Implementation of Queue using Linked List Published at:- Input and Output Functions in C Published at:- Importance of C Programming Language Published at:- How does C Programming Language Work Published at:- History of C Language : Introduction to C Programming Language Published at:- Benefits of C language over other programming languages Published at:- C Language Interview Question and Answers Published at:- Pointer declaration in C ,Address operator Published at:- C Program example: Input a number and print sum of its digits Published at:- What values are printed when we run following? Published at:- C Interview Questions And Answers Published at:- Learn C Programming language with example Published at:- C Memory Management ! Dynamic memory allocation Published at:- Structure in C: Introduction Published at:- Loops in C Published at:- Example of Function with no return type and no argument Published at:- Functions with arguments and no return values. Published at:- Functions with arguments and return values -C Examples Published at:- Functions that return multiple values -C Example Published at:- Types of Function in C ! Library Function in C ! User Defined Function In C ! Function Definition Published at:- C Functions ! What is a Function Published at:-
R4Rin Team
The content on R4Rin.com website is created by expert teams.