Assignment #3 C program (1)
Assignment #3 C program (1)
In C programing language iteration can be defined as repeating the same process multiple
times until a specific condition satisfies. Simply we can say that it is the process of repeatedly
executing a block of code . The term looping is also used for iteration. Further the concept of
iteration is that it allows you to automate repetitive tasks , Making your code more efficient and
concise. instead of writing the same lines of code multiple times, you can use a loop (iteration)
to execute them repeatedly.
_FOR LOOP
It is also known as count controlled it is used
When you know in advance how many times you need to repeat a block of code.A loop iterates
a specific number of times controlled by counter variable.
It includes initialization, condition checking, and increment/decrement within its syntax.
Example of For loop
Iterating through a list of items .
_while loop:
The term condition controlled is also used for while loop and it is Used when you need to repeat
a block of code as long as a certain condition remains true.
The condition is checked before each iteration and the iteration should continue as long as
certain condition is met .
_do-while loop:
So the do while loop is also known as guaranteed execution
It is Similar to the while loop, but it guarantees that the code block will execute at least once.
The condition is checked after each iteration.
Example
Displaying a menu and Allowing the users to choose an option
So this was the simple structure for iteration in c language however
Iteration is essential for tasks like processing arrays, reading files, and performing calculations
repeatedly.
Properly managing loop conditions is important to avoid infinite loops.
In essence, iteration in C, facilitated by loops, is a fundamental programming concept that
allows for the efficient execution of repetitive tasks.
By allowing you to control the initialization, condition, and update of variables in one place, it
simplifies repetitive operations. In this post, we’ll explore everything about the for loop, including
how it works, its syntax, real-life applications, and examples to help you master it.
Like other loops, for loop in C programming runs a block of code several times until a condition
is met. More completely, for is a statement in the C programming language that will repeat a
block of code a specific number of times.The loop consists of three main parts: initialization(
how it started), condition(when it ends), and increment/decrement,(update from loop to loop) all
defined in a single line, making it concise and efficient.
SYNTAX OF FOR LOOP IN C
for (initialization; condition; increment/decrement) {
// Code to execute in each iteration
}
Components of the for loop syntax:
1. Initialization:
This step initializes the loop control variable (e.g., int i = 0). It runs only once, at the start of the
loop.
2. Condition:
The condition is a logical expression that determines whether the loop should continue. If the
condition is true, the loop executes; if false, it terminates.For example: i < 10;
3. Increment/Decrement:
It updates the loop control variable after each iteration. Also, it ensures the loop progresses
towards termination.It's typically used to update loop counter variables.
For example: i++; or i = i + 2;
4. Loop Body:
It contains the statements to execute repeatedly. The loop body runs every time the condition
evaluates to true.The code enclosed within the curly braces {} is the loop body.
#include <stdio.h>
int main() {
int n, i;
float tradeSum = 0, tradeAvg;
// Asking user for the number of trade entries
printf("Enter the number of trade records: ");
scanf("%d", &n);
float tradeValues[n];
return 0;
}
It's a simple C program for Data Analysis in IR.