Learn to Code via Tutorials on Repl.it!

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

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!

Commentshotnewtop
DynamicSquid (2611)

His life story:

if (person.name == "Steve Jobs")
{
  kickOutOfCompany(person);
}
if (doesntWorkThereAnymore(person))
{
  comeBackToTheCompany(person);
}
if (cameBackToTheCompany(person))
{
  kickOutOfCompany(person);
}
DynamicSquid (2611)

@Raniconduh forgot c didn't have classes lol

Raniconduh (17)

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

DynamicSquid (2611)

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

@DynamicSquid Yeah.
And BillG:

if (gates_not_richest()) {
  sell_more_junk();
else if (linux_winning()) {
  fud_linux();
else {
  make_junk();
}
CodeLongAndPros (952)

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

DynamicSquid (2611)

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

CodeLongAndPros (952)

@DynamicSquid Sure.

Even virtual school is better than wasting away...

DynamicSquid (2611)

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

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

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

CodeLongAndPros (952)

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

CodeLongAndPros (952)

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

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

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

CodeLongAndPros (952)

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

DynamicSquid (2611)

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

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

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

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

DynamicSquid (2611)

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

CodeLongAndPros (952)

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

Super hot in August, too.

CodeLongAndPros (952)

@DynamicSquid Yeah. Texas summers == !good.

DynamicSquid (2611)

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

sugarfi (484)

@DynamicSquid You have to use strcmp...

CodeLongAndPros (952)

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

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

DynamicSquid (2611)

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

DynamicSquid (2611)

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

CodeLongAndPros (952)

@DynamicSquid We have floods.

2 weeks off because of a hurricane

1 week off because of the Tax Day floods.

DynamicSquid (2611)

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

CodeLongAndPros (952)

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

DynamicSquid (2611)

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

CodeLongAndPros (952)

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

HahaYes (1014)

hmmmmm still not as popular as my series

CodeLongAndPros (952)

@HahaYes Well, you've gotten traction

CodeLongAndPros (952)

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

@CodeLongAndPros hmmm I'm google you are the startup

HahaYes (1014)

@CodeLongAndPros Oh, I thought they designed their own

CodeLongAndPros (952)

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

Power2311 (0)

@codelongAndpros can you tutor me coding from scratch.

CodeLongAndPros (952)

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

HahaYes (1014)

@CodeLongAndPros Should I use manjaro or kde plasma?

HahaYes (1014)

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

CodeLongAndPros (952)

@HahaYes Not a distro

Did you mean Kde neon?

HahaYes (1014)

@CodeLongAndPros no wait there is manjaro kde plasma

sugarfi (484)

#include <stdbool.h>

HahaYes (1014)

But I do like your syntax/template of your tutorials

HahaYes (1014)

You SHALL NOT WIN, YOU CANNOT DISRUPT THE NOOB MARKET

CodeLongAndPros (952)

@HahaYes Excuse me?

I didn't hear you.

HahaYes (1014)

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

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

HahaYes (1014)

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

CodeLongAndPros (952)

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

HahaYes (1014)

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

CodeLongAndPros (952)

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

@HahaYes And it's so much faster.

Installing gdc took 2 seconds.

HahaYes (1014)

@CodeLongAndPros hmmm manjaro faster than ubuntu?

firefish (305)

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

firefish (305)

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

CodeLongAndPros (952)

@johnstev111 Good guess, but actually, no.

firefish (305)

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

CodeLongAndPros (952)

@johnstev111 Switch-case works in C.

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

firefish (305)

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

@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

firefish (305)

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

HahaYes (1014)

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

firefish (305)

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

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

HahaYes (1014)

@johnstev111 I like C++ for competitive programming

firefish (305)

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

firefish (305)

@HahaYes That program's awful right