5.2 – if selection statement

by subbu on September 14, 2013

if selection statement:

  • It is a fundamental and basic decision selective statement. It is used to select or skip a statement or block of statements according to the value given by the condition.
  • The condition is placed in a pair of parenthesis with the keyword if called conditional statement.
  • A statement or a block of statements are placed under the conditional statement.
  • The statement or block of statements is executed if the condition gives true otherwise skipped from the execution.

if decision selective statement

Let me explain with a simple example

if(10<20)
  printf("John");
if(40>80)
  printf("Smith");
printf("\nCodingFox");

In the above example, the condition 10<20 in the first selection statement  gives true (1), hence the statement under the conditional statement printf(“John”); will be selected to execute
The condition 40>80 in the second selection statement gives false (0), hence the statement printf(“Smith”); under the conditional statement will be skipped from the execution.
printf(“CodingFox”); will be executed as an independent statement because it is not controlled by any selection statement.

Note: The if conditional statement must not be terminated with a semicolon.

Example:

#include<stdio.h>
int main()
{
printf("Zero");
if(10<20)
  printf("\nOne");
if(10>40)
  printf("\nTwo");
if(1)
  printf("\nThree");
if(0)
  printf("\nFour");
printf("\nFive");
return 0;
}

Output:
Zero
One
Three
Five

Example explained:
printf(“Zero”); is executed as a normal statement.
printf(“\nOne”); is executed because 10<20 gives true (1)
printf(“\nTwo”); is skipped from execution because 10>40 gives false (0)
printf(“\nThree”); is executed because 1 is considered as true
printf(“\nFour”); is not executed because 0 is considered as false
printf(“\nFive”); is executed as a normal statement.

Another way of writing if selection statement:

If we want to control a single statement using if selection statement then we can write the statement either below or beside the conditional statement.

Example:

#include<stdio.h>
int main()
{
int n;
printf("Enter any integer:");
scanf("%d",&n);
if(n==0)printf("Zero");
if(n!=0)prinf("Not a zero");
return 0;
}

Execution 1:
Enter any integer: 45
Not a zero
Execution 2:
Enter any integer: 0
Zero

Example explained:
When 45 is the input to the program then the first condition gives false and second the condition gives true, hence “Not a zero” is printed.
When 0 is the input to the program then the first condition gives true and second the condition gives false, hence “zero” is printed.

Controlling multiple statements (block):

Generally if selection statement controls only one statement under the conditional statement. If we want to control multiple statements then statements must be placed in a pair of braces {} called block of statements. So that the total block is executed if the condition is true otherwise the total block is skipped from the execution.

How to define a block

Example:

#include<stdio.h>
int main()
{
int a=40,b=30;
if(b>a) /* false */
    printf("Alpha");/* Controlled by if */
printf("\nBeta");/* Not controlled by if */
if(a==b) /*false */
{
    printf("\nCharlie");/* Controlled by if */
    printf("\nDelta");/* Controlled by if */
}
printf("\nEcho");/* Not controlled by if */
return 0;
}

Output:
Beta
Echo

Using bool type in if selection statement:

In C language 1 is used in place of true and 0 is used in place of false. In case of C99 or C11 complaint compilers we can directly use true and false as logical constants, but stdbool.h must be included.

Example:

#include<stdio.h>
#include<stdbool.h>
int main()
{
bool flag=false;
if(true)/*using constant*/
  printf("First"); /*will be selected*/
if(flag)/*using variable*/
  printf("Second"); /*will be skipped */
return 0;
}

Output:
First

Note: Execute under MinGW gcc

Previous post:

Next post: