Jump to content

new looping techniques

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Chinmoy

Chinmoy

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 392 posts
Here are a few out of the box looping techniques.

code 1--------------->


#include<stdio.h>

#include<conio.h>

void main()

{

	int a,flag=0;

	clrscr();

	for(a=0;(a<=10 && a>=0);a++)

	{

		printf("%d ",a);

		if(a==9)

		{

			flag=1;

		}

		if(flag==1)

		{

			a-=2;

		}

	}

	getch();

}


in this code we have reversed the loop on itself and we have used a flag variable.but there is no other loop to print the reverse sequence.this makes the code compact saving size if we have more than one loop trying to implement the same( like printing the very basic star diamond
can be done with lesser no. of loops).


code 2--------------->


#include<stdio.h>

#include<conio.h>

void main()

{

	int a;

	clrscr();

	for(a=1;a<100;a++)

	{

		if(a%10==0)

		{

			printf("\n");

		}

		else if((a%10)<=(a/10))

		{

			printf("%d",(a%10));

		}

	}

}


in this code we have implemented the same size reduction technique. Conventionally we would write a code to print a sequence of:
1
12
123
1234
12345
...........
using two loops but this can easily be achieved using one loop and one variable less.This again makes the code size smaller by one variable!

also while looping with a for loop the code
for(a=0;a<10;a++)

is equivalent to
for(;(a++<10);)

Attached Files

  • Attached File  2.JPG   4.75K   30 downloads
  • Attached File  1.JPG   6.4K   22 downloads
  • Attached File  PROG2.C   210bytes   55 downloads
  • Attached File  PROG1.C   222bytes   79 downloads


#2
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
In this case:

for(;(a++<10);)

its better use:

while (a++ < 10)

In my opinion, because it's just a simple condition and a variable increment before. And also, in my opinion, i think that this looping technics don't increase processing speed, what makes them unseless if is true.