The need:
After the previous 2 programs I was asked to make the program which would ask the user to choose whether he wanted to convert temperature from degree C to degree F or vice-versa. For that I thought that there must be a 'keyword' to select an option based on some condition(if C was developed by intelligent people) and I was right. There existed a keyword 'if' for that purpose.
I had to make out how if works. This program was written just to check the use of 'if'.
The code:
---------------------------------------------
#include<stdio.h>
int main()
{
int i=0;
if(i==0)
printf("i is equal to zero\n");
else
printf("i is not equal to zero\n");
return 0;
}
---------------------------------------------
After the success of this program I came to know only that if there are 2 possibilities upon which we have to decide accordingly(like in our case either i can be equal to 1 or cannot be, there is no other possibility), we can use 'if' with a following 'else'. In the 'if' statement the condition is given between the parenthesis. Like in our case 'i==0' is a condition which claims that value of 'i' is equal to 0 or should I say compares 'i' to 0. The functionality of 'if' is some thing like, if the condition inside the parenthesis of 'if' statement is true (in this case (i==0)) then the statement just after if is executed otherwise skipped and statement just after 'else' is executed. In our case the output is:
as the condition that i is equal to 0 is always true. Two worth-noting points:
1. The statement following if has nothing to do with the condition.
2. There may be an 'if' statement without 'else'. but there is no 'else' case without 'if'
After the previous 2 programs I was asked to make the program which would ask the user to choose whether he wanted to convert temperature from degree C to degree F or vice-versa. For that I thought that there must be a 'keyword' to select an option based on some condition(if C was developed by intelligent people) and I was right. There existed a keyword 'if' for that purpose.
I had to make out how if works. This program was written just to check the use of 'if'.
The code:
---------------------------------------------
#include<stdio.h>
int main()
{
int i=0;
if(i==0)
printf("i is equal to zero\n");
else
printf("i is not equal to zero\n");
return 0;
}
---------------------------------------------
After the success of this program I came to know only that if there are 2 possibilities upon which we have to decide accordingly(like in our case either i can be equal to 1 or cannot be, there is no other possibility), we can use 'if' with a following 'else'. In the 'if' statement the condition is given between the parenthesis. Like in our case 'i==0' is a condition which claims that value of 'i' is equal to 0 or should I say compares 'i' to 0. The functionality of 'if' is some thing like, if the condition inside the parenthesis of 'if' statement is true (in this case (i==0)) then the statement just after if is executed otherwise skipped and statement just after 'else' is executed. In our case the output is:
i is equal to zero
as the condition that i is equal to 0 is always true. Two worth-noting points:
1. The statement following if has nothing to do with the condition.
2. There may be an 'if' statement without 'else'. but there is no 'else' case without 'if'
No comments:
Post a Comment
feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply