Conditional Statements - 01
Conditional Statements - 01
Explanation
The keyword if must be followed by an expression and expression must be enclosed within
parentheses.
First statement1 is executed followed by statement2.
Then Condition is checked
✓ if false - control directly jumps to statement5 ignoring statement3 and 4.
✓ if true - control goes to statement3 , statement4 and automatically goes to statement5.
An Example which illustrates if statement: To print given no is an even no.
Algorithm Flowchart Program
Note: Similarly write Algorithm, Flowchart and C Program for the following
❖ To print given no is an odd no. Logic: if (n!=0)
❖ To print given no is a positive no.Logic: if(n>0)
❖ To print given no is a negative no. Logic: if(n<0)
Disadvantage
✓ If one action has to be performed when the condition is true and another action has to be
performed when the condition is false thenif-statement is not recommended. This disadvantage
is overcome using two- way decision/selection statement called “ if-else statement”.
Syntax: Flowchart:
if (condition)
{
// do this if condition is true
// if true statements
}
else
{
// do this is condition is false
// if false statements
}
Explanation
The keyword if and else must be followed by an expression and expression must be enclosed within
parentheses.
First statement1 is executed followed by statement2.
Then Condition is checked
✓ If true- control goes to if part wherestatement3 , statement4 are executed and
automatically goes to statement7.
✓ If false -control goes to else part where statement5, statement6are executed and
automatically goes to statement7.
In if-else either true part i.e., if part is executed or false part i.e., else part is executed based in the
condition or test expression.
Note: Similarly write Algorithm, Flowchart and C Program for the following
❖ To check given integer no is a positive no or negative no.
Logic: if(n>0) its positive else negative.
❖ To find largest of 2 no’s .
Logic: Let a and b be 2 no’s if(a>b) a is greater else b is greater
❖ To check given no is even or odd
Logic: Let n be a no’s if(n%2==0) n is even else n is odd.
Nested if else
It is used to execute one set of statements out of many set of statements depending upon the outcome
of the conditions.
It consists of if else control constructs with in another if or else control constructs and hence the
name is nested if else.
Syntax: Flowchart:
if(condition-1)
{
if (condition-2)
Statement1;
else
Statement2;
}
else
{
s Statement3;
}
Statement4;
Explanation
The keyword if and else must be followed by an expression and expression must be enclosed within
parentheses.
Then Condition is checked
✓ Ifcondition-1 is true then again condition-2 is checked if both are true then Statement1 is
executed.
✓ Ifcondition-1 is true but condition-2 is false means Statement2 is executed.
✓ If condition-1 itself is false control goes to Statement3 and automatically goes to Statement4.
Advantage:
When an action has to be performed based on many decisions involving various types of expressions
and variables then nested if statement is used.
Disadvantage:
Difficult to understand and modify. As depth of nesting increases, the readability of the program
decreases.
Note: Similarly write Algorithm, Flowchart and C Program for the following
❖ To check given integer no is a positive no or negative no or zero.
❖ To find the greatest of three numbers.
❖ Magic number program.