UNIT-2 STATEMENTS
A statement is a part of your program that can be executed. That is, a statement specifies an action. Statements
generally contain expressions and end with a semicolon.
Statements that are written individually are called Single/Simple statements. Statements that are written as a
block are called Block/Compound statements. A block begins with an open brace { and ends with a closing
brace }.
C categorizes statements into these groups:
Selection / Branch / Decision making / Conditional statements
Loop/ Iteration/ Repetitive statements
Jump / Control transfer statements
Expression Statements
Block statements/ compound statements
SELECTION STATEMENTS:
A selection statement checks the given condition and decides the execution of statements after the success or
failure of the condition. C supports two selection control statements if and switch.
1. If statement: The if statement is a decision making statement that allows the computer to evaluate an
expression (condition) first and depending on the result of the condition i.e. true or false, it transfers the control
to a particular statement.
The if statement has the following forms
a) Simple if Statement
b) if...else Statement
c) Nested if Statement
d) if..else Ladder Statement
Simple if Statement
The simple if statement contains only one condition, if the condition is true, it will execute the statements that
are present between opening and closing braces. Otherwise it will not execute those statements.
Syntax: Example:
if(condition) /* A program to check whether a number is Less than 100 or not */
Single-statement #include<stdio.h>
OR #include<conio.h>
if(condition) void main()
{ {
Statement-block int a;
} clrscr();
printf("enter an integer ");
scanf("%d",&a);
if(a<=10)
printf("%d is less than 100",a); getch();
}
if...else Statement
This statement is used to define two blocks of statements in order to execute only one block. If the condition is
true, the block of if is executed; otherwise, the block of else is executed.
Syntax: Example:
if (condition) Write a program to check whether the given number is a positive number or a
{ negative number
True Block Statements; #include<stdio.h>
------------- #include<conio.h>
} void main()
else {
{ int n;
False Block Statements; clrscr();
, ------------ printf("enter any number ");
} scanf("%d",&n);
if(n>=0)
{
printf("\n %d is a positive number",n);
}
else
{
printf("\n %d is a negative number",n);
}
getch();
}
Nested if –else Statement
When an if statement is placed in another if statement or in else statement, then it is called nested if statement.
The nested if-else is used when a series of decisions are involved. In a nested if-else, an else statement
always refers to the nearest if statement which is within the same block as the else and that is not already
associated with an else.
Syntax: Example:
if (condition) Write a program to finding greatest among three numbers
{ #include<stdio.h>
if(condition) #include<conio.h>
{ void main()
True Block Statements; {
------------- int a,b;
} clrscr();
} printf("enter any two number ");
else scanf("%d%d",&a,&b);
{ if(a>b)
False Block Statements; {
------------ if(a>c)
} printf(“ %d is greatest”,a);
else
printf(“ %d is greatest”,c);
}
else
{
if(b>c)
printf(“ %d is greatest”,b);
else
printf(“ %d is greatest”,c);
}
getch();
}
if..else Ladder Statement
The if-else ladder is used when multipath (multiple) decisions are involved.
A multipath decision is a chain of if-elses in which the statement associated with each else is an if-statement.
A statement is a part of your program that can be executed. That is, a statement specifies an action. Statements
generally contain expressions and end with a semicolon.
Statements that are written individually are called Single/Simple statements. Statements that are written as a
block are called Block/Compound statements. A block begins with an open brace { and ends with a closing
brace }.
C categorizes statements into these groups:
Selection / Branch / Decision making / Conditional statements
Loop/ Iteration/ Repetitive statements
Jump / Control transfer statements
Expression Statements
Block statements/ compound statements
SELECTION STATEMENTS:
A selection statement checks the given condition and decides the execution of statements after the success or
failure of the condition. C supports two selection control statements if and switch.
1. If statement: The if statement is a decision making statement that allows the computer to evaluate an
expression (condition) first and depending on the result of the condition i.e. true or false, it transfers the control
to a particular statement.
The if statement has the following forms
a) Simple if Statement
b) if...else Statement
c) Nested if Statement
d) if..else Ladder Statement
Simple if Statement
The simple if statement contains only one condition, if the condition is true, it will execute the statements that
are present between opening and closing braces. Otherwise it will not execute those statements.
Syntax: Example:
if(condition) /* A program to check whether a number is Less than 100 or not */
Single-statement #include<stdio.h>
OR #include<conio.h>
if(condition) void main()
{ {
Statement-block int a;
} clrscr();
printf("enter an integer ");
scanf("%d",&a);
if(a<=10)
printf("%d is less than 100",a); getch();
}
if...else Statement
This statement is used to define two blocks of statements in order to execute only one block. If the condition is
true, the block of if is executed; otherwise, the block of else is executed.
Syntax: Example:
if (condition) Write a program to check whether the given number is a positive number or a
{ negative number
True Block Statements; #include<stdio.h>
------------- #include<conio.h>
} void main()
else {
{ int n;
False Block Statements; clrscr();
, ------------ printf("enter any number ");
} scanf("%d",&n);
if(n>=0)
{
printf("\n %d is a positive number",n);
}
else
{
printf("\n %d is a negative number",n);
}
getch();
}
Nested if –else Statement
When an if statement is placed in another if statement or in else statement, then it is called nested if statement.
The nested if-else is used when a series of decisions are involved. In a nested if-else, an else statement
always refers to the nearest if statement which is within the same block as the else and that is not already
associated with an else.
Syntax: Example:
if (condition) Write a program to finding greatest among three numbers
{ #include<stdio.h>
if(condition) #include<conio.h>
{ void main()
True Block Statements; {
------------- int a,b;
} clrscr();
} printf("enter any two number ");
else scanf("%d%d",&a,&b);
{ if(a>b)
False Block Statements; {
------------ if(a>c)
} printf(“ %d is greatest”,a);
else
printf(“ %d is greatest”,c);
}
else
{
if(b>c)
printf(“ %d is greatest”,b);
else
printf(“ %d is greatest”,c);
}
getch();
}
if..else Ladder Statement
The if-else ladder is used when multipath (multiple) decisions are involved.
A multipath decision is a chain of if-elses in which the statement associated with each else is an if-statement.