Contents
for loop in C language
for You can use a loop instead of while a loop when you know exactly how many times you want to loop through a block of code
for (statement 1 ; statement 2 ; statement 3 ) {
// code block to be executed
}
- Statement 1 is executed (once) before executing the code block.
- Statement 2 defines the conditions under which the code block is executed.
- Statement 3 is executed after executing the code block (every time).
The following example will print the numbers 0 through 4:
Example
int i;
for (i = 0; i < 5; i++) {
printf("%d\n", i);
}
Example explanation
- Statement 1 sets a variable ( ) before the loop begins
int i = 0. - Statement 2 defines the condition under which the loop runs (
imust be less than5). If the condition is true, the loop will start again, if it is false, the loop will end. - Statement 3 increments a value (
i++) each time the block of code in the loop is executed.
another example
This example will only print even numbers between 0 and 10:
Example
for (i = 0; i <= 10; i = i + 2) {
printf("%d\n", i);
}
Nested loops
You can also place a loop inside another loop. This is called a nested loop.
The “inner loop” will be executed once on each iteration of the “outer loop”:
Example
int i, j;
// Outer loop
for (i = 1 ; i <= 2 ; ++i) {
printf ( "Outer: %d\n" , i); // Execute 2 times
// Inner loop
for (j = 1 ; j <= 3 ; ++j) {
printf ( " Inner: %d\n" , j); // Execute 6 times (2 * 3)
}
}
real life examples
To show fora practical example of looping, let’s create a program that prints the multiplication table for a given number:
Example
int number = 2;
int i;
// Print the multiplication table of number 2
for (i = 1 ; i <= 10 ; i++) {
printf ( "%dx %d = %d\n" , number, i, number * i);
}
return 0;
breakand in C languagecontinue
break
You’ve seen breakthe use of the statement earlier. It is used for “jump out” switchstatements.
breakStatements can also be used to break out of loops.
This example breaks out of the loop when iis equal to :4for
int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
break;
}
printf("%d\n", i);
}
continue
If the specified condition occurs, continuethe statement interrupts one iteration of the loop and continues with the next iteration.
This example skips values 4:
int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
printf("%d\n", i);
}
whilebreakand in the loopcontinue
You can also whileuse breakand in a loop continue:
breakExample
int i = 0;
while (i < 10) {
if (i == 4) {
break;
}
printf("%d\n", i);
i++;
}
continueExample
int i = 0;
while (i < 10) {
if (i == 4) {
i++;
continue;
}
printf("%d\n", i);
i++;
}
C array
Arrays are used to store multiple values in a single variable instead of declaring separate variables for each value.
create array
To create an array, define the data type (for example int) and specify the array name, followed by square brackets [].
int myNumbers[] = {25, 50, 75, 100};
Now we have created a variable that contains an array of four integers.
Access array elements
To access an array element, refer to its index number.
Array indexes 0start at : [0]the first element, [1]the second element, etc.
printf ( "%d" , myNumbers[ 0 ]); // Output 25
Change array elements
To change the value of a specific element, see the index number:
myNumbers[0] = 33;
Loop through array
You can use fora loop to iterate over array elements.
for (int i = 0; i < 4; i++) {
printf("%d\n", myNumbers[i]);
}
Set array size
Another common approach is to create an array, specify the size of the array, and then add elements:
int myNumbers[4] = {25, 50, 75, 100};
With this approach, you should know the number of array elements in advance so that the program stores enough memory.
Get array size or length
To get the size of an array, you can use sizeofthe operator:
int myNumbers[] = {10, 25,
50, 75, 100};
int arraySize = sizeof(myNumbers) / sizeof(myNumbers[0]);
printf ( "%d" , arraySize); // Output 5
improvement cycle
In a loop, it’s better to use the size of the array to iterate over, to accommodate arrays of different sizes:
int myNumbers[] = {25, 50, 75, 100};
int arraySize = sizeof(myNumbers) / sizeof(myNumbers[0]);
for (int i = 0; i < arraySize; i++) {
printf("%d\n", myNumbers[i]);
}
