Control Statement

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Control statement

The flow of program control is specified by control statements in C.


It includes the following:

if statement
Statement execute set of command like when condition is true
syntax

if (condition) {
statement/body;
}

The statement is executed only when condition is true. If the if


statement body is consists of several statement then better to use
pair of curly braces. Here in case condition is false then compiler
skip the line within the if block.
Note: Note: %d is used to represent a part of memory as an integer.

Example (if statement)

#include <stdio.h>

int main()
{
int n=12;

printf( "enter a number:");


scanf("%d" ,&n);

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.

Syntax of else..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 */

/* you can have any number of case statements */


default : /* Optional */
statement(s);
The following rules apply to a switch statement −
• The expression used in a switch statement must have an integral or
enumerated type, or be of a class type in which the class has a single
conversion function to an integral or enumerated type.
• You can have any number of case statements within a switch. Each case is
followed by the value to be compared to and a colon.
• The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a constant or a literal.
• When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached.
• When a break statement is reached, the switch terminates, and the flow of
control jumps to the next line following the switch statement.
• Not every case needs to contain a break. If no break appears, the flow of
control will fall through to subsequent cases until a break is reached.
• A switch statement can have an optional default case, which must appear at
the end of the switch. The default case can be used for performing a task
when none of the cases is true. No break is needed in the default case.
Example:
#include <stdio.h>

int main () {

/* local variable definition */


char grade = 'B';

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" );
}

printf("Your grade is %c\n", grade );

You might also like