(Programming) Understanding Nested For Loops

preview_player
Показать описание
Nested "for" loops are among the trickiest programming concepts for absolute beginners--or at least they were for me. The goal of this brief tutorial is to make them much easier to understand and get you on the path to using them in your own projects.
Рекомендации по теме
Комментарии
Автор

thank you for this. This helped me like crazy.

drewdwyer
Автор

What a wonderful explanation. I would add another video where a variable from the outer loop is used in the inner loop.

paininmydroid
Автор

Nice video! I’m learning C# at the moment so this is helpful

shadowspider
Автор

Thank you so much. You really helped me a lot in understanding programming. I love to see more programming tutorials.

hiimdaisy
Автор

Thank you for this helpful explanation!

lodgechant
Автор

I love it, I'll compile a list neat tricks I've learned later. Basic arrays might make an excellent video. Still going to college for that ba in cs. Hopefully what I post will be useful.
A for loop can be broken down like this. Functions might be a must too.

for //This is the terminology for each part of the for loop. Comments like this are not used by the computer.
"Code" //You can comment lines of code like this. A comment is a personal note on what something is/how it works. Comment EVERYTHING. You'll thank yourself later.
} //You can use comments to toggle parts of your code on and off without deleting it if you wish. Look for the comment selection shortcut in your IDE.
//Beware! Not all code comments this way! --Lua comments are like this.

for loops work like this:
for ("Make your data members/values here"; "Check if the condition is true, run code if false"; "Code to run if false, this is usually to update i"){
"If the check proved false, then run this code in the brackets here"
}

"i" stands for index. You don't need to use "i" as the name for your index, but you cannot initialize it twice in a nested for loop, hence why "j" is used for the second loop. You can initialize anything, and as much as you want in your for loop's initialization. You can even use functions/methods that have a return value. Your for loop's increment/update can be anything, that code will run if the statement is false.

"i++" is shorthand for "i = i +1", becuase it is smaller and nicer. Not all code uses this shorthand. "i--" is the opposite. These either add or subtract i by one.

When making a for loop, make sure that your condition does not use a "==" unless it is checking a bool. What happens to the loop when you accidentaly go over the number you want to check? That's right, the loop will run for all of eternity, or until you close the app. You always want to check with a <, >, <= or >=. ">=" represent greater than or equal to.

For loops are very useful, even nested ones. You can also use the many other types of loops as well, I would recommend looking those up. When making loops or any other code, you should find a means of adding new lines in a functional way that is easy to read. This is why no one writes code all on the same line, it's impossible to read. Make rules for yourself, "do I add a new line after every bracket?". Make sure it looks good, is easy to read, is well commented and is consistently done every time. You and everyone reading your code will thank you.

BFTAC
Автор

i love you bro <3 thank u, ive been slamming my head for past 2 hours trying to figure out the sequencing order on inner vs outer loops.

MultiQadra
Автор

I was hoping this would be taught in C++ in id Tech 2 or Source Engine... Void forbid Unreal Engine 5.

OnyeNacho