Control Structures 1: If

Submitted by M E Senturk on Tue, 09/15/2009 - 17:26


If we compile some code, it will start to compile from top to bottom. We can manipulate the code to avoid it. There are some methods to manipulate it. Once people used "goto" statement. This statement gets an integer value, the line number, and when compiler reads the statement, it continues to compile from the line that mentioned with goto statement. Some time later, if and switch statements were created. People intended use these statements instead of "goto". Today, your school teachers or university professors will tell you not to use "goto". Ask them "why?". They will probably tell you that using goto decreases code readability, re-usability, performance, etc. They might be right. But investigate it yourself, is it really wrong to use goto?


If statement helps you to control the flow of the program. For example you have a denominator. If this denominator is not equal to zero, you will use it otherwise you need to use another value as denominator.

int denominator;
scanf("%d", &denominator);
if(denominator==0)
{
    denominator = 1;
}
// do some stuff.

As you can see, we used if statement to control the value of denominator and due to its value, we make small changes to flow of the program. Due to the if statement, if the denominator is equal to zero, we make it equal to 1, otherwise we don't do anything.

 

Else

Else is also a statement that is used with if statement. It helps to assign work for the values that do not apply for the if statement.

int denominator;
scanf("%d", &denominator);
if(denominator==0)
{
    denominator = 1;
}
else
{
    //do some stuff.
}

It is ok, if we did not use "else" in this case. Let's create another scenario. Now we want the denominator to be equal to 5 if you entered -1.

int denominator;
scanf("%d", &denominator);
if(denominator==0)
{
    denominator = 1;
}
else
{
    if(denominator==-1)
    {
        denominator=5;
    }
}

 

What we used in here is called nested if. If you need to control the flow for various situations, nested if will be a choice.
Lets

What we used in here is called nested if. If you need to control the flow for various situations, nested if will be a choice.
Here's an example.

 

#include <stdio.h>

int main()
{
	int point;
	char grade;
	scanf("%d",&point);
	if(point>90)
	{
		grade = 'A';
	}
	else
	{
		if(point>80)
		{
			grade = 'B';
		}
		else
		{
			if(point>70)
			{
				grade = 'C';
			}
			else
			{
				if(point>60)
				{
					grade = 'D';
				}
				else
				{
					grade = 'F';
				}
			}
		}
	}
	
	printf("Your grade is %c",grade);
	return 0;
}

As you see from the example, first we checked if your point is greater than 90. If it is you would get A but if not you may get one of other grades.

After that we checked if it is more than 80, if not then check if it is more than 70 and so on. If we did not use nested if, there won't be a way to separate the values greater than 50, smaller than 90. Think about it, 88 is both greater than 70 and 80.

 

I hope i could explained it well.