6 Coding Puzzles Teach You How to Read IF statements

[vc_row][vc_column width=”1/1″][vc_single_image image=”9081″ border_color=”grey” img_link_large=”” img_link_target=”_self” img_size=”150×225″ alignment=”center”][vc_column_text]This blog continues our series of C# coding brainteasers, which teach you how to read code without compiling it. This blog will provide 6 puzzles on IF statements. The puzzles will help you learn practical programming skills.

 

ifelseThis is how an IF statement works.  An IF statement runs code based on a test. This test results in true or false. When it results in true on or more statements run. Optionally alternative statements can run if the test results in false. IF statements are also called conditional statements, since code is executed based on value of the condition (test). IF statements are frequently used in all levels of programming.

 

Try to solve these 6 IF statements with your C# or other coding knowledge. Just predict the output without executing the code. If you need some help, check our C# course.  Write your answer in the comments below. Or add your own puzzle.[/vc_column_text][/vc_column][vc_column][vc_column_text]
1).  

int t = 32;

      if (t <= 32)
      {
         Console.WriteLine(“Warning!”);
         if (t == 32)
         {
             Console.WriteLine(“Freezing”);
         }
         else
         {
            Console.WriteLine(“Black ice {0}”, t);
         }
      }
      Console.ReadLine();

2).

   int[] a = {5, 3, 2 };
      int m = a[0];
      if (m < a[1])
      {
          m = a[1];
      }
      if (m < a[2])
      {
          m = a[2];
      }
      Console.WriteLine(m);
      Console.ReadLine();

3).

int i = 10, j = 20;
      if (i <= j)
      {
          if (j / 2 >= i)
          {
              Console.WriteLine(j / 2);
              if (i * 2 < j)
              {
                  Console.WriteLine(j / i);
              }
          }
      }
      Console.ReadLine();

4).

    int k = -10, l = 30;
      if (k <= l)
      {
          if (l >= -k)
          {
              Console.WriteLine(k+i);
              if (k+i <= 20)
              {
                  Console.WriteLine(l / k);
              }
          }
      }
      Console.ReadLine();

5).

  float r = 0.5f, s = -2.5f;
      if (r >= s)
      {
          if (s < -r)
          {
              Console.WriteLine(s*r);
              if (5 * r == -s)
              {
                  if (s < r)
                  {
                      Console.WriteLine(s /r);
                  }
              }
          }
      }
      Console.ReadLine();

6).

What is wrong with this syntax?
      int[] b = { 5, 3, 2 }
      int p = a[0];
      if (p < a[1])
      {
          p = a[1];
      }
      if p < a[2]
      {
          p = a(2);
      }
      Console.WriteLine(m);
      Console.ReadLine();

[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/1″][vc_single_image image=”9082″ border_color=”grey” img_link_large=”” img_link_target=”_self” img_size=”300×385″][/vc_column][/vc_row][vc_row][vc_column width=”1/2″][vc_button title=”Core C# features tutorial” target=”_self” color=”btn-warning” icon=”none” size=”wpb_regularsize” href=”http://www.talk-it.biz/tutorial-links/core-c-features/”][/vc_column][vc_column width=”1/2″][vc_button title=”More coding puzzles ” target=”_self” color=”btn-primary” icon=”none” size=”wpb_regularsize” href=”http://www.talk-it.biz/2015/03/coding-puzzles-loops/”][/vc_column][/vc_row][vc_row][vc_column width=”1/2″][vc_column_text]Write your answers in the comments below and Share it. If you struggle just ask questions bellow and we will help you.[/vc_column_text][/vc_column][vc_column width=”1/2″][vc_googleplus annotation=”bubble”][vc_tweetmeme type=”horizontal”][/vc_column][/vc_row][vc_row][vc_column width=”1/1″][vc_column_text]

Many thanks to,

1) Microsoft

2). Make a Meme

3). Keepcalm-o-matic

Artiom Jankovskij

2015

[/vc_column_text][/vc_column][/vc_row]

3 thoughts on “6 Coding Puzzles Teach You How to Read IF statements”

  1. 1. Warning Freezing
    2. 5
    3. 10
    4. -9 -3
    5. -1.25 5
    6. Array a and value m is not defined.

  2. Yes technical interviews often present coding tasks or puzzles. These will test your skill under pressure.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top