Monday, May 29, 2017

How to avoid silly mistake in if-else statement using proper indentation.

  1 //This program shows why indentation is important for better clarity of the code.
  2 //Here if any "if" condition fails then it will only execute it's respective else statement. Indentation helps us here to easily identify the respective else statement for each if statement or a block of code.
  3
  4
  5 #include <stdio.h>
  6
  7 int main()
  8 {
  9         if (2 > 1)
 10                 if (3 < 2)
 11                         if (4 > 3)
 12                                 if (5 > 4) {
 13                                         printf("Here we are\n");
 14                                         printf("Another print here\n");
 15                                 }
 16                                 else
 17                                         printf("last else\n");
 18                         else
 19                                 printf("Third else\n");
 20                 else
 21                         printf("Second else\n");
 22         else
 23                 printf("First else\n");
 24
 25
 26         return (0);
 27 }

No comments:

Post a Comment

Base and Bounds, Segmentation

Base & bounds relocation: Two hardware registers: base address for process, bounds register that indicates the last valid address t...