Decision Control Structure:
There are some circumstances where we need different sets of action to be performed at a particular instant of time.A C language has three major decision making instructions------
1->The if statement.
2->The if else statement.
1->The if statement.
2->The if else statement.
3->switch statement.
Apart from these three we have fourth type also i.e. conditional operators.
In this post I will show you about the if ,if else method, rest I will describe in another post.
In this post I will show you about the if ,if else method, rest I will describe in another post.
The if Statement:
C uses a keyword if to implement the decision control instructions,it's syntax is
if(condition true) { execute these set of statement;.....
__________________}
if(condition true) { execute these set of statement;.....
__________________}
Flow Chart of" if "Structure |
The if statement tells the compiler that to check the condition is true or not enclosed within a pair of parentheses. If true then it will undergo the execution block of "if".otherwise proceed to other statements.
NOTE: In C a non zero value is considered to be true and 0 is considered to be false. i.e.
NOTE: In C a non zero value is considered to be true and 0 is considered to be false. i.e.
if(5)
//true statements are executed
if(-5)
//true statements are executed
if(-5)
//here too statements gets executed true
if(0)
if(0)
//false it will terminate the if block
there are operators which can be used for particular conditions like:
RELATIONAL OPERATORS
1)
there are operators which can be used for particular conditions like:
RELATIONAL OPERATORS
1)
"= =" :Equal to
example:
a==b means a is equal to b
example:
a==b means a is equal to b
2)
"!=" : Not equal to
example:
a!=b means a is not equal to b.
3)
"<" :Less then
example:
a<b means a is less then b.
4)
example:
a!=b means a is not equal to b.
3)
"<" :Less then
example:
a<b means a is less then b.
4)
">":Greater then
example
example
a>b means a is greater then b.
5)
"<=" :Less then or equal to
example
a<=b means a is less then or equal to b
6)
">=" :Greater then or equal to
example
a>=b means a is greater then or equal to b.
LOGICAL OPERATORS
7) The logical And is represented as " && "
8) The logical OR is represented as " || "
9) The logical NOT is represented as " ! "
LOGICAL OPERATORS
7) The logical And is represented as " && "
8) The logical OR is represented as " || "
9) The logical NOT is represented as " ! "
The if -else Statement.
The main drawback of if statement is that the following set of statements within "if block " executes only when the condition is true .It does nothing when expression evaluates to false.This is what exactly the purpose of else statement is being used.
syntax:
if(condition)
{........//statements; }
else
{,,,,,,,,,,,,,,,,,,,,,,,,,,//statements;}
NOTE:The else block should be written exactly below the "if "block.
Nested if-else :
syntax:
if(condition)
{........//statements; }
else
{,,,,,,,,,,,,,,,,,,,,,,,,,,//statements;}
Flow chart for " if-else"structure. |
Nested if-else :
We can also write an entire if-else construct within either the body of if statement or the body of else statement. This is called nesting of ifs
The example of nested if-else is shown below:
Nested if-else. |
No comments:
Post a Comment