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);)


Sign In
Create Account




Back to top









