C wars: Chapter Three: Revenge of the if
Chapter three: Revenge of the if
Today I will be talking about booleans
and if
, else if
, else
constructs.
First, before we get to the if
, I'd like you to meet a new data type:
The boolean!
This is a type that has two possible values: 1 or 0.
Or:
true
false
To talk to your friend the bool, you have to #include
another file: stdbool.h
Can you guess what this is for?
It is the Standard boolean header
You can use the true
and false
keywords to set the value of a bool:
Examples:
bool key_pressed = true;
bool uses_windows = false;
But what use is that?
Let me introduce you to bool
's partner, the comparision operators.
There are 9:
==
: equals!=
: not-equals>
: greater-than<
: less-than<=
: less-than-or-equals>=
: greater-than-or-equals
"But wait", you say, "You can't count! That's 6!"
Well, yes, but actually no.
There are three more:
!
: not.
This one makes true
be false
, and false
be true
.
So to toggle boola
, you can do boola = !boola
.
&&
: and.
This one is true if boola
and boolb
are both true:
boola && boolb
||
: or.
This one is true if boola
or boolb
is true
Note: you can't compare char*
with ==
The if
Now, it's time for the big reveal.
How do I control my programs?
You use the if
construct.
The syntax for if is:
if (boolean) {
foo
}
The code in between {}
will run if boolean
is true
.
So:
if (1==1) {
x+=1;
}
You can use the else if
to run code if bool1
is false, but bool2
is true:
if (bool1) {
foo()
} else if (bool2) {
bar()
}
And finally, you can use else
to run if any of the other if
's are false:
if (bool1) {
foo()
} else if (bool2) {
bar()
} else {
spam && eggs;
}
There is one more syntax for an if
statment.
This one only works on one statment:
if (a)
foo();
else if (b)
bar();
else
spam && eggs;
One more thing:
You need to learn how to compare strings.
You use strcmp(a, b)
to do so.
strcmp
is in string.h
.
To compare char a and char b:
#include<string.h>
char* a = "Hello";
char* b = "Hello";
bool a_is_b = !strcmp(a, b);
Note that if a == b, strcmp
returns 0, hence the use of not.
Your homework:
Make a program that asks the user for their name.
If their name is Steve jobs
, print You own apple.
If their name is Bill gates
, print You own MS.
.
If their name is Richard stallman
, print You founded GNU
.
Otherwise, print I don't know you.
So long!
His life story:
if (person.name == "Steve Jobs")
{
kickOutOfCompany(person);
}
if (doesntWorkThereAnymore(person))
{
comeBackToTheCompany(person);
}
if (cameBackToTheCompany(person))
{
kickOutOfCompany(person);
}
Ok but that's not C @DynamicSquid
@Raniconduh fixed it
Lmao now it is @DynamicSquid
@Raniconduh forgot c didn't have classes lol
I think that's why I like C so much. If my program needs classes, I would probably make it in Java. @DynamicSquid
@Raniconduh yeah, i heard many developers actually prefer C over C++ since C++ has too many features. personally, I like it that way, but I do see your point
@DynamicSquid Mine? Or Jobs?
@CodeLongAndPros steve jobs
@DynamicSquid Yeah.
And BillG:
if (gates_not_richest()) {
sell_more_junk();
else if (linux_winning()) {
fud_linux();
else {
make_junk();
}
@DynamicSquid
Linux be like:
@DynamicSquid After reading your bio, I feel bad for you. So little school left.
@CodeLongAndPros well, I actually kinda miss school, but actually I now have 3 more days left thanks for reminding me :)
@DynamicSquid Sure.
Even virtual school is better than wasting away...
@CodeLongAndPros well I mean like, I actually don't think school helps me. well I know it's important since I want to get into a good university so I have to have good grades and stuff, but I want to pursue a coding career, and literally 99% of things I learn at school aren't related to code..
@DynamicSquid I'm also planning on having a coding career, but I think that the acquisition and assimilation of knowledge is always a good thing.
@CodeLongAndPros yeah, I see your point, but is EuRoPeAn ImPeRiAlIsTiC iMpAcTs On 19Th CeNtUrY CoLoNiAl InDia really gonna help write better code?
EuRoPeAn ImPeRiAlIsTiC iMpAcTs On 19Th CeNtUrY CoLoNiAl InDia
To quote one of my teachers:
In public school, you get a broad overview, so you have the foundation you need, and then you can chose where you go from there.
@DynamicSquid But my school has a tech apps
class.
It used to be HTML/JS/CSS
And a bit of Adobe and Office.
@CodeLongAndPros my teachers say that too, that getting a broad overview of subject is good, and I guess that's true, but like, EH...
wait, does your school have a programming class? or is tech apps the only class that involves computers?
@DynamicSquid No programming. We do have a Coding Club, but it's non-offical and was ran by me.
@DynamicSquid Actually, I had to help with a problem during a DLA. Good times 😅
@CodeLongAndPros oh... that sucks, no coding courses. hey, at least you have your own club, so well done on that!
my school does have a programming course, but in 8 months, this is what we learned:
- variables
- if statements
- a little bit of functions
and no one in the class can fluently code in any of these (maybe some people using variables)
8 months!!!
A self taught learner could learn all that in a like a couple weeks!
But to be fair, this is only our first year of our programming course.
Also what's a DLA?
@DynamicSquid I covered most of the stuff you did in my C tutorials. In three days.
A DLA is a District Learning Assement.
Local stdtest.
@CodeLongAndPros oh that's what it is lol
@DynamicSquid Super secure test.
Can't go into much detail, but there was an audio copy.
They had to scan a qr to get to it.
Opened in google drive and made a copy.
Thing is, people didn't sign out of the iPads.
My friend had a copy.
Had to sign out of all the iPads.
@DynamicSquid Major facepalm.
@DynamicSquid It's 3 weeks for me..
@johnstev111 3 weeks?? Aw... but when does school start for you?
@DynamicSquid Start? 4th September
@johnstev111 aww really? I end school in 2 days but it also starts in the beginning of september...
@DynamicSquid mine finishes 10th july
@DynamicSquid Mine got out in May, but begins in August.
Super hot in August, too.
@CodeLongAndPros and mosquitos
@DynamicSquid Yeah. Texas summers == !good.
@CodeLongAndPros oh you live in Texas? does it snow there? I'm in Canada and it snows for like 8 months
@DynamicSquid You have to use strcmp
...
@DynamicSquid In my lifetime, it's snowed 2.
3 if you count a 10 min flurry.
And, yes, we ride our horses to school.
@DynamicSquid Yes. 13 years and two snow storms, one didn't even last a day.
@CodeLongAndPros damn, once snowed 30+ centimetres overnight and school wasn't even cancelled, day just went on as normal lol
@DynamicSquid actually no, that's a lie, not once, all the time
@DynamicSquid We have floods.
2 weeks off because of a hurricane
1 week off because of the Tax Day floods.
@CodeLongAndPros oh, that's pretty bad. we never have floods. is it true you guys celebrate Christmas with an outdoor sunny barbecue?
@DynamicSquid No. It gets cold here, but never below 40F on normal days.
@CodeLongAndPros what? we have like -30C (-22F) regularly in winter
@DynamicSquid This month, we had 100F+ (37C) temps
@codelongAndpros can you tutor me coding from scratch.
@Power2311 Tutor, no.
You can read the series of tutorials, and comment if you have a question.
@CodeLongAndPros Should I use manjaro or kde plasma?
@HahaYes Manjaro kde
@CodeLongAndPros okay, hmmm kde is more powerful than xfce right?
@CodeLongAndPros hmmm what about kde plasma
@HahaYes Not a distro
Did you mean Kde neon?
@CodeLongAndPros yeah yeah
@CodeLongAndPros no wait there is manjaro kde plasma
@CodeLongAndPros Yes! These are finally gaining popularity
@CodeLongAndPros yeah, now you have over 800 cycles yay
@HahaYes yay -S 100_cycles
#include <stdbool.h>
But I do like your syntax/template of your tutorials
You SHALL NOT WIN, YOU CANNOT DISRUPT THE NOOB MARKET
@HahaYes Excuse me?
I didn't hear you.
@CodeLongAndPros Let's put it this way. If you are linus torvalds, then I'm Bill Gates, I am better known. I AM THE MEME OF THIS COMMUNITY
@CodeLongAndPros Hey CodeLong, don't you use Manjaro?
@HahaYes Why, yes, yes I do.
@CodeLongAndPros also, I'm still up at the top, I'm not exactly losing market share, also, Manjaro is debian based right?
@HahaYes No, it's Arch based. Ubuntu is Debain based.
@CodeLongAndPros hmmmm so terminal will have the same syntax though right?
@HahaYes Only diff is instead of apt
, it's pacman
:
apt install: pacman -S
apt update: pacman -Sy
apt upgrade: pacan -Su
apt update && apt upgrade: pacan -Syu
apt search: pacman -Ss
apt show: pacman -Qi
@CodeLongAndPros oh ok that is cool
@HahaYes And it's so much faster.
Installing gdc took 2 seconds.
@CodeLongAndPros hmmm manjaro faster than ubuntu?
@CodeLongAndPros I've used pacman but still by far prefer dnf
@CodeLongAndPros way too similar to C#. Have a look at my homework in C#!
usrname = Console.ReadLine();
switch (usrname) {
case "Steve Jobs":
Console.WriteLine("You own Apple."); break;
case "Bill Gates":
Console.WriteLine("You own Microsoft."); break;
case "Richard Stallman":
Console.WriteLine("You founded GNU."); break;
default:
Console.WriteLine("I don't know you."); break;
}
Similarities?
@johnstev111 Good guess, but actually, no.
@CodeLongAndPros You want me to do if in a lengthy if then?
It's the same syntax as C
@johnstev111 Switch-case works in C.
The only diff is:scanf("%s", &usrname);
@CodeLongAndPros I know it works, but the homework said to do it in an if-else, and scanf("%s", &usrname);
doesn't look clean at all. And forgot to set the datatype of the variable usrname. line 5 should've been string usrname = Console.ReadLine();
instead
@johnstev111 Well, C != C#
@CodeLongAndPros I know, but C syntax is the output if this:
decimal dividend = Decimal.One; // c# syntax
decimal divisor = 3;
Console.WriteLine(dividend/divisor * divisor); // c syntax
(it
Hmmm C++ > C
@CodeLongAndPros Did you just rate C# the worst? ARREST THIS MAN
@HahaYes C# > everything else
@johnstev111 C++ == C# I like both because of game dev
@HahaYes Well, I didn't particularly like C++... you should see my first (and last) C++ game, it's horrendous (It's been rewritten in C# as well)
@HahaYes it's just a simple read-evaluate-print-loop
@johnstev111 I like C++ for competitive programming
@HahaYes take a look at this beast that I wrote 2 years back
https://repl.it/@johnstev111/PTerm
@HahaYes That program's awful right
hmmmmm still not as popular as my series
@HahaYes Well, you've gotten traction
@CodeLongAndPros wdym
@HahaYes You've established yourself in the
market
. I'm trying to disrupt that. Not easy.But I think mine are rather well thought out
@CodeLongAndPros hmmm I'm google you are the startup
@HahaYes Im DDG
@CodeLongAndPros ummm the rap artist?
@HahaYes DuckDuckGo
@HahaYes As compared to Google.
@CodeLongAndPros Oh, why not FirefoX?
@HahaYes FF not a search engine.
@CodeLongAndPros Oh, I thought they designed their own
@HahaYes Mozilla, perhaps. But Firefox is a browser.