for loop c programming practice

Posted under C++ Course On By mohammed.mutawe

For loop c programming practice

The topic of for loops is a little annoying for beginners, so we ask you to focus and we will try to simplify the topic as much as possible in this lesson.

For loop is a function that starts with the word for and the condition and implementation inside the parentheses, and the for loop is an important nerve for the programming language C++ and other programming languages, it shortens the many conditional sentences and when passing through the condition it executes it automatically.

 

 

Benifits

If we assume that you want to search in an array containing 8 elements, it would be as follows:

#include <string> 
#include <iostream> 


int main()
{
    using std::string; 
string str[8] = { "Welcome" , "To" , "C++" , "Hello" , "World" , "Free" , "circle" , "online"};

    
}

And you want to print its elements on your console screen, you will print the elements of the array element by element, so that the code will look like this:

#include <string> 
#include <iostream> 


int main()
{
    using std::string; 
    string str[8] = { "Welcome"  , "To" , "C++" , "Hello" , "World" , "Free" , "circle" , "online"};

    std::cout << str[0] << std::endl;
    std::cout << str[1] << std::endl;
    std::cout << str[2] << std::endl;
    std::cout << str[3] << std::endl;
    std::cout << str[4] << std::endl;
    std::cout << str[5] << std::endl;
    std::cout << str[6] << std::endl;
    std::cout << str[7] << std::endl;

    
}

This is called HardCoding, which is dealing with a number of data manually, and this is very cumbersome if you want to print 200 items, for example, not at the level of printing, but also in fetching and processing data, it will be difficult.

 

Therefore, forloops came to be the appropriate and quick solution to walk through all the elements of the arrays without suffering, and in order to display that data quickly, we will make a forloop statement based on fetching the elements of the array and printing them.

 

2. How it works

The well-known and stable forloop components are:

  • The word for () : Two parentheses are attached and inside the parentheses a variable that acts as the counter int i = 0, to reach the mentioned value i < 8, which is the elements of our matrix above, to increase with each cycle i++ until it reaches the number 8.
  • Braces {}: to process the values ​​inside them, and in our lesson, the elements of the array will be printed.

 

Applying

Step by step we will write our code first:

  • Prepare the for loop body to become like this:
#include <string> 
#include <iostream> 


int main()
{
    using std::string; 
    string str[8] = { "Welcome"  , "To" , "C++" , "Hello" , "World" , "Free" , "circle" , "online"};

    for (int i = 0; i < 8; i++)
    {

    }
    
}

 

As we mentioned earlier, the variable i is the counter for that iterative loop, and we should not forget to put the semicolon in C in this way; , and the number of rotations, and the increase.

Well, things are still a little difficult for beginners, let’s imagine that there is a pen that writes in the for loop, and in each cycle it adjusts the value of the counter, increases it, enters the body of the for loop, and returns again to the beginning of the for loop, adjusts the counter values ​​and enters again, and so forth.

In fact, the pen is the translator, who keeps spinning in the iterative loop until it ends and then goes to other tasks or ends the program. This is the rhythm of programming. If you master it in understanding iterative loops, you will understand a lot later.

 

  • Put the print statement inside the for loop:
#include <string> 
#include <iostream> 


int main()
{
    using std::string; 
    string str[8] = { "Welcome"  , "To" , "C++" , "Hello" , "World" , "Free" , "circle" , "online"};

    for (int i = 0; i < 8; i++)
    {

        std::cout << str[i] << " ";
    }

    std::cout << "\n";
    
    
    system("PAUSE");

    return 0;
}

In order to enhance the importance of the for loop in our lesson today, we must realize that arrays have addresses and elements in memory, and each address is in a different place in memory, for example, if we want to access the element str[3], which contains the value “Hello”, in the iterative loop counter The counter i must be placed inside the numbering of the elements of the array so that it becomes as in the code in this form str[i] , with the presence of conditional clauses, you can request the element by name and these are the benefits of conditional clauses and loops in modern programming languages ​​and databases.

 

Example

Please, to understand the loops well, copy the following code into your code editor.

#include <string> 
#include <iostream> 


int main()
{
    using std::string; 
    string str[8] = { "Welcome"  , "To" , "C++" , "Hello" , "World" , "Free" , "circle" , "online"};

    for (int i = 0; i < 8; i++)
    {

        std::cout << str[i] << " ";
    }

    std::cout << "\n";
    
    
    system("PAUSE");

    return 0;
}

 

Leave a comment

Your email address will not be published. Required fields are marked *