5.13 – Summarizing decision flow statements

by subbu on September 25, 2013

  • Simple if selection statement is used to check multiple conditions, out of which any number of conditions may be true.
  • if-else selection statement is used to solve a problem which has only two conditions to check, out of which only a single condition must be true.
  • else-if ladder is used to check multiple conditions, out of which only a single or no condition must be true.
  • The default scope of any selection statement is only one statement, to control more statements we need to write in a pair of parenthesis.

Take care while comparing different types:

Variables, constants or expressions of same type can be properly compared in conditional statements but, may go wrong while comparing operands of different types. Here we will see some practical cases and rectify using explicit casting.

Case 1:

#include<stdio.h>
int main()
{
 float x=0.7;
 if(0.7>x)
  printf("Hello");
 else
  printf("World");
 return 0;
}

Output:
Hello

This is the output, we have never expected because 0.7 can never be greater than 0.7 (x). The reason hear is, by default the C compiler treats any real constant as double type, the binary equivalent to 0.7 is recurring and its binary up to double precision is greater than the single precision of 0.7 (float).

It can be rectified by explicitly casting double type 0.7 to float type either by using type cast operator or suffix “f”

#include<stdio.h>
int main()
{
 float x=0.7;
 if((float)0.7>x)         /* type casting */
   printf("Hello");
 else
   printf("World");
 return 0;
}

Output:
World

Case 2:

#include<stdio.h>
int main()
{
 short a=-5;
 unsigned short b=-5u;  /* assigns 65531 due to underflow */
 if(a==b)
  printf("Hello");
 else
  printf("World");
 return 0;
}

Output:
World

In the above example though the same value -5 is assigned to both the variables a and b, it is proved that they are not equal. The reason is that, there is a difference in their types. Assigning -5 to unsigned short results underflow and it would be 65531 [65535 – (-5 -0) +1]. Comparing -5 with 65531 results false

It can be rectified by explicitly converting unsigned short into signed short using type cast operator while comparing

#include<stdio.h>
int main()
{
 short a=-5;
 unsigned short b=-5u;  /* assigns 65531 due to underflow */
 if(a==(signed short)b)
  printf("Hello");
 else
  printf("World");
 return 0;
}

Output:
Hello

Points to remember:

1. In C language 0 is considered as false and any non-zero is considered as true.

#include<stdio.h>
int main()
{
if(245)
  printf("One");
else
  printf("Two");
printf("\n");
if(-120)
  printf("Three");
else
  printf("Four");
printf("\n");
if(0)
  printf("Five");
else
  printf("Six");
return 0;
}

Output:
One
Three
Six

2. If the “if conditional statement” is terminated with a semicolon then it can’t control the statement or block of statements under the conditional statement. It is generally known as null statement

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

Output:
One
Three
Four

Example explained:
Though (80<20) is false, it couldn’t control printf(“\nThree”); because conditional statement if(80<20) is terminated with a semicolon.

3. Assigning operator (=) is different from comparison operator(==).

#include<stdio.h>
int main()
{
int x=0,y=7;
if(y==1)
  printf("Hello");
if(x=5)
  printf("World");
return 0;
}

Output:
World

Example explained:
(y==1) gives false hence printf(“Hello”); is not executed.
x=5 assigns 5 to x and if(5) is true hence printf(“World”); is executed.

4. Two nearby floating point constants can’t be compared because exact value may not be compared.

#include<stdio.h>
int main()
{
float x=0.4,y=0.7;
if(x<0.4)
  printf("Hello");
if(y<0.7)
  printf("World");
return 0;
}

Output:
World

Example explained:
0.4 is stored accurately into “x” hence (x<0.4) gives false and printf(“Hello”); is not executed.
0.7 gives a recurring binary; storage differs from float to double. y<0.7 gives true because recurring binary stored in double is bigger than the recurring binary stored in float (in C language a floating point constant is by default double type).


Quiz 1:
1) What would be the output of following program?

#include<stdio.h>
int main()
{
int x,y,z;
x=25;
if(x>=50)
   y=450;
   z=500;
printf("%d\n%d\n%d",x,y,z);
return 0;
}

2) What would be the output of following program?

#include<stdio.h>
int main()
{
if(12.45)
  printf("Hello");
else
  printf("World");
return 0;
}

3) What would be the output of following program?

#include<stdio.h>
int main()
{
int a=10,b=40;
if(a>b);
  printf("%d\n%d",a,b);
return 0;
}

4) What would be the output of following program?

#include<stdio.h>
int main()
{
if(40>80);
  printf("Hello");
else
  printf("World");
return 0;
}

5) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=7;
float y=7.0;
if(x==y)
  printf("Hello");
else
  printf("World");
return 0;
}
Show Answers
1) 25
4203559     //garbage value because y is not assigned with any value
500

2) Hello       // a non-zero is considered as true

3) 10         // conditional statement is terminated with ;
40

4) Error       // else without if because if is terminated with ;

5) Hello       // properly compared


Quiz 2:
1) What would be the output of following program?

#include<stdio.h>
int main()
{
float x=0.7;
double y=0.7;
if(x==y)
  printf("Hello");
else
  printf("World");
return 0;
}

2) What would be the output of following program?

#include<stdio.h>
int main()
{
char ch='A';
int dh=65;
if(ch==dh)
  printf("Hello");
else
  printf("World");
return 0;
}

3) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=54;
printf("%d\t%d\t%d",x==54,x=70,x<=100);
return 0;
}

4) What would be the output of following program?

#include<stdio.h>
int main()
{
short x=32769;
if(x==-32767)
  printf("Hello");
else
  printf("World");
return 0;
}

5) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=135;
if(!x==0)
  printf("Hello");
else
  printf("World");
return 0;
}
Show Answers
1) World      // recurring binary in float differs from double

2) Hello       // ASCII of ‘A’ is equal to 65

3) 0       70      1 //The printf evaluates from right to left

4) Hello      // overflow of 32769 results -32767

5) Hello    //!135 !true  false (0)


Quiz 3:
1) What would be the output of following output?

#include<stdio.h>
int main()
{
int a=5,b=4;
if(a=4&&a==b)
  printf("Hello");
else
  printf("World");
return 0;
}

2) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=-1,y=0,z=2,a;
a=x+2==z-1&&z-1>x+1||z!=y+1;
printf("%d",a);
return 0;
}

3) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=2,y=0,z=1;
if(!x==y!=!z)
  printf("Hello");
else
  printf("World");
return 0;
}

4) What would be output of following program?

#include<stdio.h>
int main()
{
int x=10,y=10,z=10;
if(x==y==z)
  printf("Equal");
else
  printf("Not equal");
return 0;
}

5) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=5,y=-4;
if(!x+!y*7)
  printf("Hello");
else
  printf("World");
return 0;
}
Show Answers
1) World     // a==b executes and gives false first

2) 1           //Priority of operators is arithmetic, >, !=, ==, && and ||

3) Hello      //!x==y!=0    0==0!=0    1!=0    true

4) Not equal //10==10==z  1==10  false

5) World      //!x+0*7  → 0+0*7 →  0+0   false

Previous post:

Next post: