Learn to Code via Tutorials on Repl.it!

← Back to all posts
C++ Tutorial: Day 3
h
HahaYes (1286)

EDIT: Day 4 Is TOTALLY not DElaYeD

Day 3: Conditionals and Headers

Thank you to.....

IF SOMEONE WOULD HELP ME EXPLAIN EVERY SINGLE HEADER PLEASE CONTACT ME IN THE COMMENTS BELOW

@DynamicSquid for header emphasize

@awesome10 wanted conditionals.


A emphasize on Headers

I realized I never really went in deep with headers in C++, so here is a bunch of headers that you need or want.

Literally just use #include <bits/stdc++.h> to avoid the hassle. It has every single header that you need all in one.

Day 3!

Conditionals

What are conditionals?

Conditionals are expressions that evaulate to either true or false. They are usually used for if, else, and while loops.

If Statements

If statements are used to execute code only if the condition is true.
If the statement is false, the expression is just ignored, and the program will continue to run.

Example:

int x = 8;
int y = 5
if (x < y) {
	cout << "x might be greater than y";
}

Else Statements

Else statements are the opposite of a if statement. Else statements will only be runned(ran? someone tell me what it is.) if the condition is FALSE

Example:

int x = 5;
int y = 8;
if (x < y) {
	cout << "X is greater than y." << endl;

} else{
	cout << "Y is greater than X." << endl;

}

Explanation: y is greater than x, so that must mean that the if statement is false. Therefore, the program would move on to the else condition and print "Y is greater than X"
(Do you guys need else if?)

While Loop Statements

Loops (can) execute a block of code as long there is a condition that is specified.
Loops are awesome because they make your code look cleaner. (my code is not clean btw)

While loops only loop through a block o code as long as a specified condition is true:
Example:

int i = 0;
while (i < 10) {
	cout << i << "\n";
	i++;

}

This program will loop over and over again as long as the variable i is less than 10.

That's it!

Upvote?

Thanks for 400 cycles guys

Credits:
w3schools.com
sololearn.com

Featured Shoutout....
@Muffinlavania

Featured Shoutout....
@DyanamicSquid for all my shoutouts.

If there are any bugs or typos, please feel free to tell me.

Honk Honk YES! @DannyIsCoding (totally not stolen from me)
Comments
hotnewtop
Muffinlavania (1421)

Ayy idk why i get a shoutout but we take those :D

DannyIsCoding (690)

I really like this tutorials 👍
HeHeHooHooHonkJongBonkTronkBeepBoop YES! (Too far? maybe...)

HahaYes (1286)

@DannyIsCoding Your quote will be in the next series

HahaYes (1286)

@DannyIsCoding lol share this with others so it gets lots of views

CodeLongAndPros (1535)

Also, I think that #include<bits/stdc++.h> is bad practice. It makes your binary's fat, and abstracts what you need.

HahaYes (1286)

@CodeLongAndPros alright, I'm a competitive programmer, so it is a habit of mine

DungeonMaster00 (165)

Header Files

One way to make your code more organized and neat.

Header files are as cool and handy as heck.

A header file is a C++ file which you can use import in a header

A C++ header file's file extention is .hpp.

Make a file with the .hpp file extension and write your C++ code in there.

To use your C++ header file use #include "headerfilename.hpp", of course, replacing headerfilename with the name of your header file.

Voila! Now you know what header files are and how to use them.

BTW there are also C header files. In case you were wondering.

HahaYes (1286)

@nk1rwc yes, will continue this series :)

SpaceFire (116)

Day1: 10 days ago
Day2: 9 days ago
Day3: 5 days ago

WE DEMAND A DAY FOUR

HahaYes (1286)

@SpaceFire brb I'm sick......... I'm trying man

SpaceFire (116)

@HahaYes oh... i'm sorry bro

now i just feel bad

HahaYes (1286)

@SpaceFire hmmmm hope I don't get sicko sicko

HahaYes (1286)

@SpaceFire hope I don't get sick from that

HahaYes (1286)

@SpaceFire ummmm sure.... hmmm looking forward to hire people for day 5

SpaceFire (116)

@HahaYes # PLEASE TAKE ME WITH YOU

HahaYes (1286)

@SpaceFire # Coming VERY VERY SOON

SpaceFire (116)

@HahaYes # bringeth the knoweldge bestowed upon thy noggin

oignons (313)

You said to use <bits/stdc++/h>, but I would suggest not to, as it makes it more readable to lower-level people to see where each function and class is coming from. Just a suggestion.

HahaYes (1286)

@ipastrano yeah, I will put that later

HahaYes (1286)

Thanks for 500 cycles guys!

SpaceFire (116)

can't wait for day 4

DynamicSquid (4535)

Literally just use #include <bits/stdc++.h> to avoid the hassle. It has every single header that you need all in one.

Don't. It's terrible. It's mich better to just use each individual one

HahaYes (1286)

@DynamicSquid lol I'm a competitive programmer, it is just a habit of mine

SpaceFire (116)

Do you feel proud of yourself for becoming replit's inside meme?

HahaYes (1286)

@SpaceFire Wait I'm a meme now? I feel awesome

SpaceFire (116)

i did a code, are you proud of me? lol my fantastic code

HahaYes (1286)

What should I do for the next one?

Muffinlavania (1421)

@HahaYes check your cycles, your pretyyyyyy popular

CodeLongAndPros (1535)

@HahaYes You should do namespace and class

SpaceFire (116)

@HahaYes do they have for loops down by these C++ parts

SpaceFire (116)

@HahaYes in python and javascript we use for loops. does c++ have them?

HahaYes (1286)

@CodeLongAndPros yeah....but that is oop and that is a little harder

HahaYes (1286)

@SpaceFire yeah you want that in next tutorial?

HahaYes (1286)

@Muffinlavania but how.... someone cycle farmed?

SpaceFire (116)

@HahaYes that would be nice because we would have completed the two types of loops of loops. Also, do we have elif/else if

HahaYes (1286)

@CodeLongAndPros is class a little bit too advanced?

oignons (313)

@HahaYes For loops. If else. For the if else, explain all the relational operators and the AND, OR, and NOT stuff. I don't think you should do classes yet, wait until your readers have more of the basic stuff down. Oh and also maybe switch in the next one, or the one after.

HahaYes (1286)

@ipastrano do you want to help me with it?