4 – Conditionals

Conditionals are ways of checking whether or not a certain criteria is met, like whether or not a variable is equal to a given value. There are numerous ways of doing conditionals, some are syntactical and some are done with operators. In this lesson, I will be going over a few of them.

Sounds Scary, Should I Be Frightened?

Not at all, conditionals are easy to grasp and much more exciting than anything else we have discussed up until now.

Conditionals are presented in our code in the form of a statement and if the statement evaluates as being a true statement, we can have the program execute a piece of code, if a statement evaluates as being a false statement, we can have it execute another piece of code. This is called code-branching and it allows you to do many interesting things — like, a guessing game, which we will be doing in a lesson not too far ahead of this one.

Statements

A statement is the “question” you impose on a conditional. Statements are usually “asking” if a variable has a given value or is equal to another variable, and we do this with operators. Statements can be as simple or as complicated as you like, however you need to keep in mind that in the end a statement must evaluate either true or false (anything other than 0 is considered to be true, 0 is of course false).

True/False Operators

Unlike the operators we looked at in the last lesson, these are comparison operators and return a boolean value (true or false). There are a few of them, we will only look at six for now.

  • ==
    The equality operator compares two values and evaluates true if they are equal, false if they’re not.

  • !=
    The not equal operator compares two values and evaluates true if they are not equal, false if they are.

  • <
    The less than operator compares two values and evaluates true if the value on the left of the operator is less than the value on the right of it, false if not.

  • >
    The greater than operator compares two values and evaluates true if the value on the left of the operator is greater than the value on the right of it, false if not.

  • <=
    The less than or equal operator compares two values and evaluates true if the value on the left of the operator is less than or equal to the value on the right of it, false if not.

  • >=
    The greater than or equal operator compares two values and evaluates true if the value on the left of the operator is greater than or equal to the value on the right of it, false if not.

If … Then …

The ‘if’ statement is most basic statement. It checks whether or not a statement is true, and if it is, it will execute a code-block. An ‘if’ statement is defined by the keyword if followed by a statement enclosed in parentheses and lastly the code-block to be executed, should it evaluate true.


int a = 1;
int b = 1;

if ( a == b )
{
	printf("This statement is true, a is equal to b!");
}

Exercise 4.1
Of course, the above example will always evaluate true, since both variables are set to a fixed value of 1. Play around with the values of the variables ‘a’ and ‘b’, and the operator used to compare the two.

Else … If …

Expanding on the ‘if’ statement, we can append an optional ‘else’ code-block. This will will execute if the ‘if’ statement evaluates to false.


int a = 1;
int b = 1;

if ( a < b )
{
	printf("This statement is true, a is less than b!");
}
else
{
	printf("This statement is false, a is not less than b!");
}

Exercise 4.2
Again, play around with the values of the variables, and the operator used to compared them. Get a good feel and understanding of the if statement. If you feel comfortable with it, go on and make a series of if statements and inside the if/else code-blocks alter the values of the variables. This will make it quite clear how conditionals can affect the flow of your application and it will be very good practice for you — practice makes perfect, young padawan.

Previous: Operators
Next up: Loops

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s