Control Statements - Conditional Statements
Control Statements - Conditional Statements
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 then if-statement is not recommended. This disadvantage
is overcome using two- way decision/selection statement called “ if-else statement”.
Syntax: Flowchart:
Statement 1; Statement 1;
Statement 2; Statement 2;
if(condition)
{
Statement 3; true if false
Statement 4; condition
}
else
{ Statement 3; Statement 5;
Statement 4; Statement 6;
Statement 5;
Statement 6;
}
Statement 7;
Statement 7;
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 where statement3 , statement4 are executed and
automatically goes to statement7.
If false - control goes to else part where statement5, statement6 are 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
If condition-1 is true then again condition-2 is checked if both are true then Statement1 is
executed.
If condition-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.
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 1 is checked
if true control goes to Statement3 and automatically goes to Statement7.
If false Condition 2 is checked
If Condition 2 is true control goes to Statement4 and automatically goes to Statement7.
If false Condition 3 is checked
If Condition 3 is true control goes to Statement5 and automatically goes to Statement7
If false Statement6 and automatically goes to Statement 7.
Note: Write Algorithm, Flowchart and C Program for the following:
To check an alphabet is vowel or consonant.
To illustrate the simple calculator operations with condition for division operation.
Switch statement
Switch statements are used in following scenarios
When a decision has to be made between many alternative cases.
When the selection condition reduces to an integer value.
The multiple selection mechanism is implemented using switch.
switch is alternative for two way selection multiple if-else-if.
Syntax: Flowchart:
switch(an integer value)
{
case value1: Statement1;
break;
case value2: Statement2;
break;
case value3: Statement3;
break;
default: Statement4;
break;
}
Explanation
The switch statement is a multi-way decision that tests whether an expression matches one of a
number of constant integer values / cases and branches accordingly.
Each case is labelled by one or more integer-valued constants or constant expressions.
If a case matches the expression value, execution starts at that case.
All case expressions must be different.
The case labelled default is executed if none of the other cases are satisfied.
A default is optional; if it isn't there and if none of the cases match, no action takes place. Cases and
the default clause can occur in any order.
The break statement causes an immediate exit from the switch.
More about switch statement:
The expression of a switch statement must result in an integral type, meaning an integer (byte, short,
int, long) or a char.
The expression cannot be a boolean value or a floating point value (float or double)
No two case labels can have the same constant value.
getch();
}
for(exp1;exp2;exp3) for(exp1;exp2;exp3)
{ {
Action 1; Action 1;
------------ continue;
------------ ------------
Action N; Action N;
}
#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
int i; int i;
for(i=1;i<=3;i++) for(i=1;i<=3;i++)
{ {
printf(“INDIA”); printf(“INDIA\n”);
printf(“is our country\n”); continue;
} printf(“is our country\n”);
getch() }
} getch()
}
OUTPUT: OUTPUT:
INDIA is our country INDIA
INDIA is our country INDIA
INDIA is our country INDIA