You are on page 1of 5

CLASS 06 GUIDE 

 
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;

if​ (kilosOfTomatoes ​==​ 6) {


printf(“ Too many tomatoes\n”);
} ​else​ {
printf(“ Too fewer tomatoes\n”);
}
 
In the above code, we have a variable named ​kilosOfTomatoes​ that holds the value 6 for us. 
As we go along from top to bottom the C programming language comes along the if statement. It 
then checks the condition ​kilosOfTomatoes == 6​. ​If the condition is ​true​ (which in our case it is 
since kilosOfTomatoes holds the value 6), it gets into the if-block, prints out ​Too many tomatoes
and ​IGNORES the else statement​ completely.

But what if​ ​kilosOfTomatoes​ does ​not​ hold the value 10. What if it holds the value
7?

int kilosOfTomatoes ​=​ 10;

if​ (kilosOfTomatoes ​==​ 6) {


printf(“ Too many tomatoes\n”);
} ​else​ {
printf(“ Too fewer tomatoes\n”);
}

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: 

int kilosOfTomatoes ​=​ 10;

if​ (kilosOfTomatoes ​!=​ 6) {


printf(“ Too many tomatoes\n”);
} ​else​ {
printf(“ Too less tomatoes\n”);
}

C comes from top to bottom. The value of ​kilosOfTomatoes​ is 10.  


C checks the condition 10 != 6 → which evaluates to be true (because 10 is not equal to 6); it goes 
inside the if statement and prints Too many tomatoes. It ignores the else statement. 
 
For greater than: 
 
int kilosOfTomatoes ​=​ 10;

if​ (kilosOfTomatoes ​>​ 6) {


printf(“ Too many tomatoes\n”);
} ​else​ {
printf(“ Too less tomatoes\n”);
}

C comes from top to bottom. The value of ​kilosOfTomatoes​ is 10.  


C checks the condition 10 > 6 → which evaluates to be true (because 10 is greater than 6); it goes 
inside the if statement and prints Too many tomatoes. It ignores the else statement. 
 
And so on…….  
 
The else-if statement​: 
That brings us to the question of what if we want to compare the value of kilosOfTomato with two 
other values? You can use the else if statements to do that.  
 
 
 
 
 
 
 
 
The structure: 
 
if (​condition​)​ {
// if the condition is fulfilled then get in here and
// execute the statement inside the curly brackets of if
} ​else if (​second condition​)​ {
// If the above condition is ​NOT​ fulfilled then get in here and
// execute the statement inside the curly brackets of else if
} ​else​ {
// If the above condition is ​NOT​ fulfilled then get in here and
// execute the statement inside the curly brackets of else
}

Let’s look at an example. Suppose the perfect kilos of tomatoes is 3. Let’s


analyze the code below.

int kilosOfTomatoes ​=​ 3;

if​ (kilosOfTomatoes ​<​ 1) {


printf(“ You have zero kilos of tomatoes\n”);
} ​else if ​(kilosOfTomatoes ​>​ 3) {
printf(“ Too many tomatoes\n”);
}​ else if ​(kilosOfTomatoes ​==​ 3) {
printf(“ Perfect!!!!\n”);
} ​else​ {
printf(“ Too fewer tomatoes\n”);
}

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’;

if​ (vowel ​==​ ‘e’) {


printf(“The value inside the variable vowel is e.\n”);
} ​else​ {
printf(“The value is not e.\n”);
}
 
The output is: ​The value inside the variable vowel is e.

THE END.

You might also like