If( weather==cloudy)
It will rain.
-------------------------------------------------------------
If (cloudy weather== 1)
{
Printf(“It will rain”);
}
You can identify the similarity between the IF in English and in C language. Now, we will deal with ‘IF’ in more technical way or say let us use it programmatically. ‘IF’ is a strict comparison feature in C. ‘IF’ always look for exact comparison. Let us take the example
Main()
{
Float me=1.1;
Double you=1.1;
If(me==you)
{
Printf(“Waw! We are same”);
}
Else
Printf(“Nope! We are different”);
}
Here u must be wondering with strange word ‘ELSE’, don’t you? We will be coming to that just after the explanation of this program.
Here the values assigned to variable me and you is same, so we easily conclude that comparison will return a true or positive non-zero value and print statement in that block will get executed. One more term is there we should look for before concluding the answer and that is data type of both the variables. Variable me is of float type and you is of double type. And after referring to article 1 on data types we come to know that float and double are two different types of storing data. They differ in the number of precisions after decimal and so they require different amount of memory blocks as float requires 4 bytes while double requires 8 bytes. Hence the values assigned to float me and double you though they are same their representation in memory is different in fact and therefore the program result the answer saying “ No! We are different”.
Now, let us come to the ‘stranger’ i.e. ELSE. ‘ELSE’ is a follower of ‘IF’ but not always. Sometimes we only need ‘IF’ statement, and sometimes both ‘IF’ and ‘ELSE’. ‘ELSE’ again works exactly according to its literal meaning. Again lets take that real time example. If the condition returns zero, sometimes you want the program do something else. In that case, after the if statement, you use an else statement.
If( weather==cloudy)
It will rain.
Else
It will not rain
--------------------------------------------------------------------
If (cloudy weather== 1)
{
Printf(“It will rain”);
}
Else
{
Printf(“It will not rain”);
}
‘ELSE’ always gets executed when the condition in IF statement is false. So, it means that a ELSE block should contain something which we want to get executed if the condition in IF statements results false value or zero value.
One more provision for conditional branching is there in C language that we call ‘ELSEIF’. If we have multiple conditions to check and wish to execute separate block for each condition then it is always a good practice to use elseif than multiple IF statements. Let’s have a look at following example.
#include
int main() {
int a;
printf("Input an integer and push return:\n");
scanf("%d", &a);
if(a%2==0 && a<0) { printf("%d is even and less than zero.\n", a); } else if (a%2!=0 && a<0) { printf("%d is odd and less than zero.\n", a); } else if (a%2==0 && a>0)
{
printf("%d is even and greater than zero.\n", a);
}
else if (a%2!=0 && a>0)
{
printf("%d is odd and greater than zero.\n", a);
}
else
{
printf("You entered zero.\n");
}
return 0;
}
the program output will depend on the value you enter.
The main focus is on the if block, with the solitary if and else statements, as well as the else ifs.
Now, the program evaluates the if condition. If it returns a non zero value, the if branch is executed. Once all the statements in that branch have been executed, THE PROGRAM IGNORES THE REST OF THE IF BLOCK. In other words, the remaining conditions are not evaluated.
If, on the other hand, (a%2==0 && a<0) returns zero, the first else if condition is evaluated and so on.
The main thing to remember is that once the program chooses a branch in the if block, the remaining branches are totally ignored.
One final note: I tend not to end an if block on an else if.
If one wanted to say, "else do nothing", rather than missing the else statement totally, its always better to insert a else block containing nothing.
if(x==0)
{
printf("x is zero\n");
}
else if(x==1)
{
printf("x is 1\n");
}
else
{
/* Do nothing */
}

No comments:
Post a Comment