Skip to content
← Back to Community
C wars: Chapter Three: Revenge of the if
Profile icon
CodeLongAndPros

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!

Part One: The Segfault menace

Part Two: The data wars

Part Three: Revenge of the if

Part Four: A new loop

Part Five: The Empire points back

Part Six: Return of the function

Voters
Profile icon
elcorreodeangel
Profile icon
OakWood1
Profile icon
programmeruser
Profile icon
DominicHuieWhit
Profile icon
EpicGamer007
Profile icon
awesome10
Profile icon
Jamendez7
Profile icon
TheForArkLD
Profile icon
HahaYes
Profile icon
firefish
Comments
hotnewtop
Profile icon
HahaYes

hmmmmm still not as popular as my series

Profile icon
CodeLongAndPros

@HahaYes Well, you've gotten traction

Profile icon
HahaYes
Profile icon
CodeLongAndPros

@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

Profile icon
HahaYes

@CodeLongAndPros hmmm I'm google you are the startup

Profile icon
CodeLongAndPros

@HahaYes Im DDG

Profile icon
HahaYes

@CodeLongAndPros ummm the rap artist?

Profile icon
CodeLongAndPros

@HahaYes DuckDuckGo

Profile icon
CodeLongAndPros

@HahaYes As compared to Google.

Profile icon
HahaYes

@CodeLongAndPros Oh, why not FirefoX?

Profile icon
CodeLongAndPros

@HahaYes FF not a search engine.

Profile icon
HahaYes

@CodeLongAndPros Oh, I thought they designed their own

Profile icon
CodeLongAndPros

@HahaYes Mozilla, perhaps. But Firefox is a browser.

Profile icon
DynamicSquid

His life story:

if (person.name == "Steve Jobs") { kickOutOfCompany(person); } if (doesntWorkThereAnymore(person)) { comeBackToTheCompany(person); } if (cameBackToTheCompany(person)) { kickOutOfCompany(person); }
Profile icon
Raniconduh

Ok but that's not C @DynamicSquid

Profile icon
DynamicSquid

@Raniconduh fixed it

Profile icon
Raniconduh

Lmao now it is @DynamicSquid

Profile icon
DynamicSquid

@Raniconduh forgot c didn't have classes lol

Profile icon
Raniconduh

I think that's why I like C so much. If my program needs classes, I would probably make it in Java. @DynamicSquid

Profile icon
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

Profile icon
CodeLongAndPros

@DynamicSquid Mine? Or Jobs?

Profile icon
DynamicSquid

@CodeLongAndPros steve jobs

Profile icon
CodeLongAndPros

@DynamicSquid Yeah.
And BillG:

if (gates_not_richest()) { sell_more_junk(); else if (linux_winning()) { fud_linux(); else { make_junk(); }
Profile icon
DynamicSquid
Profile icon
CodeLongAndPros
Profile icon
DynamicSquid
Profile icon
CodeLongAndPros

@DynamicSquid After reading your bio, I feel bad for you. So little school left.

Profile icon
DynamicSquid

@CodeLongAndPros well, I actually kinda miss school, but actually I now have 3 more days left thanks for reminding me :)

Profile icon
CodeLongAndPros

@DynamicSquid Sure.

Even virtual school is better than wasting away...

Profile icon
DynamicSquid

@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..

Profile icon
CodeLongAndPros

@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.

Profile icon
DynamicSquid

@CodeLongAndPros yeah, I see your point, but is EuRoPeAn ImPeRiAlIsTiC iMpAcTs On 19Th CeNtUrY CoLoNiAl InDia really gonna help write better code?

Profile icon
CodeLongAndPros

@DynamicSquid

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.

Profile icon
CodeLongAndPros

@DynamicSquid But my school has a tech apps class.
It used to be HTML/JS/CSS

Now it's:
km

And a bit of Adobe and Office.

Profile icon
DynamicSquid

@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?

Profile icon
CodeLongAndPros

@DynamicSquid No programming. We do have a Coding Club, but it's non-offical and was ran by me.

Profile icon
CodeLongAndPros

@DynamicSquid Actually, I had to help with a problem during a DLA. Good times 😅

Profile icon
DynamicSquid

@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?

Profile icon
CodeLongAndPros

@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.

Profile icon
DynamicSquid

@CodeLongAndPros oh that's what it is lol

Profile icon
CodeLongAndPros

@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.

Profile icon
DynamicSquid
Profile icon
CodeLongAndPros

@DynamicSquid Major facepalm.

Profile icon
firefish

@DynamicSquid It's 3 weeks for me..

Profile icon
DynamicSquid

@johnstev111 3 weeks?? Aw... but when does school start for you?

Profile icon
firefish

@DynamicSquid Start? 4th September

Profile icon
DynamicSquid

@johnstev111 aww really? I end school in 2 days but it also starts in the beginning of september...

Profile icon
firefish

@DynamicSquid mine finishes 10th july

Profile icon
CodeLongAndPros

@DynamicSquid Mine got out in May, but begins in August.

Super hot in August, too.

Profile icon
DynamicSquid

@CodeLongAndPros and mosquitos

Profile icon
CodeLongAndPros

@DynamicSquid Yeah. Texas summers == !good.

Profile icon
DynamicSquid

@CodeLongAndPros oh you live in Texas? does it snow there? I'm in Canada and it snows for like 8 months

Profile icon
[deleted]

@DynamicSquid You have to use strcmp...

Profile icon
CodeLongAndPros

@DynamicSquid In my lifetime, it's snowed 2.

3 if you count a 10 min flurry.

And, yes, we ride our horses to school.

Profile icon
DynamicSquid
Profile icon
CodeLongAndPros

@DynamicSquid Yes. 13 years and two snow storms, one didn't even last a day.

Profile icon
DynamicSquid

@CodeLongAndPros damn, once snowed 30+ centimetres overnight and school wasn't even cancelled, day just went on as normal lol

Profile icon
DynamicSquid

@DynamicSquid actually no, that's a lie, not once, all the time

Profile icon
CodeLongAndPros

@DynamicSquid We have floods.

2 weeks off because of a hurricane

1 week off because of the Tax Day floods.

Profile icon
DynamicSquid

@CodeLongAndPros oh, that's pretty bad. we never have floods. is it true you guys celebrate Christmas with an outdoor sunny barbecue?

Profile icon
CodeLongAndPros

@DynamicSquid No. It gets cold here, but never below 40F on normal days.

Profile icon
DynamicSquid

@CodeLongAndPros what? we have like -30C (-22F) regularly in winter

Profile icon
CodeLongAndPros

@DynamicSquid This month, we had 100F+ (37C) temps

Profile icon
mkw

hello

Profile icon
CodeLongAndPros

@mkw world

Profile icon
firefish
Profile icon
Power2311

@codelongAndpros can you tutor me coding from scratch.

Profile icon
CodeLongAndPros

@Power2311 Tutor, no.
You can read the series of tutorials, and comment if you have a question.

Profile icon
HahaYes

@CodeLongAndPros Should I use manjaro or kde plasma?

Profile icon
CodeLongAndPros

@HahaYes Manjaro kde

Profile icon
HahaYes

@CodeLongAndPros okay, hmmm kde is more powerful than xfce right?

Profile icon
CodeLongAndPros
Profile icon
HahaYes

@CodeLongAndPros hmmm what about kde plasma

Profile icon
CodeLongAndPros

@HahaYes Not a distro

Did you mean Kde neon?

Profile icon
HahaYes
Profile icon
HahaYes

@CodeLongAndPros no wait there is manjaro kde plasma

Profile icon
HahaYes

@CodeLongAndPros Yes! These are finally gaining popularity

Profile icon
CodeLongAndPros

@HahaYes Mine?

Profile icon
HahaYes

@CodeLongAndPros yeah, now you have over 800 cycles yay

Profile icon
CodeLongAndPros

@HahaYes yay -S 100_cycles

Profile icon
HahaYes
Profile icon
JustAWalrus
Profile icon
[deleted]

#include <stdbool.h>

Profile icon
HahaYes

But I do like your syntax/template of your tutorials

Profile icon
HahaYes

You SHALL NOT WIN, YOU CANNOT DISRUPT THE NOOB MARKET

Profile icon
CodeLongAndPros

@HahaYes Excuse me?

I didn't hear you.

Profile icon
HahaYes

@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

Profile icon
CodeLongAndPros

@HahaYes That's right. You are a meme.

Profile icon
HahaYes

@CodeLongAndPros Hey CodeLong, don't you use Manjaro?

Profile icon
CodeLongAndPros

@HahaYes Why, yes, yes I do.

Profile icon
HahaYes

@CodeLongAndPros also, I'm still up at the top, I'm not exactly losing market share, also, Manjaro is debian based right?

Profile icon
CodeLongAndPros

@HahaYes No, it's Arch based. Ubuntu is Debain based.

Profile icon
HahaYes

@CodeLongAndPros hmmmm so terminal will have the same syntax though right?

Profile icon
CodeLongAndPros

@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

Profile icon
HahaYes

@CodeLongAndPros oh ok that is cool

Profile icon
CodeLongAndPros

@HahaYes And it's so much faster.

Installing gdc took 2 seconds.

Profile icon
HahaYes

@CodeLongAndPros hmmm manjaro faster than ubuntu?

Profile icon
firefish

@CodeLongAndPros I've used pacman but still by far prefer dnf

Profile icon
firefish

@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?

Profile icon
CodeLongAndPros

@johnstev111 Good guess, but actually, no.

Profile icon
firefish

@CodeLongAndPros You want me to do if in a lengthy if then?
It's the same syntax as C

Profile icon
CodeLongAndPros

@johnstev111 Switch-case works in C.

The only diff is:
scanf("%s", &usrname);

Profile icon
firefish

@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

Profile icon
CodeLongAndPros

@johnstev111 Well, C != C#

Profile icon
firefish

@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

Profile icon
HahaYes

Hmmm C++ > C

Profile icon
Jakman
Profile icon
CodeLongAndPros

@HahaYes

There’s:

D

C

C++

...

...

...

C#

Profile icon
firefish

@CodeLongAndPros Did you just rate C# the worst? ARREST THIS MAN

Profile icon
firefish

@HahaYes C# > everything else

Profile icon
HahaYes

@johnstev111 C++ == C# I like both because of game dev

Profile icon
firefish

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

Profile icon
firefish

@HahaYes it's just a simple read-evaluate-print-loop

Profile icon
HahaYes

@johnstev111 I like C++ for competitive programming

Profile icon
firefish

@HahaYes take a look at this beast that I wrote 2 years back
https://repl.it/@johnstev111/PTerm

Profile icon
firefish

@HahaYes That program's awful right