C Programming language

adplus-dvertising
Loops in C
Previous Home Next

Odd Loop


 /* odd loop using a for loop */
main( )
{
char count = 'y' ;
int number ;
for ( ; count == 'y' ; )
{
printf ( "Enter a number " ) ;
scanf ( "%d", &number ) ;
printf ( "square of %d is %d", number, number * number ) ;
printf ( "\n Want to enter another number y/n " ) ;
scanf ( " %c", &count ) ;
}
}

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 statement
int 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 stament
printf ("\n%d",count );
a++;
if (count  < 10) goto loop ; // jump statement 
retrun 0;
}

Note: Avoid the use of goto.

Previous Home Next