9-For Loops
9-For Loops
ENGINEERING
LAB NO : 09
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 3 is executed (every time) after the code block has been executed.
Example
int i;
Example explained
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:
Example
In this example, we create a program that only print even values between 0 and
10:
int number = 2;
int 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;
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;
Break Example
int i = 0;
Continue Example
int i = 0;
#include <stdio.h>
int main() {
int 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?