Learn to Code via Tutorials on Repl.it!

← Back to all posts
C++ Tutorial: Day 5!
HahaYes (902)

Day 5!

Bugs/Typos

Forgot semicolon. Thanks to @SpaceFire
Please upvote to help dominate the tutorial section.

REMEMBER. ALL FUNCTIONS MUST END WITH A SEMICOLON


Day 5!........... For Loops!

What in the world are For Loops?

Definition: For Loops are disigned to iterate a number of times.
Basically a template for a for loop looks like this...
for(initialization; condition; increase/decrease) statement; bla bla bla

So this would be a cool for loop for a SpaceX countdown.
Don't copy the code, try to write it yourself.

#include <iostream>
using namespace std;

int main()
{
	for (int n = 10; n>0; n--) {
		cout << n << ", ";

	}
	cout << "Liftoff!\n";

}

Initialization: Is executed, and sets it to some inital value.
Condition: This is checked, and if true, run, if false, end.
Statement: basically the statements enclosed in the curly brackets.
Increase: is executed and loops over again until false.


While Loops!

What are While Loops?

In a while loop, the condition is evauated first and if true, then the statement in sde the while loop will be executed. This will continue until the condition becomes false. When the condition is false, it comes out of the loop and goes to the next statement.
Syntax of a while loop:

while(conditon)
{
	statement(s);

}

A Example:

#include <iostream>
using namespace std;
int main()
{
	int i = 1;

	while(i <= 6){
		cout << "Value of variable i is: " << i << endl; i++;
	}
}

This will print out the numbers 1,2,3,4,5,6.

Ummmm that's it!

Challenges:
Build a launch sequence again,
but instead of it launching, make it abort.
Like this: 10,9,8,7,6,5,4,3, abort!
Using a while loop, make a infinite sequence.
Go study pointers, because the next one is going to be HARD

Hey everyone, all these competitors in the tutorials section must be wiped out.

Everyone upvote! (To dominate the tutorial section.)

Ummm featured shoutout.....
@TheForArkLD Always think the name is THeFORk

@Jakman A hardcore Rust programmer.

Commentshotnewtop
SpaceFire (82)

@HahaYes YEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE

HahaYes (902)

@SpaceFire SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS

lightningrock (47)

YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA @HahaYes

sean098 (0)

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY[email protected]HahaYes

HahaYes (902)

@TheForArkLD FFFFFF Upvote All my projects

TheForArkLD (607)

@HahaYes Ok later ( ͡° ͜ʖ ͡°)

johnstev111 (229)

@HahaYes NO SPAM PING ALLOWED OR I WILL HAHANO YOU AGAIN

HahaYes (902)

@johnstev111 what do you mean. You literally just 404 ed us

theangryepicbanana (1597)

@HahaYes Please try not to mass-ping users

HahaYes (902)

@theangryepicbanana I've stopped mass-pinging. Someone told me to stop before this. But thanks for the warning anyways. :)

Muffinlavania (669)

Licking noises
(There i commented)

Muffinlavania (669)

@Muffinlavania Also C++ more like C-RUST-RUST (please dont hate me)

Muffinlavania (669)

@HahaYes :D hahaaaaaaaaaaaaaaaa

yes

HahaYes (902)

@Muffinlavania hmmm the competition is getting insane

HahaYes (902)

@Muffinlavania But muffin and I must COME OUT ON TOP

Muffinlavania (669)

@HahaYes with who CodeLongAndPros? Cause i dont care boiiiiiiiiiii lol

Muffinlavania (669)

@HahaYes yay im not part of the dark side

HahaYes (902)

@Muffinlavania yes you are strong with the LIGHT

Muffinlavania (669)

@HahaYes yay now i will kill EVERYONE

HahaYes (902)

@Muffinlavania master skywalker, there are too many of them, what do we do?

Muffinlavania (669)

@HahaYes we dominate the tutorial section Soldier HAHAyes

HahaYes (902)

@Muffinlavania Anakin proceeds to kill younglings. I guess you didn't watch star wars

Muffinlavania (669)

@HahaYes no i did lol but i dont remember quotes WeiRDo

HahaYes (902)

@Muffinlavania didn't star wars squadrons come out?

Muffinlavania (669)

@HahaYes idk i just watched the main star wars stuf like the rise of skywalker and stuf

Muffinlavania (669)

@HahaYes yessir but im helping some guy do something, he said its for school? so yea, might not talk very much

HahaYes (902)

@Muffinlavania yeah, I'm looking at stupid traders at r/wallstreetbets it is like speedrunning for the stock market

Muffinlavania (669)

@HahaYes yea... i looked at your project and my brain died....

CodeLongAndPros (875)

@HahaYes Well, well, well. What do we have here?

HahaYes (902)

@CodeLongAndPros why is everyone attacking me

HahaYes (902)

@CodeLongAndPros Hey man, sorry to say this, but you guys are really getting agressive. I'm trying to be nice, but I'm starting to get threats from wuru

CodeLongAndPros (875)

@HahaYes I'm, at worst, passive-agressive.

I think that your 'we need to take over the tutorials section' is more aggressive. I just want my tutorial to reach more people.

HahaYes (902)

@CodeLongAndPros Hey CodeLong, to reply to your statement, I wasn't really talking to you in general. I was talking more about wuru, and his kinda agressive statements. (again just look at the comments) but sorry for the agressive stance in your opinion.

johnstev111 (229)

@HahaYes Because Wurur calls people buffoons

HahaYes (902)

@Muffinlavania Dang muffin, a 100% increase in cycles? Whatttt, (also 1/5 of your cycles come from a dead account but whatever)

Muffinlavania (669)

@HahaYes lol idk and im not joking i have no clue y,
ITs probably cause im a cool kidddddd

Muffinlavania (669)

@HahaYes lol
insert another stock photo of cool kid here

johnstev111 (229)

@HahaYes I don't mean to be a pedant but it's an example not A Example

HahaYes (902)

@johnstev111 hmm I don't use grammarly see, I don't want a keylogger

Muffinlavania (669)

@HahaYes @johnstev111 ya know you are commenting on my comment, can you like duke it out somewhere else

HahaYes (902)

@Muffinlavania lol it is like 11 pm but okay

johnstev111 (229)

@HahaYes Ok you use EST. I can determine because ET is 5 hours behind me in BST

johnstev111 (229)

@Muffinlavania Oh dear... Ok.Sorry for late reply, this comment was posted at 3am my time

HahaYes (902)

@johnstev111 yeah, me new yorka time

johnstev111 (229)

@HahaYes Funny that, I just sent a pull request to some of Dynamic Squid's code on github, who is in MST, ang you are in EST, And I'm in BST! Time zone madness?

Muffinlavania (669)

@johnstev111 Wait what time is it for you

Muffinlavania (669)

@HahaYes me british :D jk i know hes british
also

them canadians ~ HAHAYES

Muffinlavania (669)

@johnstev111 oh its 8:50 Pm for me, so its 7 hours? idk
btw i have HAHAYES's time

AshishSarkar (5)

How to upvote? I am new man. I just use repl.it because I don't like to install an IDE @HahaYes

johnstev111 (229)

@AshishSarkar You click the button with the up arrw.

Wuru (64)

[ link redacted by moderators for advertising ]

Wuru (64)

Better than C++: [ link redacted by moderators for advertising ] Also a lot more in depth than this tutorial. :D

HahaYes (902)

@Wuru considering every single competitior is trying to "shake up" my monopoly in the tutorial section, nice try.

Wuru (64)

Youll see with your dirty *ss C++ @HahaYes

Wuru (64)

Nobody wants short *ss tutorials with no good explanation @HahaYes

HahaYes (902)

@Wuru sorry man, the monopoly is strong with me.

Jakman (324)

@Wuru lol his comrade is a Rust chad. It would be really bad for you if he helped him with his tutorials.

Jakman (324)

@Wuru any way @HahaYes is hustling with Cpp in a python world.

Wuru (64)

Yeah, personally I use Go more often than python and C++ but on repl.it i use python more. Though Java and Go are my most prominent languages. @Jakman

Wuru (64)

Personally Rust is fine, I just don't like the Syntax. @Jakman

Jakman (324)

@Wuru for you to call another ones projects and content ass is very disrespectful.

Wuru (64)

Look, we resolved it. Don't get all defensive for your friend without even looking into it. @Jakman

Jakman (324)

@Wuru cool. Just dont do that man. It aint cool.

Kognise (403)

@Wuru Hey, please try to be kind and respectful to fellow replers!

Kognise (403)

@Wuru Hey, please try to be kind and respectful to fellow replers! (again)

Wuru (64)

I know. We resolved it. It just got heated. We are good now :D @Kognise

Kognise (403)

@Wuru No problem, just going through the reports and doing my job as a mod :D

HahaYes (902)

@Kognise we resolved it :) we are good now :)

johnstev111 (229)

@Wuru You don't need to be so RUDE

HahaYes (902)

@Jakman Thanks for the compliment

HahaYes is hustling with Cpp in a python world

SpaceFire (82)

I swear we did while loops on day 3....

HahaYes (902)

@SpaceFire yeah but I didn't really go into detail

SpaceFire (82)

@HahaYes i like the challenge thing tho

HahaYes (902)

@SpaceFire yeah thanks, share this with anyone that you can. I am competing with CodeLongAndPros, and of course I must win.

HahaYes (902)

@CodeLongAndPros Hello! This is customer support! How is your day!

HahaYes (902)

@SpaceFire SHARE IT WITH EVERYONE

CodeLongAndPros (875)

@HahaYes Hello, I can't find the 'any' key on my keyboard...

HahaYes (902)

@CodeLongAndPros hmmmm keyboard not found... press aany key to continue

CodeLongAndPros (875)

@HahaYes It also says:

User not responding. Error ID-10-T. Please replace user.

CodeLongAndPros (875)

Markdown syntax highlighting is a thing:
```{language_name}

```

HahaYes (902)

@CodeLongAndPros we're obviously not gonna upvote each other's projects

CodeLongAndPros (875)

@HahaYes

#include <iostream>
using namespace std;

int main()
{
	for (int n = 10; n>0; n--) {
		cout << n << ", ";

	}
	cout << "Liftoff!\n";

}

I fixed it.

CodeLongAndPros (875)

@HahaYes You have to do
```c++
#include <iostream>
using namespace std;

int main()
{
for (int n = 10; n>0; n--) {
cout << n << ", ";

}
cout << "Liftoff!\n";

}
```

HahaYes (902)

@CodeLongAndPros oh thanks, hmmm rivalries helping each other? wattttt

CodeLongAndPros (875)

@HahaYes It's not the formatting, it's the content.

I prefer to give all the information they need for a task, over going though the wiki.

HahaYes (902)

@CodeLongAndPros Yes, good content is awesome. Competition is awesome. Someone always loses

CodeLongAndPros (875)

@HahaYes You'll lose.

You didn't even explain how if works, just spoon-fed them.

Jakman (324)

That's a good tutorial.

HahaYes (902)

@Jakman hmmm we should ask @amasad who is better. The C wars vs the C++ Side

Jakman (324)

@HahaYes Well C obviously... It gives you less to worry about.

HahaYes (902)

@Jakman hmmm the rich library. Allows exception handling and function overloading

Jakman (324)

@HahaYes C. Better memory management. Allocation is better. 32 keywords. No namespaces.

HahaYes (902)

@Jakman 32 keywords vs like 64. More to memorise is good. as memory management goes I can't see the difference.

HahaYes (902)

@Jakman Hmmm c++ also has OOP

Jakman (324)

@HahaYes OOP in C is possible. You just have to work around some issues

Jakman (324)

@HahaYes therefore C is good. And better than Cpp

HahaYes (902)

@Jakman but see, more complicated

AmazingMech2418 (655)

@Jakman @HahaYes C++ is easier to use than C, but C has better memory management. Though, C++ is object-oriented and you can make a library in C++ much easier than in C.

HahaYes (902)

@AmazingMech2418 C is good for buiding a OS, C++ is better for pretty much anything else

AmazingMech2418 (655)

@HahaYes Yeah. Pretty much! Though, C is good for a lot of things C++ is good for too, although C++ is better in those cases (C's memory allocation system can cause a lot of bugs...).

HahaYes (902)

@AmazingMech2418 yes, I like a safer programming language like C++

AmazingMech2418 (655)

@HahaYes Though, Rust > C++. (although, it is more difficult to take user inputs in Rust...)

AmazingMech2418 (655)

@HahaYes I like both C++ and Rust, but Rust is better.

HahaYes (902)

@AmazingMech2418 I prefer C++ because of competitive programming

AmazingMech2418 (655)

@HahaYes Yeah, but there's a reason a lot of applications are switching to Rust. Rust's memory system fixes a lot of C++ memory bugs.

Jakman (324)

@AmazingMech2418 Cpp programmers would be better off if they followed Rust memory guidelines

HahaYes (902)

@Jakman nah cpp is faster than RUST

Jakman (324)

@HahaYes lol no. Rust is from ASM. Not from C. And it is faster than C.

HahaYes (902)

@Jakman C++ is almost always faster than Rust

Jakman (324)

@HahaYes nah. I think bias has got you twisted.

HahaYes (902)

@Jakman Rust is a young language, and the older the language = better the language

Jakman (324)

@HahaYes Not all the time. Fortran is old but you dont see it everywhere. Noone writes compilers in binary. We arent using turing machines.

AmazingMech2418 (655)

@Jakman Well, Fortran actually still is used in many cases due to it being a lot faster than C. Though, it is being phased out gradually as newer, better programming languages emerge (like Rust).

CubeyTheCube (7)

@AmazingMech2418 What’s Rust? If it’s better than C++ why haven’t I heard of it?

HahaYes (902)

@CubeyTheCube ummm rust is like c++ but with better memory managment. Also c++ > rust

Jakman (324)

@HahaYes Rust > cpp. Rust chads dominate society

HahaYes (902)

@Jakman sorry man, rust < C++

HahaYes (902)

@Jakman sorry man, I prefer SPEEEEEEEEEEEED over ###### safety

Jakman (324)

@HahaYes With rust you Can have BOOOOTHHH

Jakman (324)

@HahaYes If you use Rust pointer rules. Cpp dominates rust. But you guys tend to have several mutable pointers.

AmazingMech2418 (655)

@HahaYes They both compile to machine code. Neither is actually faster. Actually, I'd say that Rust is faster since it has immutability by default (and, therefore, can inline most variables). @CubeyTheCube Rust is a relatively new programming language.

Jakman (324)

@johnstev111 Get that Free GC out my face bruh.

johnstev111 (229)

@Jakman C# and python are better than all other languages. You can't change my mind

Jakman (324)

@johnstev111 What makes them better in your eyes?

Jakman (324)

@johnstev111 so you only like them because they hold your hand through everything?

Jakman (324)

C holds your hand too. Rust is made for men and so is Cpp. Are you not a real man @johnstev111 ? How is a simple language the best? Scratch is not anywhere other than its own website.

HahaYes (902)

@johnstev111 nah nah c# and java both suck

Jakman (324)

@johnstev111 That just gave me a stroke.

HahaYes (902)

@johnstev111 Why isn't it? I can use C++ for Game Dev.

HahaYes (902)

@Jakman same. C++ is so much better than C#

johnstev111 (229)

@HahaYes You can use what you want.

Jakman (324)

@johnstev111 lol. I bet you hate static typing and had multi-brain failure when you saw C the first time.

HahaYes (902)

@johnstev111 lemme guess. You chose C# for that dynamic programming

Jakman (324)

@HahaYes lol. Thats why he loves python as well.

johnstev111 (229)

@Jakman Like no. I'm saying Java is horrid, and I avoid when necessary. C# is like Java, but not AS horrid.

HahaYes (902)

@Jakman oh these python kiddos.

johnstev111 (229)

@HahaYes The only thing I disagree with in that statement is the first greater than symbol should be reflected along the x-axis.

Jakman (324)

@HahaYes I knew he was a python kid when he said they were better because of the simplicity.

HahaYes (902)

@Jakman Simple = Slow C and C++ are elegant

Jakman (324)

@HahaYes i know. He doesnt want to play with the chads anymore.

johnstev111 (229)

@HahaYes It was an accident, I sent an empty message, but it went through, and 404ed you

Jakman (324)

@HahaYes we use languages with pointers and concepts that python minds can't comprehend.

johnstev111 (229)

@Jakman I didn't 404 you purposely, I found a bug in repl.it , so to speak.

johnstev111 (229)

@Jakman HaHaYes doesn't think so.

johnstev111 (229)

@Jakman Don't talk behind my back either

Jakman (324)

@johnstev111 bro the messages are public. how am i going to hide. One thing i dont do is fake things.

johnstev111 (229)

@Jakman @HahaYes C# has static typing... silly

Wilke000 (345)

NEVER @HahaYes! I was going to do this! BUT NON NO NO NO, C++ IS HARD

HahaYes (902)

@IndyCarter C++ is better than python

Wilke000 (345)

booooooo @HahaYes spotify, google, amazon, and other ginorma companies use python!!!!

HahaYes (902)

@IndyCarter microsoft uses c++, google uses c++, amazon uses c++

Wilke000 (345)

Instagram, facebook, Netfix, Uber, Dropbox, Pinterest, Instacart @HahaYes uses python

HahaYes (902)

@IndyCarter also c++ faster than python. Python is for data science.

HahaYes (902)

hmmm day 6 about to come

lightningrock (47)

whats the difrence between for & while loops?

HahaYes (902)

@lightningrock While both for and while are entry-control loops used to execute block(s) of code repeatedly certain number of times, they differ in functionality. The for loop is quite similar to the while loop in terms of memory consumption and speed. However, the for loop is preferable when you know exactly the number of times the loop has to be repeated. On the contrary, while loop is appropriate when the exact number of iterations is not known, meaning you do not know how many times the loop has to be repeated.

lightningrock (47)

hm... so like the python version?
@HahaYes

HahaYes (902)

@lightningrock feel free to upvote(I think you already have) and share this with everyone!

HahaYes (902)

@lightningrock gud now time to make day 6