c Comma Operator in ‘for’ Loops


The Comma Operator in ‘for’ Loops
The COMMA OPERATOR (,) is primarily used in conjunction with the ‘for’
statement
This OPERATOR permits two different expressions to appear with in the
‘for’ Loop Syntax.
Syntax:
for(expr1, expr2; expr3; expr4, expr5)
{
Executional Statements;
}
Illustrative Example1
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int x = 0, j, k;
clrscr();
printf("\n\nThe Values of K, J, X are...\n\n");
printf("\n\tK\tJ\tX\n");
printf("\t-----------------\n");
for(k = 0, j = 5; k < 5, j > 0; k = k + 2, j--)
{
x++;
printf("\n\t%d\t%d\t%d", k, j, x);
}
}
Illustrative Example2
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int x = 0, j, k;
clrscr();
printf("\n\nThe Values of K, J, X are...\n\n");
printf("\n\tK\tJ\tX\n");
printf("\t-----------------\n");
for(k = 0, j = 5; j > 0, k < 5; k = k + 2, j--)
{
x++;
printf("\n\t%d\t%d\t%d", k, j, x);
}
}
Program to print a sequence top to down and vice-versa
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int up, down;
clrscr();
printf("\n\tUp\tDown\n");
printf("\t-----------\n");
for(up = 1, down = 10; up <= 10; up++, down--)
printf("\n\t%d\t%d", up, down);
}


No comments: