Decision Control Instructions.
In C programs, we must be able to perform different sets of actions depending on the circumstances. In many programming situations, we want one set of instructions to be executed in one situation, and a different set in another situation.
In C Programming, such situations are dealt with using a decision control instruction.
If-Else Statements:-
if and else to implement the decision control instruction. The general form of the stament look like this.
if (this condition is true)
line 1;
else
line 2;
if is always enclosed with parentheses, If the condition is true, then statement 1 is executed. If the condition is not true, then statement2 is executed.
How they look and how they are evaluated in C.
This Expression Is true if
x==y x is equal to y
x!=y x is not equal to y
x<y x is less than y
x>y x is greater than y
x<=y x is less than or equal to y
x>=y x is greater than or equal to y
We also learn some codes in future for more clarity regarding this topic.
Some points that you must about this:-
(a) If the statements after the if doesn't include the else is called an 'if block'. Like that the statement after the else is called the 'else block'.
(b) else is always written below the if.
(c) Had there been only one statement to be executed in the if block and only one statement in the else block.
(d) The default scope of if as well as else is the statement immediately after them.
Nested if-else:-
If we write another if-else construct within either the if block or the else block.
This is called Nesting.
Example:-
if (i==1)
printf ("You would go to heaven !\n");
else
{
if (i==2)
printf ("Hell was created with you in mind \n");
else
printf ("How about mother earth !\n");
}
In this program must see on the second if-else construct is nested in the first else block.
If the condition in the first if is false, then the condition in the second if is checked. If it is false as well, then the second else is executed.
This is the basic's of this topic, that you must have to know.
You must have to write some small codes in your Laptop/Computer/Mobile to learn coding in less effort.
Coading is that art which needs constant practise.
Comments