C++Introduction to C++Learn C++

Learn To Use For Loops In C++

In programming, one of the most used statement is for() loops. It is used to count in range with given conditions. If you know exactly how many times you want to execute a block of code in your loop, then they are very useful than other loops. Occasionally, it is used to calculate series in range or to list elements that have the number of elements (i.e. string lists, arrays, char arrays, vectors, structured arrays, etc. )

for() function is generally used with 3 parameters separated by ; character. the first one is initial-expression defines primary start condition (a=0 for example) and the second one is conditional expression defines looping condition ( for example a<10), this condition limits its range, and the third parameter is looping condition that defines the change in our loop statement in every loop.

We can create single-line for() loop as an example here,

We can define variable in the for() parameters as below.

or we can dive it to two lines,

these 3 examples above are same. If we have more than one line in our code block, then we must use { and } brackets, as here,

Note that in for loops last parameter has no ; character and never put ; at the end of for without statement like this for( i=0; i<10; i++ ); This example below will print out numbers between 0 and 10, including 0 and 10. The output will be 0,1,2,3,4,5,6,7,8,9,10

This code below will print out even numbers between 0 and 10, the output will be 0,2,4,6,8,10

Instead of a+2 term to increase we can use a = a+2 also as here, but remember this is slower than the previous one in operation,

This code below will print out odd numbers between 1 and 9, the output will be 1,3,5,7,9

and as same above we can also write like this too,

If you want to learn about more advanced usage and General Loop Statements in Modern C++ please check this post,

https://learncplusplus.org/general-loop-statements-in-modern-c/

 

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
Artificial Intelligence TechC++C++11C++14C++17Learn C++

Why You Should Know ELU Artificial Neural Net Functions

Artificial Intelligence TechC++C++11C++14C++17Learn C++

What You Need To Know About C++ Gaussian Error Linear Units

C++C++11C++14C++17Learn C++

What Is The SoftPlus Activation Function in C++ Neural Nets?

Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Make AI Binary/Heaviside Step Functions In C++

en_USEnglish