loops

Loop Control Statements

http://install4install.com

Loop Control Statements alter  execution of a program from the normal execution sequence.The statements are as follows

  • break
  • continue
  • goto

break statement

break statement terminates the execution of loop immediately ineffective of the condition status.The control is transferred to the first statement after the loop block.If break statement is used in nested loops      (Refer : Nesting of Loops) then innermost loop is terminated and control is transferred to outer loop.

Syntax

break;

Program 1

#include <stdio.h>

int main() 
{
	int i;
	for(i=1;i<20;i=i+2)
	{
		if(i>15)
		{
		break;
		}
	printf("%d\n",i);
	}
	return 0;
}

Run

 

Program 2

#include <stdio.h>

int main() 
{
int i;
while(i<=15)
{
if(i%2 == 0)
{
	printf("%d\n",i);
}
i++;
if(i>10)
{
	break;
}

}
	return 0;
}

Run

continue statement

continue statement forces to execute( instead of termination)the next iteration and skips some statements.

Syntax

continue;

Program 3

#include <stdio.h>

int main()
{
	int i;

	for(i=0;i<10;i++)
	{
		if(i >5)
		{
		continue;
		i=i+5;
		printf("%d\n",i);
		}
		printf("%d\n",i);
		}
	return 0;
}

Run

goto statement

goto statement jumps the control to a desired labelled statement of a program.The use of goto is generally avoided as it leads to difficult understanding of the control flow.

Syntax

goto label;

…..

….

label:statement;

 

Program 4

#include <stdio.h>
 
int main ()
{
   int i,j;
   int num;
   //printf("Enter the no");
   scanf("%d",&num);
   for(i=0;i<10;i++)
   {
   	if(num>5)
   	{
   		num = num +2;
   		printf("%d\n",num);
   	}
   	else
   	{
   		goto LOOP;
   	}
   }
   LOOP:for(i=0;i<10;i++)//LOOP is a label
   {
   	num=num+3;
   	printf("%d\n",num);
   }
   return 0;
}

Run


Short URL: http://tinyit.cc/d42290
By Cusp2207 on July 10, 2014 | Computer Science | 2 comments
Tags:

Nesting of loops

Loops can be nested if required.Nesting means to have one or more loops inside the main loop.

Syntax of nested for loops

for(initialization;condition;iteration)

{

for(……)

{

for(…)

{…

}

…….

}

}

Syntax of nested while loops

while(condition)

{

…..

while(condition)

{

……..

}

….

}

Syntax of nested do-while loop

do

{

statements;

do

{

statements;

}

while(condition);

}

while(condition);

Example 1

Say we want to print a pattern like

#
# #
# # #
# # # #
# # # # #

Program 1

 

#include <stdio.h>

int main() 
{
int i,j;
for(i=0;i<5;i++)
{
	for(j=0;j<=i;j++)
	{
		printf("*");
	}
	printf("\n");
}
	return 0;
}

Run

Illustration 

The working of Program 1 is explained in Fig 1.

 

Screen Shot 2014-07-08 at 6.06.39 PM

Fig 1 : Working of Program 1

 

Example 2

Say we want to print table of numbers from 5 t0 10.If we need to print a table of single number then single loop can be used.In order to print table of different numbers in a single go,we need nested loop.

 

Program 2

#include <stdio.h>

int main()
{
	
	int i,j;
	int res;
	
	for(i=5;i<=10;i++)
	{
		printf("\n");
		for(j=1;j<=10;j++)
		{
			res = i*j;
			printf("%d * %d = %d",i,j,res);
			printf("\n");
		
		}
		
	}
	return 0;
}

Run


Short URL: http://tinyit.cc/00849b
By Cusp2207 on July 9, 2014 | Computer Science | A comment?
Tags:

do-while loop

The functionality of do-while is slightly different from while and for loop.The condition of do-while is at the bottom of loop and it executes at least once even if the condition is false.The initialization of a variable loop is done before the loop block.

Syntax

do

{

set_of_statements;

}

while(condition);

 

Example 1

Say we need to print  numbers from 0 to 100.

Program 1

#include <stdio.h>

int main()
{
int i = 0;
do
{
	printf("%d\t",i);
	i++;
}
while(i<=100);
	return 0;
}

 

Run

 

Example 2

Say we want to print a table of a user entered number.

Program 2

#include <stdio.h>

int main() 
{
	int num,i=1;
	int tab;
	//printf("Enter the number");
	scanf("%d",&num);
	do
	{
		
		tab = num * i;
		printf("%d * %d = %d\n",num,i,tab);
		i++;
		
		
	}
	while(i<=10);

	return 0;
}

Run

 

Example 3

Say we want to print even numbers and odd numbers from 0-100  separately .

Program 3

#include <stdio.h>

int main()
{
int i=0;
int j=1;
printf("The even mumbers are :\n");
do
{
	printf("%d\t",i);
	i=i+2;
}
while(i<=100);
printf("\nThe odd numbers are :\n");
do
{
	printf("%d\t",j);
	j=j+2;
}
while(j<100);
return 0;
}

 

Run

 

Example 4

Say we want to print series like 30,27,24,21,18,15,12,9,3.

Program 4

#include <stdio.h>

int main() 
{
int i=30;
do
{
	printf("%d\t",i);
	i=i-3;
	
}
while(i>=3);
	return 0;
}

 

Run

 


Short URL: http://tinyit.cc/ec0f9d
By Cusp2207 on July 8, 2014 | Computer Science | A comment?
Tags:

while loop

While loop keeps on executing the given statements as long as the condition is true.When the condition  turns out to be false then set of statements in the block never gets executed and the control goes to first line after the while block.

Syntax

while(condition)

{

set_of_statements;

iteration statement;

}

If the condition is true,then set of statements are executed.The value of i is iterated and then control again returns to while condition.If condition is true again then same process is followed otherwise control exits the loop.

 

Example 1

Say we need to print  numbers from 0 to 100.while loop can also be used instead of for loop to fulfill this task.

Program 1

#include <stdio.h>

int main() 
{
int i=0;
while(i<=100)
{
	printf("%d \t",i);
	i++;
}
	return 0;
}

 

Run

 

 

Example 2

Say we want to print a table of a user entered number.

Program 2

#include <stdio.h>

int main()
{
int n,i=1;
int res;
//printf("Enter the number");
scanf("%d",&n);
while(i<=10)
{
	res = n * i;
	printf("%d * %d = %d\n",n,i,res);
	i++;
}
	return 0;
}

 

Run

 

Example 3

Say we want to print even numbers and odd numbers from 0-100  separately .

Program 3

#include <stdio.h>

int main() 
{
int i=0;
int j=1;
printf("The even numbers are:\n");
while(i<=100)
{
	printf("%d\t",i);
	i=i+2;
}
printf("\nThe odd numbers are :\n");
while(j<100)
{
	printf("%d\t",j);
	j=j+2;
}
	return 0;
}

 

 

Run

 

Example 4

Say we want to print series like  15625,3125,625,125,25,5.

Program 4

#include <stdio.h>

int main() 
{
int i=15625;
while(i>=5)
{
	printf("%d\t",i);
	i=i/5;
}
	return 0;
}

 

Run

 

 

Illustration 

The working of program 4 is explained in Fig 1.

Screen Shot 2014-07-08 at 11.09.51 AM

Fig 1 : Working of Program 4


Short URL: http://tinyit.cc/8489e9

for loop

Loops are control structures that are used to execute a code several times.There may be situation when we need to print series from 1-100 or we need to print our name 1000 times on screen.It will be inappropriate to use 100 or thousand  times printf for this scenario.Loops can be used to efficiently perform the task as many times as required.There are 3 types of loops used in C language.They are:

  • for loop
  • while
  • do-while

for loop

for loop is a control structure which has  initialization statement,condition  and iteration statement.

Syntax

for(initialization,condition,iteration)

{

body_of_loop

}

  • In initialization,variable is initialized i.e variable is assigned some value.Declaration can also be done in this step.
  • If the condition is true,body of the loop is executed otherwise flow of control jumps to next lines of code after the ending of loop.After this,control jumps back to increment statement.
  • The incrementation/ decrementation  is done and condition is checked again.If it is true then control goes to body of loop and executes it.
  • The same process happens again and again until the condition becomes false.
  • When the condition becomes false control exits the for loop.

Example 1

Say we need to print  numbers from 0 to 100.For loop can be used to fulfill this task.

Program 1

#include <stdio.h>

int main() 
{
int i;
printf("Numbers are ");
for(i=0;i<=100;i++)
{
	printf("%d\t",i);
}
	return 0;
}

Run

 

Illustration

The working of program 1 is explained in Fig 1.

Screen Shot 2014-07-02 at 4.25.26 PM

Fig 1 : Working of Program 1

 

Example 2

Say we want to print a table of a user entered number.

 

Program 2

#include <stdio.h>

int main() 
{
int n;
int i;
int t;
//printf("Enter the number");
scanf("%d",&n);
printf("The table of %d is:\n ",n);
for(i=1;i<=10;i++)
{
	t=n*i;
	printf("%d * %d = %d\n",n,i,t);
}

	return 0;
}

Run

 

Example 3

Say we want to print even numbers and odd numbers from 0-100  separately .

 

Program 3

 

#include <stdio.h>

int main() 
{
	int i,j;
	printf("The even numbers are:\n");
	for(i=0;i<=100;i=i+2)
	{
		printf("%d\t",i);
	}
	printf("\nThe odd numbers are:\n");
	for(j=1;j<100;j=j+2)
	{
		printf("%d\t",j);
	}
	return 0;
}

Run

 

Example  4

Say we want to print series like  64,32,16,8,4,2,1

Program 4

#include <stdio.h>

int main() 
{
	int i;
	for(i=64;i>0;i=i/2)
	{
		printf("%d\t",i);
	}

	return 0;
}

Run

 


Short URL: http://tinyit.cc/24ddd
By Cusp2207 on July 3, 2014 | Computer Science | A comment?
Tags: