0% found this document useful (0 votes)
4 views8 pages

9-For Loops

The document is a lab report on 'For Loops' in computer programming, detailing the syntax, examples, and explanations of for loops, nested loops, break, and continue statements. It includes practical programming tasks that utilize for loops to solve various problems. The report is structured with a table of contents and examples to illustrate the concepts discussed.

Uploaded by

eishaahmed17
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)
4 views8 pages

9-For Loops

The document is a lab report on 'For Loops' in computer programming, detailing the syntax, examples, and explanations of for loops, nested loops, break, and continue statements. It includes practical programming tasks that utilize for loops to solve various problems. The report is structured with a table of contents and examples to illustrate the concepts discussed.

Uploaded by

eishaahmed17
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/ 8

DEPARTMENT OF AVIONICS

ENGINEERING

SUBJECT : Computer Programming Lab

LAB NO : 09

TITLE : For Loops


SUBMITTED TO : Ms. Maha Intakhab Alam
SUBMITTED BY :
BATCH : Avionics 10
SECTION :
Marks Obtained :

Remarks:

DEADLINE:

DATE OF SUBMISSION:

Table of Contents
1 For Loop.....................................................................................................2
Syntax..........................................................................................................2
Example........................................................................................................3
Example explained.......................................................................................3
2 Nested Loops.............................................................................................3
Example........................................................................................................3
Example........................................................................................................4
Example........................................................................................................4
Example........................................................................................................4
3 Break.........................................................................................................4
Example........................................................................................................5
4 Continue....................................................................................................5
Example........................................................................................................5
5 Break and Continue in While Loop.............................................................5
Break Example.............................................................................................5
Continue Example........................................................................................6
6 TASKS........................................................................................................6
5 TASKS……………………………………………………………………………………..
………………….7
1 FOR LOOP
When you know exactly how many times you want to loop through a block of
code, use the for loop instead of a while loop:

Syntax
for (expression 1; expression 2; expression 3) {
// code block to be executed
}

Expression 1 is executed (one time) before the execution of the code block.

Expression 2 defines the condition for executing the code block.

Expression 3 is executed (every time) after the code block has been executed.

The example below will print the numbers 0 to 4:

Example
int i;

for (i = 0; i < 5; i++) {


printf("%d\n", i);
}

Example explained

Expression 1 sets a variable before the loop starts (int i = 0).

Expression 2 defines the condition for the loop to run (i must be less than 5). If
the condition is true, the loop will start over again, if it is false, the loop will end.

Expression 3 increases a value (i++) each time the code block in the loop has
been executed.
2 NESTED LOOPS
It is also possible to place a loop inside another loop. This is called a nested
loop. The "inner loop" will be executed one time for each iteration of the
"outer loop":

Example
int i, j;

// Outer loop
for (i = 1; i <= 2; ++i) {
printf("Outer: %d\n", i); // Executes 2 times

// Inner loop
for (j = 1; j <= 3; ++j) {
printf(" Inner: %d\n", j); // Executes 6 times (2
* 3)
}
}

Example
To demonstrate a practical example of the for loop, let's create a program
that counts to 100 by tens:

for (i = 0; i <= 100; i += 10) {


printf("%d\n", i);
}

Example

In this example, we create a program that only print even values between 0 and
10:

for (i = 0; i <= 10; i = i + 2) {


printf("%d\n", i);
}
Example
And in this example, we create a program that prints the multiplication table for
a specified number:

int number = 2;
int i;

// Print the multiplication table for the number 2


for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", number, i, number * i);
}

return 0;

3 BREAK
You have already seen the break statement used in an earlier chapter of this
tutorial. It was used to "jump out" of a switch statement.
The break statement can also be used to jump out of a loop.
This example jumps out of the for loop when i is equal to 4:

Example
int i;

for (i = 0; i < 10; i++) {


if (i == 4) {
break;
}
printf("%d\n", i);
}

4 CONTINUE
The continue statement breaks one iteration (in the loop), if a specified
condition occurs, and continues with the next iteration in the loop.
This example skips the value of 4:
Example
int i;

for (i = 0; i < 10; i++) {


if (i == 4) {
continue;
}
printf("%d\n", i);
}

5 BREAK AND CONTINUE IN WHILE LOOP


You can also use break and continue in while loops:

Break Example
int i = 0;

while (i < 10) {


if (i == 4) {
break;
}
printf("%d\n", i);
i++;
}

Continue Example
int i = 0;

while (i < 10) {


if (i == 4) {
i++;
continue;
}
printf("%d\n", i);
i++;
}
6 TASKS
Many of the problems listed can be efficiently solved using a for loop that are
particularly well-suited for a for loop due to their iterative nature:
I. Write a C program to print numbers from 1 to n using a for loop
#include <stdio.h>
int main() {
int n;

printf("Enter a positive integer: ");


scanf("%d", &n);

for (int i = 1; i <= n; i++) {


printf("%d ", i);
}
return 0;
}

Adding checks using if/else:

#include <stdio.h>

int main() {
int n;

printf("Enter a positive integer: ");


scanf("%d", &n);

if (n > 0) {
for (int i = 1; i <= n; i++) {
printf("%d ", i);
}
} else {
printf("Please enter a positive integer.\n");
}

printf("\n");
return 0;
}
II. Write a C program to calculate the sum of the first n natural
numbers using a for loop.
III. Write a C program to calculate the factorial of a given number using
a for loop.
IV. Write a C program to print the multiplication table of a given
number using a for loop.
V. Write a C program to calculate the sum of all even numbers from 1
to n using a for loop.
VI. Write a C program to calculate the power of a number x^y using a
for loop.
VII. Write a C program to calculate the power of a number x^y using a
for loop.
VIII. Write a C program to count the number of digits in a given number
using a for loop.
IX. Write a C program to print the multiplication table of numbers from
1 to 10.
X. Write a C program to generate a random password of a specified
length. Use rand() command. Include <stdlub.h> library to use it.
XI. Write a C program to calculate the salary of employees based on
their hours worked and hourly wage.
XII. Write a C program to create a menu interface for a game. Include
options such as start game, load game, settings, exit, etc. Use a for
loop to continuously display the menu until the user chooses to exit.
XIII. Write a C program to implement a basic character creation system
for a game. Prompt the user to enter their character's name, choose
a class (warrior, mage, archer, etc.), and allocate points to different
attributes (strength, intelligence, agility, etc.). make it so the user is
asked to make character every time until he/she says stop.
XIV. Write a C program to implement a high score tracking system for a
game. Allow the user to enter their score after completing a game,
and display the top 10 high scores using a for loop.
XV. How many and which programs of while loop can be easily done
using for loop instead? List them?

You might also like