codescracker


c

C Loops



« Previous Tutorial Next Tutorial »


In C, loops allows a set of instructions to be repeatedly executed until a certain condition is reached. However, the condition, may be predetermined (as in for loop) or open ended (as in while and do-while loops). C allows the following three types of loops:

Now let's discuss about these loops in detail with examples.

C for Loop

Here is the general form of the for loop.

for(initialization; condition; increment)
{
	statements;
}

Here initialization occurs first but only once, then checks the condition, if the conditions is true, then the loop control statements will executed, otherwise not. And then, the loop variable will be incremented or decremented, then checks the condition again, if the condition is true, then the loop control statements is again executed, otherwise not. This action is continued until the condition becomes false. Now concentrate on the following example program.

The for Loop Example

Here are some example programs given that illustrates the C for loop.

/* C Loops - The for Loop example
 * This program illustrates the concepts
 * of C for loop
 */

#include<stdio.h>
#include<conio.h>
void main()
{
	int num = 2;
	int i;
	clrscr();

	for(i=1; i<=10; i++)
	{
		printf("%d * %d = %d", num, i, num*i);
		printf("\n");
	}

	getch();
}

Here is the sample output of this C program which prints the table of 2:

c loops

Here is one more example

/* C Loops - The for Loop Example Program
 * This program illustrates multiple loop
 * control variables
 */

#include<stdio.h>
#include<conio.h>
#include<string.h>

void conv(char *targ, char *src);

void main()
{
	char target[80] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
	clrscr();

	conv(target, "You are learning C Loop Control.");
	printf("\nFinal string = %s\n", target);

	getch();
}
/* this function copies one string into another string.
 * It copies characters to both the ends,
 * converging at the middle
 */
void conv(char *targ, char *src)
{
	int i, j;
	printf("%s\n", targ);
	for(i=0, j=strlen(src)-1; i<=j; i++, j--)
	{
		targ[i] = src[i];
		targ[j] = src[j];
		printf("%s\n", targ);
	}
}

Here is the sample run of this C program:

c loop

Here is another example program

/* C Loops - The for loop example program
 * To quit this program, just enter 0
 */

#include<stdio.h>
#include<conio.h>

int sqrnum(int);
int readnum(void);
void prompt(void);

void main()
{
	int t;
	clrscr();

	for(prompt(); t=readnum(); prompt())
	{
		sqrnum(t);
	}

	getch();
}
void prompt(void)
{
	printf("Enter a number : ");  // to quit, enter 0
	return 0;
}
int readnum(void)
{
	int num;
	scanf("%d", &num);
	return num;
}
int sqrnum(int num)
{
	printf("%d\n", num*num);
	return num*num;
}

Here is the sample output of this C program, press 0 to quit the program.

c loop control

C Infinite Loop

In C, you can use any loop statement to create an infinite loop. Here is an example program of infinite loop.

/* C Loops - The infinite loop */

#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();

	for(;;)
	{
		printf("*  ");
	}

	getch();
}

As you can see, when the condition expression is absent, then it is assumed to be true. Here is the sample run of this C program. The printing of stars(*) continue forever.

loops in c

Now let's discuss about C while loop.

C while Loop

Here is the general form of the while loop.

while(condition)
{
	statements;
}

As you can see, when the condition is true, then the while loop's statements will be executed, otherwise not. Now go through the following example that illustrates the while loop.

The while Loop Example

Here is a while loop example program.

/* C Loops - The while loop example program */

#include<stdio.h>
#include<conio.h>
void main()
{
	int num = 2;
	int i=1;
	clrscr();

	while(i<=10)
	{
		printf("%d * %d = %d\n", num, i, num*i);
		i++;
	}

	getch();
}

Here this program also prints the table of 2 but using the while loop, below is the sample output of this C program.

c loops example

Here is one more example program that illustrates the concept of while loop in C programming.

/* C Loops - This program illustrates the concept
 * of while loop in C programming
 */

#include<stdio.h>
#include<string.h>
#include<conio.h>

void pad(char *str, int len);

void main()
{
	char str[80];
	clrscr();

	strcpy(str, "this is a test");
	pad(str, 50);
	printf("%d", strlen(str));

	getch();
}
/* adding spaces to the end of the string
 * to fill the string to a predefined length
 */
void pad(char *str, int len)
{
	int l;
	l=strlen(str);
	while(l<len)
	{
		str[l] = ' ';
		l++;
	}
	str[l] = '\0';
}

Below is the sample run of this program

for loop in c

Now let's discuss about C do-while loop.

C do-while Loop

Unlike the for and while loop, which test the loop condition at the top of the loop, but the do-while loop checks its condition at the bottom of the loop. It means that the do-while loop always executes at least once, unlike the other two loops. So, do-while loop is generally used in menu-driven program like Calculator Program, which gives options to the user whether he/she wants addition, subtraction, multiplication, etc. to perform the desired action accordingly. Following example illustrates the concept of do-while loop.

The do-while Loop Example

Here is an example program based on do-while loop.

/* C Loops - This program illustrates the
 * concept of do-while loop in C
 */

#include<stdio.h>
#include<dos.h>
#include<conio.h>
void main()
{
	char choice;
	struct date d;
	getdate(&d);
	clrscr();

	do
	{
		printf("1. Good News\n");
		printf("2. Bad News\n");
		printf("3. Today's Date\n");
		printf("4. Exit\n");
		printf("Enter your choice (1-4) : ");
		scanf("%c", &choice);
		switch(choice)
		{
			case '1' :
				printf("You have chosen right language.");
				break;
			case '2' :
				printf("You have to learn a lot");
				break;
			case '3' :
				printf("Today's Date : %d/%d/%d", d.da_day, d.da_mon, d.da_year);
				break;
			case '4' :
				exit(1);
			default :
				printf("Wrong choice..!!\n");

		}
		printf("\n\n");
	}while(choice!=4 && choice!=getchar());

	getch();
}

Here is the 2 sample run of this do-while loop example program.

c while loop

c do while loop

To print date in words in C language, refer print date in words.

C Loop Control Statements

There are three types of statements that controls the loop in C, these are:

Let's discuss about the statements that controls the loop in C. Let's start with the goto statement.

C goto Statement

In C language, the goto statement requires a label (a label is a valid identifier followed by a colon) for operation. Furthermore, the label must be in the same function as the goto that uses it, you can't jump between functions. Here is the general form of the goto statement:

label:
.
.
.
goto label;

here label is any valid label either before or after goto. Let's go through the following example program.

The goto Statement Example

Here is an example program that uses the goto statement:

/* C Loops - This program illustrates the
 * concept of goto statement in C
 */

#include<stdio.h>
#include<conio.h>
void main()
{
	int i = 1;
	int num = 2;
	clrscr();

	tab:
		printf("%d * %d = %d\n", num, i, num*i);
		i++;
		if(i<=10)
			goto tab;

	getch();
}

This program prints the table of 2. Here is the sample run of the above C program.

c goto statement

Now, let's discuss about the break statement.

C break Statement

The break statement has the following two uses:

Now let's go through the following example program that illustrates the concept of break statement.

The break Statement Example

Here is an example program of that uses the break statement.

/* C Loops - This program illustrates
 * the concept of break statement in C
 */

#include<stdio.h>
#include<conio.h>
void main()
{
	int i;
	int num = 2;
	clrscr();

	for(i=1; i<50; i++)
	{
		if(i==11)
			break;
		printf("%d * %d = %d\n", num, i, num*i);
	}

	getch();
}

This program prints the table of 2. Here is the sample run of this program.

c break statement

You can also use break statement in the switch statement. To see how, refer calculator program.

C continue Statement

The continue statements work just opposite to the break statement. That is, instead of forcing termination, continue forces the next iteration of the loop to take place, skipping any code remains after the continue statement. Let's go through the following program to understand the continue statement.

The continue Statement Example

Here is the example program that uses the continue statement.

/* C Loops - This program illustrates
 * the concept of break statement in C
 */

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
	int i;
	int num = 2;
	clrscr();

	for(i=1; i<50; i++)
	{
		printf("%d * %d = %d\n", num, i, num*i);
		if(i>0 && i<10)
			continue;
		else
			printf("\npress any key to exit..\n");
			getch();
			exit(1);
	}

	getch();
}

This program also prints the table of 2. Here is the sample output of this program.

c continue statement

More Examples

Here are some more examples that you can go for:


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test