0% found this document useful (0 votes)
18 views7 pages

Wa0015.

The document is an assignment by Laraib Rodeni, a second-year student in International Relations, discussing iteration in C programming. It explains the concept of iteration, detailing three loop structures: for loop, while loop, and do-while loop, along with their syntax and applications. Additionally, it includes a C program example for calculating the average trade value from user input.

Uploaded by

mujeebzehri251
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

Wa0015.

The document is an assignment by Laraib Rodeni, a second-year student in International Relations, discussing iteration in C programming. It explains the concept of iteration, detailing three loop structures: for loop, while loop, and do-while loop, along with their syntax and applications. Additionally, it includes a C program example for calculating the average trade value from user input.

Uploaded by

mujeebzehri251
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Name Laraib Rodeni

Seat No:B23161006047
Deptt: International relations
Class: 2nd year (3rd semester)

Assignment #3 C program

Q1:What is Iteration in C Language?

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.

Loop (iteration) structures in C:

C provides three primary loop (iteration) structures:


1for loop

2While loop

3Do while loop

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 UJsed 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.

EXAMPLE OF WHILE LOOP

Reading input from the users untill a specific value is entered. 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.

What is for loop in C language?


FOR LOOP IN C LANGUAGE

Repetition is a key aspect of programming, and the C language for loop is a powerful tool to
handle it effectively. When you need to perform a task multiple times, whether it's iterating
through an array,generating patterns, or summing up numbers, the for loop in C offers a
concise and structured approach.

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 cont

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.
Q3: Write a c program for data analysis in international relations that calculates
the average trade value fromuser input.
Solution: #include

<stdio.h>

int main(){ int n;//Number of countries

double trade value, sum=0.0,average;

//Ask the user how many countries' trade values to input

printf("Enter the number of countries involved in the trade: "); scanf("%d",&n);

// Loop through each country and get the trade value

for(∫ti = 1;i <= n;i ++){

printf("Enter the trade value for country %d:",i); scanf("%lf",

&trade value); sum +=tradevalue;//Add each country's trade

value to the sum


}了
//Calculate the average trade value average=sum/n;

// Display the average trade value printf("\nThe average trade value across %d

countries is: %.2lf\n", n, average); printf("\nThis Assignment is by Laraib %s",

"Student");

return 0;

}了
Output Results:
Enter the number of countries involved in the trade:5

Enter the trade value for country 1:234

Enter the trade value for country 2:323

Enter the trade value for country 3:432

Enter the trade value for country 4: 380

Enter the trade value for country 5: 267

The average trade value across 5 countries is:329.00

Visual Ouput:

.Program finished with exit code 0


Press ENTER to exit console.This Assignment is by Laraib Student

You might also like