Hello Everyone.
Now Lets Talk about the very useful if-else statements. It is a very simple type of Selective Statement but is of great Use.
A If statement is Generally used to Compare Standard Data Types like integer,char ,double,etc. which is put inside parenthesis, whose result is given in either true or false,or 0 or 1, and is then checked by the compiler, if it is true ,the statements following this if statement will execute ,Otherwise it will not execute if statement block and execute the else block if present.
An else block is a Block that executes in case if statement is not satisfied.
Now Coming to the example.It will be more clear when you see the Example.
int n=5;
if(n>2)
{
n++;
n = 2*n;
}
else{
n--;
n=2*n;
}
Here, We have Declared a integer type variable n with value 5. Then there is an if statement with test expression = n>2 . Since, n=5 ,i.e., n>2 , Therefore, this condition is true and satisfied. Therefore , if statement block will execute. In this case the else block will not execute. Now , inside the if Block there are 2 statements. The first one is n++ which will increase our n by 1 , i.e. , n=6. now it will multiply value of n by 2 and store it. Therefore the final value of n will be 12.
Now take another Case.
int n=2;
if(n>2)
{
n++;
n = 2*n;
}
else{
n--;
n=2*n;
}
Now Lets Talk about the very useful if-else statements. It is a very simple type of Selective Statement but is of great Use.
A If statement is Generally used to Compare Standard Data Types like integer,char ,double,etc. which is put inside parenthesis, whose result is given in either true or false,or 0 or 1, and is then checked by the compiler, if it is true ,the statements following this if statement will execute ,Otherwise it will not execute if statement block and execute the else block if present.
An else block is a Block that executes in case if statement is not satisfied.
Now Coming to the example.It will be more clear when you see the Example.
int n=5;
if(n>2)
{
n++;
n = 2*n;
}
else{
n--;
n=2*n;
}
Here, We have Declared a integer type variable n with value 5. Then there is an if statement with test expression = n>2 . Since, n=5 ,i.e., n>2 , Therefore, this condition is true and satisfied. Therefore , if statement block will execute. In this case the else block will not execute. Now , inside the if Block there are 2 statements. The first one is n++ which will increase our n by 1 , i.e. , n=6. now it will multiply value of n by 2 and store it. Therefore the final value of n will be 12.
Now take another Case.
int n=2;
if(n>2)
{
n++;
n = 2*n;
}
else{
n--;
n=2*n;
}
In this case, the test expression of the if statement results false , therefore it will not be executed. Now, it will go to the else block and execute it. The result this code will be n=2.
Now, There can be Multiple and Nested if-else Statements. In that Case You have to take care that you have right indentation so that each if has its corresponding else at right place so that there is no misplaced else.Example:-
if(text expression1){
if(test expression2){
}
}
else{//for test expression 2.
}
Notice that it will not give any syntax error , most of the times it causes the logical error.
So , that's it for today.
Hope you learned Something useful today.
Next time we will practice some questions.
If you liked this post please share it and post a comment to it.
Comments
Post a Comment