Professional Documents
Culture Documents
1
Loops
Loop with “if” using goto and label
Example : Demonstrates use of if for iteration
#include <stdio.h>
int main()
{
int sum=0, i=1; /*declaration and
initialization combined*/
Step3: /*label- loop starts here*/
if (i<=10)
R. Subburaj, “Programming in C” © Vikas Publshing House Pvt. Ltd, New Delhi 2
Loops (Contd.)
{
sum= sum+i;
i=i+1;
goto step3;
}
printf(“sum of first 10 natural numbers=%d\n”,
sum);
}
Result:
sum of first 10 natural numbers=55
R. Subburaj, “Programming in C” © Vikas Publshing House Pvt. Ltd, New Delhi 3
for statement
for (exp1; exp2; exp3)
{statements; }
Note the keyword, the parentheses and
semicolons. No semicolon after exp3.
exp1, exp2 and exp3 are expressions.
exp1 – contains the initial value of an index
or a variable.
exp3 – contains the alteration to the index
after each iteration of the body of the for
statement.
STATEMENTS
STATEMENTS
STATEMENTS
TRUE
While
condition
FALSE
if(a>0)
printf(“you entered %d\n”, a);
if (a<0)
{ printf(“you entered a negative
number\n”);
continue;
}
}
while(a!=0);
printf(“End of session\n”);
30
Arrays
40
Modular Programming Concept
MAIN
f1 f2 f3 f4