Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Nested if-else statements...

Nested if-else statements...

Scheduled Pinned Locked Moved The Weird and The Wonderful
15 Posts 13 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Thomas Daniels

    I found this in one of my old codes (it was a program to check whether a square was a magic square[^] or not)

    private void button1_Click(object sender, EventArgs e)
    {
    int lefttop = Convert.ToInt16(_1.Text);
    int centertop = Convert.ToInt16(_2.Text);
    int righttop = Convert.ToInt16(_3.Text);
    int leftcenter = Convert.ToInt16(_4.Text);
    int centercenter = Convert.ToInt16(_5.Text);
    int rightcenter = Convert.ToInt16(_6.Text);
    int leftbottom = Convert.ToInt16(_7.Text);
    int centerbottom = Convert.ToInt16(_8.Text);
    int rightbottom = Convert.ToInt16(_9.Text);
    int row1 = lefttop + centertop + righttop;
    int row2 = leftcenter + centercenter + rightcenter;
    int row3 = leftbottom + centerbottom + rightbottom;
    int col1 = lefttop + leftcenter + leftbottom;
    int col2 = centertop + centercenter + centerbottom;
    int col3 = righttop + rightcenter + rightbottom;
    int dia1 = lefttop + centercenter + rightbottom;
    int dia2 = leftbottom + centercenter + righttop;
    if (row1 == row2)
    {
    if (row2 == row3)
    {
    if (row3 == col1)
    {
    if (col1 == col2)
    {
    if (col2 == col3)
    {
    if (col3 == dia1)
    {
    if (dia1 == dia2)
    {
    MessageBox.Show("Your magic square is correct!");
    }
    else
    {
    MessageBox.Show("Wrong! Application will restart!");
    this.Close();
    Process restart = new Process();
    restart.StartInfo.FileName = @"path name here";
    restart.Start();
    }
    }
    else
    {
    MessageBox.Show("Wrong! Application will restart!");
    this.Close();
    Process restart = new Process();
    restart.StartInfo.FileName = @"path name here";
    restart.Start();
    }
    }

    M Offline
    M Offline
    Marius ten Napel
    wrote on last edited by
    #6

    I don´t believe in if anymore :-D

    Z 1 Reply Last reply
    0
    • M Marius ten Napel

      I don´t believe in if anymore :-D

      Z Offline
      Z Offline
      ZurdoDev
      wrote on last edited by
      #7

      Quote:

      I don´t believe in if anymore

      It's not "if", it's "when." You have to think positive. :)

      There are only 10 types of people in the world, those who understand binary and those who don't.

      M 1 Reply Last reply
      0
      • Z ZurdoDev

        Quote:

        I don´t believe in if anymore

        It's not "if", it's "when." You have to think positive. :)

        There are only 10 types of people in the world, those who understand binary and those who don't.

        M Offline
        M Offline
        Marius ten Napel
        wrote on last edited by
        #8

        You're right! But, don`t just disregard this code, even Metallica says "nothing ELSE matters" :doh: So, try coding ELSE without the IF.

        D 1 Reply Last reply
        0
        • T Thomas Daniels

          I found this in one of my old codes (it was a program to check whether a square was a magic square[^] or not)

          private void button1_Click(object sender, EventArgs e)
          {
          int lefttop = Convert.ToInt16(_1.Text);
          int centertop = Convert.ToInt16(_2.Text);
          int righttop = Convert.ToInt16(_3.Text);
          int leftcenter = Convert.ToInt16(_4.Text);
          int centercenter = Convert.ToInt16(_5.Text);
          int rightcenter = Convert.ToInt16(_6.Text);
          int leftbottom = Convert.ToInt16(_7.Text);
          int centerbottom = Convert.ToInt16(_8.Text);
          int rightbottom = Convert.ToInt16(_9.Text);
          int row1 = lefttop + centertop + righttop;
          int row2 = leftcenter + centercenter + rightcenter;
          int row3 = leftbottom + centerbottom + rightbottom;
          int col1 = lefttop + leftcenter + leftbottom;
          int col2 = centertop + centercenter + centerbottom;
          int col3 = righttop + rightcenter + rightbottom;
          int dia1 = lefttop + centercenter + rightbottom;
          int dia2 = leftbottom + centercenter + righttop;
          if (row1 == row2)
          {
          if (row2 == row3)
          {
          if (row3 == col1)
          {
          if (col1 == col2)
          {
          if (col2 == col3)
          {
          if (col3 == dia1)
          {
          if (dia1 == dia2)
          {
          MessageBox.Show("Your magic square is correct!");
          }
          else
          {
          MessageBox.Show("Wrong! Application will restart!");
          this.Close();
          Process restart = new Process();
          restart.StartInfo.FileName = @"path name here";
          restart.Start();
          }
          }
          else
          {
          MessageBox.Show("Wrong! Application will restart!");
          this.Close();
          Process restart = new Process();
          restart.StartInfo.FileName = @"path name here";
          restart.Start();
          }
          }

          N Offline
          N Offline
          NAANsoft
          wrote on last edited by
          #9

          Wonderfull! Even to the point of paraphrasing the old BASIC error message "?Redo from start" - and so that we really can see it, duplicate it several times. Nice ;P

          1 Reply Last reply
          0
          • M Marius ten Napel

            You're right! But, don`t just disregard this code, even Metallica says "nothing ELSE matters" :doh: So, try coding ELSE without the IF.

            D Offline
            D Offline
            dusty_dex
            wrote on last edited by
            #10

            How about SOMETHING ELSE? ;P

            "It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.

            1 Reply Last reply
            0
            • T Thomas Daniels

              I found this in one of my old codes (it was a program to check whether a square was a magic square[^] or not)

              private void button1_Click(object sender, EventArgs e)
              {
              int lefttop = Convert.ToInt16(_1.Text);
              int centertop = Convert.ToInt16(_2.Text);
              int righttop = Convert.ToInt16(_3.Text);
              int leftcenter = Convert.ToInt16(_4.Text);
              int centercenter = Convert.ToInt16(_5.Text);
              int rightcenter = Convert.ToInt16(_6.Text);
              int leftbottom = Convert.ToInt16(_7.Text);
              int centerbottom = Convert.ToInt16(_8.Text);
              int rightbottom = Convert.ToInt16(_9.Text);
              int row1 = lefttop + centertop + righttop;
              int row2 = leftcenter + centercenter + rightcenter;
              int row3 = leftbottom + centerbottom + rightbottom;
              int col1 = lefttop + leftcenter + leftbottom;
              int col2 = centertop + centercenter + centerbottom;
              int col3 = righttop + rightcenter + rightbottom;
              int dia1 = lefttop + centercenter + rightbottom;
              int dia2 = leftbottom + centercenter + righttop;
              if (row1 == row2)
              {
              if (row2 == row3)
              {
              if (row3 == col1)
              {
              if (col1 == col2)
              {
              if (col2 == col3)
              {
              if (col3 == dia1)
              {
              if (dia1 == dia2)
              {
              MessageBox.Show("Your magic square is correct!");
              }
              else
              {
              MessageBox.Show("Wrong! Application will restart!");
              this.Close();
              Process restart = new Process();
              restart.StartInfo.FileName = @"path name here";
              restart.Start();
              }
              }
              else
              {
              MessageBox.Show("Wrong! Application will restart!");
              this.Close();
              Process restart = new Process();
              restart.StartInfo.FileName = @"path name here";
              restart.Start();
              }
              }

              L Offline
              L Offline
              Lee Chetwynd
              wrote on last edited by
              #11

              At least it's easy to see what its doing if nothing else!

              1 Reply Last reply
              0
              • T Thomas Daniels

                I found this in one of my old codes (it was a program to check whether a square was a magic square[^] or not)

                private void button1_Click(object sender, EventArgs e)
                {
                int lefttop = Convert.ToInt16(_1.Text);
                int centertop = Convert.ToInt16(_2.Text);
                int righttop = Convert.ToInt16(_3.Text);
                int leftcenter = Convert.ToInt16(_4.Text);
                int centercenter = Convert.ToInt16(_5.Text);
                int rightcenter = Convert.ToInt16(_6.Text);
                int leftbottom = Convert.ToInt16(_7.Text);
                int centerbottom = Convert.ToInt16(_8.Text);
                int rightbottom = Convert.ToInt16(_9.Text);
                int row1 = lefttop + centertop + righttop;
                int row2 = leftcenter + centercenter + rightcenter;
                int row3 = leftbottom + centerbottom + rightbottom;
                int col1 = lefttop + leftcenter + leftbottom;
                int col2 = centertop + centercenter + centerbottom;
                int col3 = righttop + rightcenter + rightbottom;
                int dia1 = lefttop + centercenter + rightbottom;
                int dia2 = leftbottom + centercenter + righttop;
                if (row1 == row2)
                {
                if (row2 == row3)
                {
                if (row3 == col1)
                {
                if (col1 == col2)
                {
                if (col2 == col3)
                {
                if (col3 == dia1)
                {
                if (dia1 == dia2)
                {
                MessageBox.Show("Your magic square is correct!");
                }
                else
                {
                MessageBox.Show("Wrong! Application will restart!");
                this.Close();
                Process restart = new Process();
                restart.StartInfo.FileName = @"path name here";
                restart.Start();
                }
                }
                else
                {
                MessageBox.Show("Wrong! Application will restart!");
                this.Close();
                Process restart = new Process();
                restart.StartInfo.FileName = @"path name here";
                restart.Start();
                }
                }

                R Offline
                R Offline
                RafagaX
                wrote on last edited by
                #12

                Aaaah!, it makes me remember some of my early programs...

                CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...

                1 Reply Last reply
                0
                • T Thomas Daniels

                  I found this in one of my old codes (it was a program to check whether a square was a magic square[^] or not)

                  private void button1_Click(object sender, EventArgs e)
                  {
                  int lefttop = Convert.ToInt16(_1.Text);
                  int centertop = Convert.ToInt16(_2.Text);
                  int righttop = Convert.ToInt16(_3.Text);
                  int leftcenter = Convert.ToInt16(_4.Text);
                  int centercenter = Convert.ToInt16(_5.Text);
                  int rightcenter = Convert.ToInt16(_6.Text);
                  int leftbottom = Convert.ToInt16(_7.Text);
                  int centerbottom = Convert.ToInt16(_8.Text);
                  int rightbottom = Convert.ToInt16(_9.Text);
                  int row1 = lefttop + centertop + righttop;
                  int row2 = leftcenter + centercenter + rightcenter;
                  int row3 = leftbottom + centerbottom + rightbottom;
                  int col1 = lefttop + leftcenter + leftbottom;
                  int col2 = centertop + centercenter + centerbottom;
                  int col3 = righttop + rightcenter + rightbottom;
                  int dia1 = lefttop + centercenter + rightbottom;
                  int dia2 = leftbottom + centercenter + righttop;
                  if (row1 == row2)
                  {
                  if (row2 == row3)
                  {
                  if (row3 == col1)
                  {
                  if (col1 == col2)
                  {
                  if (col2 == col3)
                  {
                  if (col3 == dia1)
                  {
                  if (dia1 == dia2)
                  {
                  MessageBox.Show("Your magic square is correct!");
                  }
                  else
                  {
                  MessageBox.Show("Wrong! Application will restart!");
                  this.Close();
                  Process restart = new Process();
                  restart.StartInfo.FileName = @"path name here";
                  restart.Start();
                  }
                  }
                  else
                  {
                  MessageBox.Show("Wrong! Application will restart!");
                  this.Close();
                  Process restart = new Process();
                  restart.StartInfo.FileName = @"path name here";
                  restart.Start();
                  }
                  }

                  T Offline
                  T Offline
                  tumbledDown2earth
                  wrote on last edited by
                  #13

                  OMG .. reminds me of some old code base I had to maintain some 4 yrs back ... Once we tried counting the cyclometric complexity of the code and got a headOverfolowException On a more serious note there are many situations when coders care little about how others see it ... They just fall into a loop of get-it-done-yesterday (by one of the zillion managers who think they are God) and the result is just infront of us :)

                  1 Reply Last reply
                  0
                  • T Thomas Daniels

                    I found this in one of my old codes (it was a program to check whether a square was a magic square[^] or not)

                    private void button1_Click(object sender, EventArgs e)
                    {
                    int lefttop = Convert.ToInt16(_1.Text);
                    int centertop = Convert.ToInt16(_2.Text);
                    int righttop = Convert.ToInt16(_3.Text);
                    int leftcenter = Convert.ToInt16(_4.Text);
                    int centercenter = Convert.ToInt16(_5.Text);
                    int rightcenter = Convert.ToInt16(_6.Text);
                    int leftbottom = Convert.ToInt16(_7.Text);
                    int centerbottom = Convert.ToInt16(_8.Text);
                    int rightbottom = Convert.ToInt16(_9.Text);
                    int row1 = lefttop + centertop + righttop;
                    int row2 = leftcenter + centercenter + rightcenter;
                    int row3 = leftbottom + centerbottom + rightbottom;
                    int col1 = lefttop + leftcenter + leftbottom;
                    int col2 = centertop + centercenter + centerbottom;
                    int col3 = righttop + rightcenter + rightbottom;
                    int dia1 = lefttop + centercenter + rightbottom;
                    int dia2 = leftbottom + centercenter + righttop;
                    if (row1 == row2)
                    {
                    if (row2 == row3)
                    {
                    if (row3 == col1)
                    {
                    if (col1 == col2)
                    {
                    if (col2 == col3)
                    {
                    if (col3 == dia1)
                    {
                    if (dia1 == dia2)
                    {
                    MessageBox.Show("Your magic square is correct!");
                    }
                    else
                    {
                    MessageBox.Show("Wrong! Application will restart!");
                    this.Close();
                    Process restart = new Process();
                    restart.StartInfo.FileName = @"path name here";
                    restart.Start();
                    }
                    }
                    else
                    {
                    MessageBox.Show("Wrong! Application will restart!");
                    this.Close();
                    Process restart = new Process();
                    restart.StartInfo.FileName = @"path name here";
                    restart.Start();
                    }
                    }

                    F Offline
                    F Offline
                    fulloflove
                    wrote on last edited by
                    #14

                    A right/wrong flag will make the code simpler. But I think you should place all the values in 2 arrays and use memcmp (or similar functions)

                    F 1 Reply Last reply
                    0
                    • F fulloflove

                      A right/wrong flag will make the code simpler. But I think you should place all the values in 2 arrays and use memcmp (or similar functions)

                      F Offline
                      F Offline
                      Filipe Moreira de Oliveira
                      wrote on last edited by
                      #15

                      There're lots of ways to optimise this code (without a right/wrong flags, there are more elegant solutions) but I don't think it's thread author's intention to get a code review... How can we code so ugly when we're fresh eh? :laugh: It's so funny to review our old codes. These days I found a code of mine written in C# and using a goto, WTF? :omg: *palmface*

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups