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.
C wars: Chapter Three: Revenge of the if
Chapter three: Revenge of the if
Today I will be talking about
booleans
andif
,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
andfalse
keywords to set the value of a bool:Examples:
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!"
There are three more:
!
: not.This one makes
true
befalse
, andfalse
betrue
.So to toggle
boola
, you can doboola = !boola
.&&
: and.This one is true if
boola
andboolb
are both true:boola && boolb
||
: or.This one is true if
boola
orboolb
is trueNote: 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:
The code in between
{}
will run ifboolean
istrue
.So:
You can use the
else if
to run code ifbool1
is false, butbool2
is true:And finally, you can use
else
to run if any of the otherif
's are false:There is one more syntax for an
if
statment.This one only works on one statment:
One more thing:
You need to learn how to compare strings.
You use
strcmp(a, b)
to do so.strcmp
is instring.h
.To compare char a and char 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
, printYou own apple.
If their name is
Bill gates
, printYou own MS.
.If their name is
Richard stallman
, printYou 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 That's right. You are a meme.