C Programming Fundamentals - Loops
Key Takeaways
The video covers C programming fundamentals, specifically loops, by HackerSploit, an intermediate-level cybersecurity topic.
Full Transcript
[Music] hey guys hackersploit here back again with another video and welcome back to the c programming series in this video we're going to be taking a look at loops alright so the first question that might come into your mind is what exactly are loops is this your first time hearing about them in programming well loops essentially allow us to execute a code or uh you know a few statements multiple times so they allow you to execute the same piece of code multiple multiple times without doing it manually and the the second question that might be coming into your mind is well how exactly is this going to help us well this helps especially as i mentioned when you need to execute a code more than once or you want to repeat your code depending on on what a user prints out and we're looking at an example at the end of the video so uh when it comes down to loops there are multiple loops that you can have in programming languages you have your four loops you have your while loops uh you have your two while loops now in this video i'm going to be covering two of my favorite all two of the most important ones uh and that are the four and while loops all right so let's get started so uh when as i mentioned uh it really just comes into play when you're trying to execute code multiple times or you do not want to manually write every piece of code and you want to use you want to utilize the mathematical operators to do the work for you so in this case we're going to be starting off with the for loop and we're going to be using a simple program that i'm sure everyone has done or used and that is a program to essentially print the numbers 1 to 10 automatically the library here standard library.h and let me include my sddio my standard input output i actually should do this before but in any case let me just create a main function here and in the main function i'll also return zero so we always can check if our function is not is doing everything correctly all right so the first thing i'm going to do is i'm going to create an integer i'm going to create a variable in x and this is not going to have any value and i'll talk about initialization in a second so the structure of a for loop is really very simple uh simple in the sense that it's very simple to understand how it goes about essentially how the initialization the initialization goes about so the syntax is as follows so you have your for loop you started with four and in parentheses is where you initialize the variable so i could have initialized the variable saying in x equal and i give it a value the value is very important because it acts as the counter all right you then have the condition under which you're performing the loop and finally you have the increment so let me just give you a very small example so i can say in x but in this case i've already initialized it outside of the for loop which i want in this case so i'm just going to say x is equal to uh i'll give it a value of 1 meaning we'll start off from 1 and then since this this loop is going to print out numbers from 1 to 10. we'll start off we'll keep the value from 1. you can also change it to 0 if that's what you want now it is the condition under which this loop will be will be executed and when it will stop so i'm going to say execute this loop until the value of x is going to be i can say whatever i want in this case we're going to say i want you to keep it less than 10. so i want you to execute this loop until the value of x is less than 10 once it crosses that threshold stop the loop or break the loop so use a semicolon there to separate these these conditions and then finally i use the increment operator so every time this loop runs i want to increase the value of x by one you can change this to whatever you want so i'm going to say x is equal to x plus one that is standard programming but you know incrementing a variable by one or incrementing itself every time you go through a loop and then we open up our curly braces and then in here is where you put the code that you want to be executed in this case i can put anything i want really anything that i could put an if statement i could put a mathematical operation but in this case we simply want to print out the numbers 1 to 10 and x is going to be our counter so so what we need to just type in printf and in here i would uh we need to specify the the type of data we're outputting so it's going to be in the form of a decimal and we're outputting the variable x all right so now if we print this out something is going to happen and this is where i actually need to uh move to the next line for every time this loop comes about so i'm going to use the n command so back slash n and that is going to move us to the next line every time this loop runs which is going to do about 10 times until the value is 10. all right so let me save this and we can just compile and run this so give that a few seconds and as you can see it printed out for us the numbers one two two to nine and of course that's because uh if you want to go from uh one to ten then i would start for example from zero here and if i just compile and run one more time you can see that now it goes all the way from 0 to 9 and again if you want to keep it to the entire value of 10 i'll keep this i can say x is less than so you can say x is less than or equal to 10 right over here and we can compile and run that one more time and as you can see it will go to the to the value of 10 right so that is how to use a for loop and let me just explain it one more time so the structure of a for loop is very simple now you can initialize the variable inside the for loop so i can say intex equals zero and i can get rid of that line of code and if i just compile and run this well it gives me the error and i'll explain why it does that now if i was to externally declare it we would have a different case so that's what i was trying to point out so x is equal to zero so the initialization happens within the for loop all right and that's primarily what i wanted to talk about so this would work but if i was to declare it uh globally something else would happen and i'll get to that one as we move along so that is the structure so initializing the variable uh here into x and then you start i mean sorry you declare it and then initialize the value here you set up the condition under which you want the uh the loop to run in this case we want the loop to run until the value of x is less than or equal to 10 and then we increment it you know by one every time this loop runs and it's storing the the value into into x every time and then uh you every time this the loop runs i want you to print this out i want you to print this statement out until the value of x is less than or equal to 10. and then after that the loop is closed and it essentially ends the entire program giving us the return over here if the script executed correctly which as you can see it does just over here sorry about that and there we are returned with a value of zero all right so that is how to essentially create a for loop and this can work in in various ways and at the end of the video i'll be showing an example we'll be in encapsulating everything we've learned so far and creating a very nice program that i'll show you how they'll really show you the power of loops all right let's move on to while loops all right now while loops are a little bit different in the way they work so a while loop is very simple to understand uh but in my mind is one of the most powerful ones apart from the for loop which also has its uses all right so let's talk about a while loop so let me just get rid of all of this here and we'll talk about a while loop so a while loop works really very simply as i mentioned the syntax is as follows you have while uh well and in here is where you have your condition which can be anything and under this is where you have your code that you want to execute it and uh of course you end it with a semicolon and that's essentially a while loop now again i'm going to give you a very simple example where i will print out uh i will print out the i'll print out one to ten with the while loop again and then i'll show you how to use loops uh more professionally or in a much better way all right so uh the first thing i want to do is again we have to give it uh we need to initialize the variable this time outside of the while loop you cannot remember we're only specifying the condition apologies for that guys my phone always seems to go off uh whenever i'm trying to make a video so apologies for that so as i mentioned the while loop does not contain any initialization and all is provided the only thing you provide in the parentheses is the condition itself all right so in this case i'm just going to create uh the same variable into x and this time i'm going to give it the value of one i'm going to initialize it right over there outside of the loop and then uh while we create our loop and then while and then inside our condition uh inside the the while loop is where we provide our condition similar to the for loop and then the increment can then be provided within the conditions here within the code that is to be executed that's the most important bit so this is the condition so we're going to say while x is less than or equal to 10 and then we open up our curly braces and this is where you put in the code that you want to be executed in this case we are simply going to do the same thing we did with the for loop and we are going to increment it by one every time it runs so we are just going to say printf and again we are specifying the type of data which is a decimal point here and x and then after this we need to increment it so you can increment it the same way we did by the typing in x is equal to x plus one or you can do it the other way which is much better which is x x plus plus which will essentially increment it by itself every time this loop runs all right and let me just return 0 here so we know everything worked perfectly and i'm going to save this right now and we're going to compile and run all right so let us wait for the results here fantastic so you can see that it printed out one two three four five six seven eight nine ten and we forgot to actually break the line here and go to the next line so let me just do that one more time compile and run and there we are one or it prints us it prints for us uh the numbers one to ten as follows now of course you can do this as many you you can go as long as you want i can say i want this loop to run you know until x is less than equal to 100 which means it will go all the way to 100 so if i compile and run you'll see it'll go it'll print out all the numbers from one to 100 and now is where you can see the real power of a loop now of course these examples are not doing uh the loops any justice in terms of their functionality and what they can offer so i'm going to create a simple script here that again will ask a user for their age and it will test it will test it against the legal drinking age and if you're over the age the legal drinking age it will print out please it'll print out you're old enough to drink similar to what we did in the previous video and then the interesting thing is we're going to incorporate a loop and functions and what's going to happen is where we're going to make sure that the script prompts the user uh as to whether or not they want to run the script again and if they do they enter an option and then it runs the script if they chose the correct option and if they didn't then it it ends the script right over there all right so i'm going to leave the standard libraries right over there and we're just going to create a main function ourselves this time and the main function in this case is only going to be used for function calls uh because we are not going to be doing anything uh extra in here so we're just going to leave the main function as it is so now we are creating our own functions now that are going to essentially uh this is going to encapsulate all that we've learned so far all right so let's create our first function here and the first function is going to be responsible for decision making all right so i'm just going to give it a return data type of integer so we're going to call it decision making uh we're not going to specify any parameters in there and we're going to open up our parentheses here we're going to provide a variable we're going to create a variable and we're going to call it int h let's call it intage instead of int a and we're now going to print f we're going to prompt the user to enter the age so we're going to say please enter your age all right and they're going to enter their age here and after they've entered the age we're going to scan f here and we are going to again provide the data type that we're inputting and store it to the variable uh we're going to store it to the variable h here all right so now we have the user's edge into the variable h now we need to use our conditional statements which are if statements to perform the test so again we're going to say if h is greater than or equal to is greater than or equal to 18 i want you to print out the following and we're going to say printf oops sorry print f uh you are old enough to drink all right so you are old enough to drink and we're gonna close that up and then we're gonna use the else statement right over here to give us another option in case the user enters a an edge that is younger all right so we're going to say printf print f right over here and we can say you are not old enough to drink all right and like that we have a simple test here that will test uh the value that the user inputs now the interesting thing comes in when we create our other function that will essentially provide us with a loop to essentially repeat this process if the user chooses to do so okay so let me just show you what i'm talking about so i'm going to create another loop i'm going to create another function sorry and i'm going to call it loop all right and of course you can name it whatever you want the whole purpose here is to understand how we're going to create a very interactive program here so um i'm going to create uh another variable called response all right so i'm just going to call it response i'm not going to give it any value because the user is going to input the value now i'm going to provide this script with a bit of a user interface so please bear with me if i this looks confusing so i'm just going to create a divider here so i'm just going to use these simple uh dashes here with the greater than and less than sign here and that's going to separate the content it's going to separate the test the data that's being displayed here from the actual loop and the decision making so i'm going to now prompt the user i'm going to ask the user whether he or she wants to run the script again so i'm going to say do you want to run the script again all right and this is a very simple question and i think i should actually break the line here so we're asking the user if they want to run the script again so now we're going to provide them with options all right so the options are going to be one option one is yes it's going to be yes and we're going to break the line here one more time sorry about that guys that is my bad uh you're gonna break the line to go to next line and we're gonna give the user another option and we're going to say uh two so the value two is going to be equal to no all right so if they select one it's going to be is going to be yes and if they select two it's going to be no so if they press one uh then i want this function to repeat one more time and to give the user the script again instead of us running it again over and over so now you're getting the real value of a script all right so now we need to accept the user input so i'm going to say scan f and in here is uh the value the data type that is going to be input is in the form of decimal and we are waiting for the response right over here so response which is an integer data type uh it it is essentially the data type of integer so i'm going to close that right over there so now they have input their response into the response variable now we can use the while loop all right so the while loop really applies very well here because i i was able to you know create a variable right over here and of course you can use a for loop if you uh if you so choose to and actually do recommend that you do that and let me know what you changed so so the script runs the way it does all right so i'm going to say while while we're going to say while response is equal to and these are your operators uh you know your logical operators here so while your response is equal to 1 which is yes well we can actually say uh yes while your response is equal to one i want you to run the following and this is where we call the uh the decision making function right over here so we're gonna say if or while the response is one i want you to do the following so i want you to run this function so i want you to run the decision making function so i call the function here decision making and like that it's going to execute now in the case that this is in the case that the user enters two then all we we want to do is it will close the loop anyhow and uh we just simply print out uh buy and thank you for using the program all right so thank you for using the program simple exit message that tells the user you know thank you for using the program and once that is done this should run now of course there is something you're missing here we need various function calls the first one that we need in the in the main function is we need to call the decision making function so that is the function that is going to run first and we'll call it right over here decision making um right over here so the the main function is going to call this function now this function is going to be the core of everything so after uh after we run our if statement or conditional statement we now want to call the loop function all right within the decision making function so we're going to say loop right over here and now let me explain what is happening before we actually test this program or this script all right so we've created our main function and we'll get to that in a second so we created two functions here the decision making function and the loop function each responsible for their own unique task or their own unique function so the decision making function is to ask the user for their age and depending on their response on on their on the edge it gives you an appropriate response all right it then calls the loop function to run so the loop function then asks the user whether they want to run the script again depending on the answer it then either it then calls the decision making function once again to repeat the script and again if they choose not to to run the script again it then gives us it then gives them this nice exit message here telling them by thank you for using the program all right so let me save that and we can compile this hopefully we don't have any errors and yeah we forgot this is the part of programming we forgot to actually have a semicolon there so i'm going to compile that and run it one more time and there we are we got zero errors and it's gonna ask me for my age so i can you know provide an age like 45 i'm gonna hit 45 it's going to tell me you're old enough to drink now i have an error here with my uh yes i forgot to actually break the lines here so let me just do that right now so i want to break the lines here so i'm going to say n and we'll run this one more time and we'll also include one after each of these statements so that in case uh in case you get any of them it prints out the it prints them out on the next line it prints out the the responses for the loop on the next line so let me just do that so we can have this nice and sorted so i'm going to compile and run all right so please enter your age so far so good i'm going to enter something like 32 it's going to tell me you're old enough to drink fantastic you can see it separates them right over here and now it's asking the user do you want to run the script again and again we get this a little mistake where it's not moving on to the next line for the user's response all right so to do that uh we simply just need to um give give it an uh we need to give it we need to break the line at no over here which is what i forgot to do uh sorry we need to use a backslash here i'm going to run it one more time compile and run all right so if our age is 45 yes now it's working perfectly so do you want to run the script again one is for yes and two is for no so let's say i hit one it's gonna give me the script one more time as i wanted so we know it's working so i can enter another age like 67 it's telling us you're old enough to drink do you want to run the script again yes i do let's say i'm 10 years old it's still it's going to give me the appropriate response you are not old enough to drink do you want to run the script again and this case in this case i'm going to hit no i do not want that want to run the script again bye thank you for using the program and for some reason it's telling us here uh it's actually giving us this error here by thanks for using the program and it runs uh it actually runs the script one more time and that's because we probably did not include a break after after it's all after we have called the decision making function now this of course can can be sorted really really easily by simply uh by using the break over here so if i just break that here and compile and run one more time and there we are so 45 and i hit no and there we are so yes uh so i forgot to tell you guys about breaking in in loops and in conditional statements or uh yes i will cover it anyway so a break essentially make sure you you end the loop after uh after we have called this function in our case so once you include the break uh command right over here this essentially ends the function abruptly or without without performing anything else so i'm just going to compile it around one more time so you can test it out for yourselves uh so please enter your age uh you can say 67 you're old enough to drink do you want to run the script again yes i do uh i'm 12 years old you're not old enough to drink do you want to run the script again i hit no and there we are bye thank you for using the program and for some reason yes because i'll get to why it's printing it twice and that's because we made our decision twice so that means every time the script is run and depending on that the number of times you use it it'll print out the exit message twice now this is an error that i will fix in the next video but i want to hear your response as to how you you should think i fix it so let me know what you guys think and of course i know what the issue is but i want to hear what you guys think so i do recommend that you try out these examples they really do help your understanding of uh you know the the various important bits in c programming uh and it really it really does go to show you how powerful this language is anyway guys thank you so much for watching this video if you like this video found value in it please leave a like down below if you have any questions or suggestions let me know in the comment section on my social networks or on my website and i'll be getting back to you guys in the next video peace getting back to you guys in the next video
Original Description
Hey guys! HackerSploit here back again with another video, in this video, I will be explaining how to use loops in the C programming language.
⭐Help Support HackerSploit by using the following links:
🔗 NordVPN: https://nordvpn.org/hacker
Use the link above or the code below for 77% Off your order
Promo Code: hacker
Patreon: http://patreon.com/hackersploit
I Hope you enjoy/enjoyed the video.
If you have any questions or suggestions feel free to ask them in the comments section or on my social networks.
🔗 HackerSploit Website: https://hsploit.com/
🔹 Support The Channel
NordVPN Affiliate Link: https://nordvpn.org/hacker
Patreon: http://patreon.com/hackersploit
🔹 Get Our Courses
Get a special discount on our courses:
The Complete Deep Web Course 2018:
https://www.udemy.com/the-complete-deep-web-course-2017/?couponCode=DWCBP2017
🔹 SOCIAL NETWORKS - Connect With Us!
-------------------------------
Facebook: https://www.facebook.com/HackerSploit/
Twitter: https://twitter.com/HackerSploit
Instagram: https://www.instagram.com/hackersploit/
Patreon: http://patreon.com/hackersploit
--------------------------------
Thanks for watching!
Благодаря за гледането
Kiitos katsomisesta
Danke fürs Zuschauen!
感谢您观看
Merci d'avoir regardé
Grazie per la visione
Gracias por ver
شكرا للمشاهدة
دیکھنے کے لیے شکریہ
देखने के लिए धन्यवाद
#Hacking#C#ExploitDevelopment
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from HackerSploit · HackerSploit · 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
How To Install Kali Linux 2.0 On Virtual Box
HackerSploit
100 Subscriber Q&A! - How I Learned Ethical Hacking
HackerSploit
BlackArch Linux Review - Better Than Kali Linux?
HackerSploit
How to Access the Deep Web Safely | Deep Web Starter Guide 1.0
HackerSploit
Wireshark Tutorial for Beginners - Installation
HackerSploit
Wireshark Tutorial for Beginners - Overview of the environment
HackerSploit
Wireshark Tutorial for Beginners - Capture options
HackerSploit
Wireshark Tutorial for Beginners - Filters
HackerSploit
Complete Ethical Hacking Course - Become a Hacker Today - #1 Hacking Terminology
HackerSploit
Complete Ethical Hacking Course #2 - Installing Kali Linux
HackerSploit
Parrot OS 3.5 Review | The Best Kali Linux Alternative
HackerSploit
Nmap Tutorial For Beginners - 1 - What is Nmap?
HackerSploit
Katoolin | How To Install Pentesting Tools On Any Linux Distro
HackerSploit
Nmap Tutorial For Beginners - 2 - Advanced Scanning
HackerSploit
Nmap Tutorial For Beginners - 3 - Aggressive Scanning
HackerSploit
Zenmap Tutorial For Beginners
HackerSploit
How To Setup Proxychains In Kali Linux - #1 - Stay Anonymous
HackerSploit
How To Setup Proxychains In Kali Linux - #2 - Change Your IP
HackerSploit
How To Change Mac Address In Kali Linux | Macchanger
HackerSploit
How To Setup And Use anonsurf On Kali Linux | Stay Anonymous
HackerSploit
Ubuntu 17.04 "Zesty Zapus" Review - Bye Unity
HackerSploit
VPN And DNS For Beginners | Kali Linux
HackerSploit
Tails OS Installation And Review - Access The Deep Web/Dark Net
HackerSploit
Steganography Tutorial - Hide Messages In Images
HackerSploit
The Lazy Script - Kali Linux 2017.1 - Automate Penetration Testing!
HackerSploit
Best Linux Distributions For Penetration Testing
HackerSploit
Netcat Tutorial - The Swiss Army Knife Of Networking - Reverse Shell
HackerSploit
Gaining Access - Web Server Hacking - Metasploitable - #1
HackerSploit
Web Server Hacking - FTP Backdoor Command Execution With Metasploit - #2
HackerSploit
How To Install Kali Linux On VMware - Complete Guide 2018
HackerSploit
Q&A #1 - Best Cyber-security Certifications?
HackerSploit
Terminator - Kali Linux - Multiple Terminals
HackerSploit
Shodan Search Engine Tutorial - Access Routers,Servers,Webcams + Install CLI
HackerSploit
Q&A #2 - Mr Robot?
HackerSploit
Metasploit Community Web GUI - Installation And Overview
HackerSploit
Linux Expl0rer - Forensics Toolbox - Installation & Configuration
HackerSploit
QuasarRAT - The Best Windows RAT? - Remote Administration Tool for Windows
HackerSploit
Metasploit For Beginners - #1 - The Basics - Modules, Exploits & Payloads
HackerSploit
Metasploit For Beginners - #2 - Understanding Metasploit Modules
HackerSploit
Kali Linux Quick Tips - #1 - Adding a non-root user
HackerSploit
Metasploit For Beginners - #3 - Information Gathering - Auxiliary Scanners
HackerSploit
Spectre Meltdown Vulnerability - How To Check Your System
HackerSploit
Metasploit For Beginners - #4 - Basic Exploitation
HackerSploit
ARP Spoofing With arpspoof - MITM
HackerSploit
WordPress Vulnerability Scanning With WPScan
HackerSploit
Generating A PHP Backdoor with weevely
HackerSploit
Nikto Web Vulnerability Scanner - Web Penetration Testing - #1
HackerSploit
How To Install Kali Linux On Windows 10 - Windows Subsystem For Linux
HackerSploit
Stacer - System Optimizer And Monitoring Tool For Linux
HackerSploit
Kali Linux 2018.1 - Kernel Updates & Patches
HackerSploit
MITM With Ettercap - ARP Poisoning
HackerSploit
Password Cracking With John The Ripper - RAR/ZIP & Linux Passwords
HackerSploit
How To Detect Rootkits On Kali Linux - chkrootkit & rkhunter
HackerSploit
Channel Updates - How To Post Questions & Video Suggestions
HackerSploit
Web App Penetration Testing - #1 - Setting Up Burp Suite
HackerSploit
Web App Penetration Testing - #2 - Spidering & DVWA
HackerSploit
Cl0neMast3r - GitHub Repository Cloning Tool
HackerSploit
Kali Linux On Windows 10 Official - WSL - Installation & Configuration
HackerSploit
DoS/DDoS Protection - How To Enable ICMP, UDP & TCP Flood Filtering
HackerSploit
Web App Penetration Testing - #3 - Brute Force With Burp Suite
HackerSploit
More on: Algorithm Basics
View skill →Related Reads
📰
📰
📰
📰
The Silent Killer in Your Node.js APIs: Mass Assignment & How to Catch It Before Production
Medium · AI
The Silent Killer in Your Node.js APIs: Mass Assignment & How to Catch It Before Production
Medium · Cybersecurity
Australian Cyber Security Centre Issues Alert on Mass Exploitation of CMS Vulnerabilities
Dev.to · NetSecOpsIO
Malicious 'jscrambler' NPM Package Versions Deploy Cross-Platform Infostealer in Sophisticated Supply Chain Attack
Dev.to · NetSecOpsIO
🎓
Tutor Explanation
DeepCamp AI