Control Statement
Control Statement
Control Statement
if statement
Statement execute set of command like when condition is true
syntax
if (condition) {
statement/body;
}
#include <stdio.h>
int main()
{
int n=12;
if(n>10)
printf("number is greater");
}
Output:
Enter a number:12
Number is greater
If ….else ... Statement
It is bidirectional conditional control statement that contains one condition &
two possible actions. Condition may be true or false, where non-zero value
regarded as true & zero value regarded as false. If condition are satisfy true,
then a single or block of statement executed otherwise another single or
block of statement is executed.
syntax
if (condition)
{
Statement 1;
}
else
{
Statement 2;
}
Else statement cannot be used without if or no
multiple else statement are allowed with one if
statement. It means there must be an if
statement with in an else statement
Note:
n%2 == 0 means that the loop is true (i.e. should
run the code inside it) when the value of n is a
number that when divided by 2 has no
remainder, i.e. any even number.
Example:
//To check a number is even or odd
#include <stdio.h>
int main()
{
int x;
printf("enter a number:");
scanf("%d", &x);
if(x%2==0)
printf("even number");
else
printf("odd number");
}
Nesting of if …else statement
When an if else statement is present inside the body of another “if” or “else” then this is called
nested if else.
Syntax of Nested if else statement:
if(condition 1)
{
//Nested if else inside the body of "if"
if(condition2)
{
//Statements inside the body of nested "if"
}
else
{
//Statements inside the body of nested "else"
}
}
else
{
//Statements inside the body of "else"
}
Example:
#include <stdio.h>
int main()
{
int x, y;
printf("Input the value of x:");
scanf("%d", &x);
printf("Input the value of y:");
scanf("%d",&y);
if (x!=y)
{
printf("var1 is not equal to y\n");
//Nested if else
if (x > y)
{
printf("x is greater than y\n");
}
else
{
printf("y is greater than x\n");
}
}
else
{
printf("x is equal to y\n");
}
If….else LADDER
In this type of nesting there is an if else statement in every else
part except the last part. If condition is false control pass to block
where condition is again checked with its if statement.
if (condition1)
{
//These statements would execute if the condition1 is true
}
else if(condition2)
{
//These statements would execute if the condition2 is true
}
else if (condition3)
{
//These statements would execute if the condition3 is true
}
.
.
else
{
//These statements would execute if all the conditions return false.
}
This process continue until there is no if statement in the last block. if one of
the condition is satisfy the condition other nested “else if” would not executed.
But it has disadvantage over if else statement that, in if else statement
whenever the condition is true, other condition are not checked. While in this
case, all condition are checked.
Example: Create a program that will display an
output:
Input the value of x:12
Input the value of y:21
x is not equal to y
#include <stdio.h>
int main()
{
int x, y;
printf("Input the value of x:");
scanf("%d", &x);
printf("Input the value of y:");
scanf("%d",&y);
if (x !=y)
{
printf("x is not equal to y\n");
}
else if (x > y)
{
printf("x is greater than y\n");
}
else if (y > x)
{
printf("y is greater than x\n");
}
else
{
printf("x is equal to y\n");
}
}
As you can see that only the statements inside the body of “if” are executed.
This is because in this statement as soon as a condition is satisfied, the
statements inside that block are executed and rest of the blocks are ignored.
Important Points:
1. else and else..if are optional statements, a program having only “if”
statement would run fine.
2. else and else..if cannot be used without the “if”.
3. There can be any number of else..if statement in a if else..if block.
4. If none of the conditions are met then the statements in else block gets
executed.
5. Just like relational operators, we can also use logical operators such as
AND (&&), OR(||) and NOT(!).
Switch case statement
A switch statement allows a variable to be tested for equality against a list of
values. Each value is called a case, and the variable being switched on is checked
for each switch case.
Syntax:
switch(expression) {
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
int main () {
switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}