C++ Tutorial: Day 5!
h
HahaYes (1052)

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.

You are viewing a single comment. View All
Jakman (378)

That's a good tutorial.

HahaYes (1052)

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

Jakman (378)

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

HahaYes (1052)

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

Jakman (378)

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

HahaYes (1052)

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

HahaYes (1052)

@Jakman Hmmm c++ also has OOP

Jakman (378)

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

Jakman (378)

@HahaYes therefore C is good. And better than Cpp

HahaYes (1052)

@Jakman but see, more complicated

AmazingMech2418 (693)

@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 (1052)

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

AmazingMech2418 (693)

@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 (1052)

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

AmazingMech2418 (693)

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

AmazingMech2418 (693)

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

HahaYes (1052)

@AmazingMech2418 I prefer C++ because of competitive programming

AmazingMech2418 (693)

@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 (378)

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

HahaYes (1052)

@Jakman nah cpp is faster than RUST

Jakman (378)

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

HahaYes (1052)

@Jakman C++ is almost always faster than Rust

Jakman (378)

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

HahaYes (1052)

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

Jakman (378)

@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 (693)

@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 (1052)

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

Jakman (378)

@HahaYes Rust > cpp. Rust chads dominate society

HahaYes (1052)

@Jakman sorry man, rust < C++

HahaYes (1052)

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

Jakman (378)

@HahaYes With rust you Can have BOOOOTHHH

Jakman (378)

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

AmazingMech2418 (693)

@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 (378)

@johnstev111 Get that Free GC out my face bruh.

firefish (308)

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

Jakman (378)

@johnstev111 What makes them better in your eyes?

Jakman (378)

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

Jakman (378)

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 (1052)

@johnstev111 nah nah c# and java both suck

firefish (308)

@HahaYes Java sucks, C# is good

Jakman (378)

@johnstev111 That just gave me a stroke.

HahaYes (1052)

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

HahaYes (1052)

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

firefish (308)

@HahaYes You can use what you want.

Jakman (378)

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

HahaYes (1052)

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

Jakman (378)

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

firefish (308)

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

HahaYes (1052)

@Jakman oh these python kiddos.

firefish (308)

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

Jakman (378)

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

HahaYes (1052)

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

Jakman (378)

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

firefish (308)

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

Jakman (378)

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

firefish (308)

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

firefish (308)

@Jakman HaHaYes doesn't think so.

firefish (308)

@Jakman Don't talk behind my back either

Jakman (378)

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

firefish (308)

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