For Loop ES081 PART B
For Loop ES081 PART B
For Loop ES081 PART B
for statement
- a kind of loop command wherein the execution of the
statement does not depend on another instruction.
Syntax:
for (initialization expression; loop repetition condition; update expression)
{
statement/s;
}
Basic For Loop Syntax
For loops are usually used when we know exactly how many
times we need to execute repeating block of code. It is also
good for creating definite loops.
int counter;
1. Priming: Set 2. Test Condition: 3. Update: Update the
the start value. Set the stop value. value.
Example
FALSE
Sample Problems: Implement using for loop
#include<stdio.h>
#include<conio.h>
//Make a C program to input 10 integers and will output their sum
Problem 1
int main()
{
Make a C program int num, ctr, sum=0; //declaration part
that will input 10 for(ctr=1; ctr<=10; ctr++)
{
numbers. Output the printf("Enter number%d:", ctr);
sum. scanf("%d", &num);
sum=sum+num; //can be written also as sum+-=sum;
}
printf("The sum is %d", sum);
return 0;
}
Sample Problems: Implement using for loop
int main()
{
int num, ctr, ctr_pos_odd=0, sum_pos_odd=0; //declaration part
Problem 2 float ave_pos_odd=0.0;
for(ctr=1; ctr<=10; ctr++)
{ printf("Enter number%d:", ctr);
Make a C program scanf("%d", &num);
if(num>0 && num%2!=0)
to output the { sum_pos_odd+=num;
ctr_pos_odd++;
average of all }
positive odd }
ave_pos_odd=(float) sum_pos_odd/ctr_pos_odd;
integers. );
printf("The average of all positive add ineteger is %0.2f", ave_pos_odd
return 0;
}
Types of Loop
You do not know ahead of time how many times your loop will execute.
For example, you do not know how many books a person might order.
Definite Loop:
You know exactly how many times you want the loop to execute.
not at compile time necessarily
Infinite Loop
You can still end up with an infinite loop when using for loops
while (*)
{
/* repeating block of code */
}
do {
/* repeating block of code */
} while (*);
do-while is same as while loop with difference that the condition is checked at every
iteration end. Consequence is that repeating block of code in do-while loop is
always executed at least once.
When to Use Which Loop ?
If you know (or can calculate) how many iterations you need
, then use for loop.
Otherwise, if it is important that the loop complete at least
once before checking for the stopping condition, or if it is
not possible or meaningful to check the stopping
condition before the loop has executed at least once, then
use a do-while loop.
Otherwise use a while loop.
Loop within a loop (Nested loops)
C programming language allows to use one loop inside another loop.
Following section shows few examples to illustrate the concept.
a] *
**
* * *
* * * *
Solution to Figure a:
#include<stdio.h>
#include<conio.h>
//This program will print a box of asterisks
int main(){
int row, col, max_col, max_row;
printf("Enter maximum rows:");
scanf("%d", &max_row);
max_col=max_row; //since figure is square
for(row=1; row<=max_row; row++) //this is for outer loop: row
{
for(col=1; col<=row; col++) //this is for inner loop: column
{
printf("*");
}
printf("\n"); //this will position the cursor to the next row
}
return 0;
}
Loop within a loop (Nested loops)
The syntax for a nested while loop statement in C
programming language is as follows:
while(condition)
{
while(condition)
{
statement(s);
}
statement(s);
}
Loop within a loop (Nested loops)
The syntax for a nested do...while loop statement in C programming
language is as follows:
do
{
statement(s);
do
{
statement(s);
}while( condition );
}while( condition );
Loop within a loop (Nested loops)