Loops - C++ Tutorial For Beginners #11
Key Takeaways
This video tutorial covers the basics of loops in C++, including while loops, do-while loops, and for loops, demonstrating how to use them for counting, iterating, and conditional statements.
Full Transcript
[Music] what is going on guys welcome back to the c plus plus tutorial series in today's video we're going to talk about loops so let us get right into it all right so loops are just basic programming structures that allow us to execute the same block of code repeatedly either as long as a certain condition is met or a specific amount of times we have different types of loops for that we have two major different types and a third different type which is actually just a slight variation of the first one and the first one we're going to talk about is the while loop and it's initialized with the keyword while followed by a condition we already know what conditions are expressions that result in a boolean value so either true or false this can be a boolean value itself so either true or false uh this can be a function that returns a boolean this can be a comparison whatever we want as long as is as long as it results in a boolean sorry it is a condition it can be stated inside of those parentheses here and as long as this condition inside of those parentheses returns true we're going to execute this code over and over and over again so in this case this would go on forever this would never stop because while true obviously true is always true it's not going to change it's a constant a boolean constant we're going to execute the code here over and over and over again but we can also go ahead with a slightly different example here we can go ahead and say int i and then we can say std c in i so we're getting the user input saving it into i and then what we can do is we can say while i is larger than zero greater than zero we're going to say std c out i and then we're going to decrease i by one so we're going to say or actually we can go ahead and do this directly in here so we can say i minus minus notice again that i minus minus means that we're printing i as it is and then we're decreasing it by one not the other way around so we're not doing this we're doing i minus minus um and essentially what this does is the user inputs a number and as long as this number is positive we're decreasing it and printing the value so we're doing some sort of countdown here uh by the way i need to turn off my antivirus system uh because otherwise whenever i compile a c plus code it's uh turning on i don't know why then we can go ahead compile this and i can input a number here let's say i go with 8 and as you can see it says eight seven six five four three two one uh we could also go ahead to zero if we want uh so we can either go ahead and print if we don't wanna decrease it further but we wanna print the value when it reaches zero we can go ahead and say c out i at the end again because if you just go ahead and say large or equal to zero you can print zero but it's going to end up uh as minus one it's negative one so in this case it will still be zero in the end but it will also be printed so we can go ahead now with 30 and you can see it does the full countdown up until zero or down until zero so this is the basic while loop how it works now before we get to the second major type of loop we're going to cover a slight variation of the while loop and this is the so called do while loop so in this case we say do specify the block of code the loop code essentially and then we add a while in the end so we do it like that and we can do the same thing actually and the difference here is that we check for the condition after the first iteration so this means that no matter what the condition is we're going to execute this block of code at least once so in this case uh we can we can do the same thing we can say std c out i minus minus s to the end line uh and this code will be executed here even if we start with a number that is below zero which otherwise would not happen in ordinary while loop because the condition gets checked immediately and you don't even enter the loop if the condition is not true in this case we do it at least once and then if the condition is still met we can do it as long as the condition is met but we're going to do it at least once this is the do while loop and we can check it out um so i can go ahead first and enter a valid number like 10 and you can see it works like the while loop but now if i go ahead and enter something like negative nine you can see that it prints negative nine at least once uh it also decreases it we can we can check that this is true by just printing the value of i in the end so you can see that it's still or not still actually it's going to be negative 10 if i enter negative 9 as you can see but it doesn't continue or it doesn't repeat the code over and over again it does it one time because it has to do at one time even if that condition is false we can check that by saying while false so even if the condition is definitely false 100 false it's going to execute this code at least once no matter what i put in so 10 it's going to say 10 and then afterwards 9 because it's decreased of course so this is the difference if we do a while loop we're going to check for the condition immediately a do while loop we're going to execute the code once and after that we're going to check for the condition so you can imagine it to be like doing this for sure one time and then we can start with a new ordinary while loop this is the same thing so the second major loop type that we're going to look at is called the for loop and it's initialized with the keyword four followed by parentheses the loop hat or header so to say then we have the block of code where we do something and inside of the head or the header of the for loop what we specify are three things first of all a control variable this can be a variable that was defined in the uh in the program before so we can say in i here and we just have to initialize it here saying i equals 10 or something like that or we can say and i directly in there so i can go ahead here and say in i equals zero uh the second thing that we specify after the semicolon here uh we specify a condition which says do this or execute this block of code as long as this condition is met so this is something that we already know from the while loop uh stuff like where something like i is less than 100 and then an action that is happening with each iteration so something like i plus plus in this case what we do is we'll create a variable starting at zero and as long as this variable is uh below 100 we're increasing it by one and executing the code inside of this loop here so for example we could just go ahead and say stdc out hello and what this will do is it will print hello a hundred times like that we can go ahead and see what happens you can see it prints hello a hundred times we can also go ahead and print i of course because i is a variable that is if we define it here like that in i equals zero uh it's valid or accessible in the scope of that loop of course we cannot access i here so this is not valid we cannot find the variable i in here it's only it only exists in the loop which is also the reason why we can define it here again so this is not a violation of any rules and if we do that we can see it's the same thing like counting we can also start at 100 for example and say as long as it's larger than 100 we're going to decrease it by one and then it's more like a countdown uh what's happening here why didn't it work it's a hundred less oh greater than zero sorry not greater than 100 and then it works like countdown as you can see so the major difference here is that we specify the for loop more uh in the header it's more used for counting and iterating so when we get to collections to lists to arrays and so on we're going to use more for loops whereas while loops are actually if you want to do something like as long as i don't get a trigger as long as some boolean is not true or or not false actually as long as something is met let me do this if you want to have that kind of loop you're going to use a while loop and if you want to count if you want to iterate if you want to do something a certain amount of times you're going to use the full loop so that's it for this we hope you enjoyed it and hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course subscribe to this channel and hit the notification bell if you don't want to miss a single future video for free other than that thank you very much for watching see you next video and bye [Music] you
Original Description
Today we talk about loops in C++.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
🐍 The Python Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: LLM Foundations
View skill →Related Reads
📰
📰
📰
📰
AI, Payroll, and Policy: What HR Leaders Must Know Now.
Medium · Programming
7 AI Notion Workflows That Actually Run in 2026 (Honest Comparison, incl. Easlo & Thomas Frank)
Dev.to AI
ChatGPT Plus and Claude Pro reject your card? It is probably the billing country, not the card
Dev.to · Tung@fizen
👾 🧚🏼♀️Maximizing Fable for Life Admin
Dev.to · Anna Villarreal
🎓
Tutor Explanation
DeepCamp AI