Professional Documents
Culture Documents
Today we are going to learn about conditional statements. Conditional statements allow you to
execute a certain programming statement on a condition. As simple as that. We use the if-else
clause to tell C that we need it to use a conditional statement. The code goes like this:
if (condition) {
// if the condition is fulfilled then get in here and
// execute the statement inside the curly brackets of if
} else {
// If the above condition is NOT fulfilled then get in here and
// execute the statement inside the curly brackets of else
}
So what is this condition thing? This condition is a boolean. So what is a boolean you might ask? A
boolean is either true or false.
Boolean Conditions AKA relational operators:
Before jumping into if-else statements let us look into some operators that evaluate/calculate an
expression into a boolean statement:
Operators:
The equality operator: == → checks if two values are equal.
The inequality operator: != → checks if two values are NOT equal.
The greater than operator: > → checks if one value is greater than the other.
The greater than or equal to operator: >= → checks if one value is greater than or equal to the other.
The less than operator: < → checks if one value is less than the other value.
The less than or equal to operator: <= → checks if one value is less than or equal to the other.
The equality operator:
==
The equality operator checks if two values (both inside a variable and not inside a variable) are
equal or not. Don’t confuse yourself with the assignment operator which is a single equal sign( for
example x = 5 ).
For example:
Let’s look at the code below.
int kilosOfTomatoes = 6;
But what if kilosOfTomatoes does not hold the value 10. What if it holds the value
7?
Let’s take another walk through the code. Suppose the kilosOfTomatoes hold 10.
As we go along from top to bottom the C programming language comes along the if statement. It
then checks the condition kilosOfTomatoes == 6. BUT this time kilosOfTomatoes is NOT 6. It
holds the value 10 instead; and bam!!! The condition is false. So what C does now is, it IGNORES the
if statement and goes to the else statement and prints out Too fewer tomatoes.
In simple Bengali: Jodi kilosOfTomatoes 6 hoi then Too many tomatoes print koro
NAHOLE Too fewer tomatoes print koro.
For the other operators, the procedure for selecting if-else is similar; only the
logic is different.
For example:
For inequality:
Let’s walk through the code. We have a variable kilosOfTomatoes whose value is 3. We check the
condition of the if statement first. Is 3 less than 1? NOOOO!!!. That is a false condition hence we
ignore the if block and go to the second else if condition. We check the condition of the second
else if. Is 3 greater than 3? NOOO!!! That is a false condition hence we ignore the if block and go to
the third else if condition. We check the condition of the third else if. Is 3 equal to 3? Absolutely!
The condition is true and hence it gets inside the third else if block and prints Perfect!!!! to the
console and IGNORES the else statement completely.
Some Tips:
To compare a character it should be within single quotes (Like the way you declare a variable).
For example:
char vowel = ‘e’;
THE END.