Learn to Code via Tutorials on Repl.it!

← Back to all posts
C wars: Chapter Three: Revenge of the if
CodeLongAndPros (1631)

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.


  • 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:


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

The code in between {} will run if boolean is true.


if (1==1) {

You can use the else if to run code if bool1 is false, but bool2 is true:

if (bool1) {
} else if (bool2) {

And finally, you can use else to run if any of the other if's are false:

if (bool1) {
} else if (bool2) {
} else {
    spam && eggs;

There is one more syntax for an if statment.

This one only works on one statment:

if (a)
else if (b)
    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:


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

HahaYes (1869)

hmmmmm still not as popular as my series

CodeLongAndPros (1631)

@HahaYes Well, you've gotten traction

CodeLongAndPros (1631)

@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

HahaYes (1869)

@CodeLongAndPros hmmm I'm google you are the startup

HahaYes (1869)

@CodeLongAndPros Oh, I thought they designed their own

CodeLongAndPros (1631)

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

DynamicSquid (5024)

His life story:

if (person.name == "Steve Jobs")
if (doesntWorkThereAnymore(person))
if (cameBackToTheCompany(person))
DynamicSquid (5024)

@Raniconduh forgot c didn't have classes lol

Raniconduh (18)

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

DynamicSquid (5024)

@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

CodeLongAndPros (1631)

@DynamicSquid Yeah.
And BillG:

if (gates_not_richest()) {
else if (linux_winning()) {
else {
CodeLongAndPros (1631)

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

DynamicSquid (5024)

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

CodeLongAndPros (1631)

@DynamicSquid Sure.

Even virtual school is better than wasting away...

DynamicSquid (5024)

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

CodeLongAndPros (1631)

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

DynamicSquid (5024)

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

CodeLongAndPros (1631)


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.

CodeLongAndPros (1631)

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

Now it's:

And a bit of Adobe and Office.

DynamicSquid (5024)

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

CodeLongAndPros (1631)

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

CodeLongAndPros (1631)

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

DynamicSquid (5024)

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

CodeLongAndPros (1631)

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

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

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

DynamicSquid (5024)

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

CodeLongAndPros (1631)

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

Super hot in August, too.

CodeLongAndPros (1631)

@DynamicSquid Yeah. Texas summers == !good.

DynamicSquid (5024)

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

sugarfi (630)

@DynamicSquid You have to use strcmp...

CodeLongAndPros (1631)

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

3 if you count a 10 min flurry.

And, yes, we ride our horses to school.

CodeLongAndPros (1631)

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

DynamicSquid (5024)

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

DynamicSquid (5024)

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

CodeLongAndPros (1631)

@DynamicSquid We have floods.

2 weeks off because of a hurricane

1 week off because of the Tax Day floods.

DynamicSquid (5024)

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

CodeLongAndPros (1631)

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

DynamicSquid (5024)

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

CodeLongAndPros (1631)

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

Power2311 (0)

@codelongAndpros can you tutor me coding from scratch.

CodeLongAndPros (1631)

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

HahaYes (1869)

@CodeLongAndPros Should I use manjaro or kde plasma?

HahaYes (1869)

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

HahaYes (1869)

@CodeLongAndPros hmmm what about kde plasma

CodeLongAndPros (1631)

@HahaYes Not a distro

Did you mean Kde neon?

HahaYes (1869)

@CodeLongAndPros no wait there is manjaro kde plasma

sugarfi (630)

#include <stdbool.h>

HahaYes (1869)

But I do like your syntax/template of your tutorials

HahaYes (1869)


CodeLongAndPros (1631)

@HahaYes Excuse me?

I didn't hear you.

HahaYes (1869)

@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

HahaYes (1869)

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

HahaYes (1869)

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

CodeLongAndPros (1631)

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

HahaYes (1869)

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

CodeLongAndPros (1631)

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

@HahaYes And it's so much faster.

Installing gdc took 2 seconds.

HahaYes (1869)

@CodeLongAndPros hmmm manjaro faster than ubuntu?

firefish (953)

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

firefish (953)

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


CodeLongAndPros (1631)

@johnstev111 Good guess, but actually, no.

firefish (953)

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

CodeLongAndPros (1631)

@johnstev111 Switch-case works in C.

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

firefish (953)

@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

firefish (953)

@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


firefish (953)

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

HahaYes (1869)

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

firefish (953)

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

firefish (953)

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

HahaYes (1869)

@johnstev111 I like C++ for competitive programming

firefish (953)

@HahaYes take a look at this beast that I wrote 2 years back

firefish (953)

@HahaYes That program's awful right