C Beginners Practice — For loop

Naomi Fridman
5 min readNov 7, 2021

--

The For Loop

A dear friend of mine, Aviv Ativ, is a student that learning a C coding course. To assist her in practice, I was looking for a good exercise resources. But, I didn't found enough simple basic training. So I decided to write one. Enjoy

https://pixabay.com/photos/fair-fairground-ferris-wheel-540126/

The For loop syntax

In the for loop, we perform the same code again and again, each time with a different value of the counter. The loop stops, when the check statement is not True anymore.

for (Initialize counter; Check weather to continue; Update counter)
{
// *** Code that will be performed again and again
// until the loop stops.
}

Basic example. Lets print all the natural number up to 10:

  • Initialize: i = 1, our counter is i, and we initialize it to 1.
  • Check: i < 11 , when i will be smaller then 11, the loop will be performed again. when the check statement: i < 11 will be false, the loop will stop, and the code will continue running from after the for loop block.
  • Update: i++ , after every one run of the loop , i will be incriminated by 1.
// Print natural numbers from 1 to 10
#include <stdio.h>

int main() {
int i;

for (i = 1; i < 11; i++)
{
printf("%d ", i);
}
return 0;
}

Output

1 2 3 4 5 6 7 8 9 10

Use few variables in a for loop

You can use more then one variable in the for loop. Each variable, can be initialized, used for the stop check, and get to be updated in each loop run. In the following example, we use k to count how many times the loop was running. Tip, you can use it in exercise 6.

#include<stdio.h>
void main() {
int i,n, k;
printf("Input positive number n: ");
scanf("%d",&n);
for (i=3, k = 1; i<=n; i=i+3, k++) { printf(" The loop is running the #%d time. i = %d\n",k, i);
}
}

Output:

Input positive number n: 11
The loop is running for the #1 time. i = 3
The loop is running for the #2 time. i = 6
The loop is running for the #3 time. i = 9

Break statement

Break statement, breaks the loop. Meaning it stops the loop when its called, even if the stop condition defined in the loop is not met. Usually using break in a for loop is bad coding practice, but its important to be aware of the option. In the following example, we break the loop if the user enters negative number, instead of positive.

int i,n;for (i=0; i < 10; i++) {
printf("Enter a positive number n: ");
scanf("%d",&n);
if (n < 0) {
printf(" %d is not a positive number! Loop is stopped.", n);
break;
}
printf(" ** you entered n = %d\n",n);
}

Output

Enter a positive number n: 7
** you entered n = 7
Enter a positive number n: 2
** you entered n = 2
Enter a positive number n: -1
-1 is not a positive number! Loop is stopped.

To write and run your C code, you can use one of the online C compilers website. I use https://www.onlinegdb.com/online_c_compiler, and its great.

Exercises

For each exercise, the expected output is given. Your mission is to produce the same output.

  1. Print all the even numbers, up to a given number.
Enter a positive integer: 16
Positive numbers in 1...16 are: 2 4 6 8 10 12 14 16

2. Print the sum of all numbers up to a given number.

Enter a positive integer: 5
Sum of all numbers 1...5 = 15

3. Print all the number divided by 3, up to a given number, and their sum.

Enter a positive integer: 16
3 + 6 + 9 + 12 + 15 = 45

4. Ask the user to input 4 number between 1 and 100. Print min and max.

Input number between 0-100 # 1: 7
Input number between 0-100 # 2: 56
Input number between 0-100 # 3: 3
Input number between 0-100 # 4: 88
The Maximum # is 88
The Minimum # is 3

5. Print elements of arithmetic series, and Find the sum. a0, d and n are given.

Input first series number a0: 2
Input the denominator d: 3
Input series length n: 4
5 8 11 14 The sum is 40

6. Divide given number by 3, if dividable, else print that its not.

Input a number n: 9
9 divided by 3 = 3

or

Input a number n: 5
5 is not divided by 3

Solutions

// Solution 1
#include <stdio.h>
int main()
{
int num, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Positive numbers in 1...%d are: ", num);

for(i = 2; i <= num; i = i+2)
{
printf(" %d ",i);
}
printf("\n");return 0;
}

Solution 2

#include <stdio.h>
int main()
{
int num, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
for(i = 1; i <= num; i++)
{
sum += i; // same as sum = sum + i
}
printf("Sum of all numbers 1...%d = %d",num, sum); return 0;
}

In the next solutions, we will write only the “important” part of the code.

Solution 3:

int sum = 0;
for(i = 3; i <= num; i = i+3) {
if (i + 3 > num) {
// In the last numbers, = (not +) is printed
printf(" %d = ", i);
} else {
printf(" %d +", i);
}
sum += i; // same as sum = sum + i

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

Solution 4:

Min = 101; Max = 0;
// Call the First number as current maximum and minimum
for (x=1;x<=4;++x)
{
printf("Input number between 0-100 # %d: ",x);
scanf("%d",&Inp);
if (Inp>Max) {
// if the next number is bigger than current maximum, store it
Max = Inp;
}
if(Inp<Min) {
// if the next number is lower than current minimum, store it
Min = Inp;
}
}
printf(" The Maximum # is %d\n",Max);
printf(" The Minimum # is %d\n",Min);

Solution 5

sum=a0;
for (i=1;i<=n;i++) {
element = a0 + d* i;
sum += element;
printf("%d ", element);

}
printf(" The sum is %d\n",sum);

Solution 6

is_devided_by_3 = 0;
for (i=3, k = 1; i<=n; i=i+3, k++) {
if (i == n) {
is_devided_by_3 = 1;
printf(" %d divided by 3 = %d\n", n, k);
}

}
if (is_devided_by_3 == 0) {
printf(" %d is not divided by 3\n", n);
}

Or without using k inside the for loop declaration:

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

if (i == n) {
is_devided_by_3 = 1;
printf(" %d divided by 3 = %d\n", n, k);
}
k+=1;
}

Next Chapter --> Exercise on nested For loops

--

--

Naomi Fridman
Naomi Fridman

Written by Naomi Fridman

MSc. Mathematics. Data Scientist. Love Deep learning ,Machine learning , Mathematics and Surfing. https://github.com/naomifridman

No responses yet