Arrays and for Loops in C

For Complete YouTube Video: Click Here

In this class, we will try to understand the Arrays and for Loops in C.

The concepts of an array and array initialization have been discussed in our previous classes.

Array and for loop

Instead of the initialization of an array at the time declaration, we can assign the values of an array at run time.

There are any other constructs where arrays and for loops are used extensively in programming.

The use of the for loops and the arrays is regularly used in every program.

General constructs of the for loop and arrays

The images below shows the use of the for loops and arrays.

The first construct in the above image shows how to clear the elements of an array to zero.

Arrays and for loops in C First Construct
Arrays and for loops in C First Construct

The values of the array have been initialized to the values from one to ten.

In the for loop, we are reassigning all the values in the array to zero.

The loop starts from zero and will iterate up to nine every time it will update an element to zero.

The second construct is used to take the values of an array for the end-user.

Arrays and for loops in C Second Construct
Arrays and for loops in C Second Construct

Here the user of the program can provide the values to the array.

In every iteration, the loop will wait for the user to provide the value.

This construct is used very frequently in programming.

Would you please try to practice this construct for better understanding?

The third construct is used to print the values of an array.

Arrays and for loops in C Third Construct
Arrays and for loops in C Third Construct

In every iteration, the loop will print the value of the array element.

This construct is used very frequently in programming.

Would you please try to practice this construct for better understanding?

The fourth construct is used to sum the values of all the elements of an array.

Arrays and for loops in C Fourth Construct
Arrays and for loops in C Fourth Construct

Initially, the array is assigned with values, and the variable sum is declared to zero.

In every iteration, the value of the array’s element is added to the sum.