Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The GNU C Programming Tutorial - Nested if statements

Node:Nested if statements, Next:, Previous:if... else..., Up:Decisions



Nested if statements

Consider the following two code examples. Their purposes are exactly the same.

int my_num = 3;

if ((my_num > 2) && (my_num < 4))
{
  printf ("my_num is three");
}

or:

int my_num =3;

if (my_num > 2)
{
  if (my_num < 4)
  {
    printf ("my_num is three");
  }
}

Both of these code examples have the same result, but they arrive at it in different ways. The first example, when translated into English, might read, "If my_num is greater than two and my_num is less than four (and my_num is an integer), then my_num has to be three." The second method is more complicated. In English, it can be read, "If my_num is greater than two, do what is in the first code block. Inside it, my_num is always greater than two; otherwise the program would never have arrived there. Now, if my_num is also less than four, then do what is inside the second code block. Inside that block, my_num is always less than four. We also know it is more than two, since the whole of the second test happens inside the block where that's true. So, assuming my_num is an integer, it must be three."

In short, there are two ways of making compound decisions in C. You make nested tests, or you can use the comparison operators &&, ||, and so on. In situations where sequences of comparison operators become too complex, nested tests are often a more attractive option.

Consider the following example:

if (i > 2)
{
  /* i is greater than 2 here! */
}
else
{
  /* i is less than or equal to 2 here! */
}

The code blocks in this example provide "safe zones" wherein you can rest assured that certain conditions hold. This enables you to think and code in a structured way.

You can nest if statements in multiple levels, as in the following example:

#include <stdio.h>

int main ()
{
  int grade;

  printf("Type in your grade: ");
  scanf ("%d", &grade);

  if (grade < 10)
  {
    printf ("Man, you're lame!  Just go away.\n");
  }
  else
  {
    if (grade < 65)
    {
      printf ("You failed.\n");
    }
    else
    {
      printf ("You passed!\n");
      if (grade >= 90)
      {
        printf ("And you got an A!\n");
      }
      else
      {
        printf ("But you didn't get an A.  Sorry.\n");
      }
    }
  }
  return 0;
}

 
 
  Published under free license. Design by Interspire