You are on page 1of 7

Statements

A statement is a command given to the computer that instructs the computer to take a specific
action, such as display to the screen, or collect input. A computer program is made up of a series
of statements.

The statements of a C program control the flow of program execution. In C, there are several
kinds of statements available to perform loops, to select other statements to be executed, and to
transfer control.

Selection Statements
Based upon the outcome of a particular condition, selection statements are used to transfer
control from one point to another. Selection statements select a statement to be executed among
a set of various statements. Following are the selection statements available is C:

if statement
if-else statement
switch statement

if statement
Syntax:

The syntax of an if statement in C programming language is:

if(expression) // if header
{ // if body starts here
/* statement(s)will execute
if the expression is true */
} // if body starts here
If the expression in header evaluates to true, then the block of code inside the if body will be
executed. If expression in header evaluates to false, then the first set of code after the end of the
if body (after the closing curly brace) will be executed. The expression written in if header is
known as condition.

C programming language assumes any non-zero and non-null values as true and if it is
either zero or null, then it is assumed as false value.

1
Flow Diagram

Example 1
#include <stdio.h>
int main()
{
int a = 10;
if( a < 20 )
{
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
Output
a is less than 20
value of a is : 10
Example 2
#include <stdio.h>
int main()
{
int num;
2
printf("Enter a number to check.\n");
scanf("%d",&num);
if(num<0) // checking whether number is less than 0 or not
{
printf("Number = %d\n",num);
}
/*If test condition is true, statement above will be executed, otherwise it
will not be executed */
printf("Example of if statement in C programming");
return 0;
}
Output 1
Enter a number to check.
-2
Number = -2
Example of if statement in C programming
Output 2
Enter a number to check.
5
Example of if statement in C programming
When the user enters 5 then, the test expression (num<0) becomes false. So, the statement/s
inside body of if is skipped and only the statement below it is executed.

if-else statement
An if statement can be followed by an optional else statement, which executes when condition
becomes false.

Syntax

if(expression)
{
/* statement(s) will execute if the expression is true */
}
else
{

3
/* statement(s) will execute if the expression is false */
}
If the expression evaluates to true, then the if block of code will be executed, otherwise else block
of code will be executed.

Flow Diagram

Example
#include <stdio.h>
int main()
{
int a = 100;
if( a < 20 )
{
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
else
{
/* if condition is false then print the following */
printf("a is not less than 20\n" );
}
printf("value of a is : %d\n", a);

return 0;
4
}
Output
a is not less than 20;
value of a is : 100
Example 2
#include <stdio.h>
int main()
{
int num;
printf("Enter a number you want to check.\n");
scanf("%d",&num);
if((num%2)==0) //checking whether remainder is 0 or not.
printf("%d is even.",num);
else
printf("%d is odd.",num);
return 0;
}
Output 1
Enter a number you want to check.
25
25 is odd.
Output 2
Enter a number you want to check.
2
2 is even.

Nested if statements
If the body of the if statement is another if statement or contains another if statement, then we
say that ifs are nested and the statement is known as nested if statement.

Syntax
The syntax for a nested if statement is as follows:

if (condition 1)
5
if (condition 2)
or

if( condition 1)
{
// Executes when the condition1 is true
if(condition2)
{
// Executes when the condition2 is true
}
}
Example
#include <stdio.h>
int main ()
{
int a = 100;
int b = 200;
/* check the boolean condition */
if( a == 100 )
{
/* if condition is true then check the following */
if( b == 200 )
{
/* if condition is true then print the following */
printf("Value of a is 100 and b is 200\n" );
}
}
printf("Exact value of a is : %d\n", a );
printf("Exact value of b is : %d\n", b );

return 0;
}
When the above code is compiled and executed, it produces the following result:

6
Value of a is 100 and b is 200
Exact value of a is : 100
Exact value of b is : 200

You might also like