C wars: Chapter Three: Revenge of the if
h
CodeLongAndPros (1589)

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

You are viewing a single comment. View All
DynamicSquid (4624)

His life story:

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

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

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

@DynamicSquid Yeah.
And BillG:

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

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

DynamicSquid (4624)

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

CodeLongAndPros (1589)

@DynamicSquid Sure.

Even virtual school is better than wasting away...

DynamicSquid (4624)

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

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

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

CodeLongAndPros (1589)

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

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

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

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

CodeLongAndPros (1589)

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

DynamicSquid (4624)

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

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

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

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

DynamicSquid (4624)

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

CodeLongAndPros (1589)

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

Super hot in August, too.

DynamicSquid (4624)

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

sugarfi (625)

@DynamicSquid You have to use strcmp...

CodeLongAndPros (1589)

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

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

DynamicSquid (4624)

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

DynamicSquid (4624)

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

CodeLongAndPros (1589)

@DynamicSquid We have floods.

2 weeks off because of a hurricane

1 week off because of the Tax Day floods.

DynamicSquid (4624)

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

CodeLongAndPros (1589)

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

DynamicSquid (4624)

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

CodeLongAndPros (1589)

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