Jump to content




Recent Topics

Recent Status Updates

  • Photo
      15 Sep
    Error

    Programming is something that I enjoy and want to make a career out of. But, I usually tend to start things and not finish them. Any advice on how I can finish what I start?

    Show comments (1)
  • Photo
      12 Sep
    FacetiousTurtle33

    Just joined. Really enjoy this sight. Excited to become a great programmer, and helper.

    Show comments (3)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

If statement is ignored

if statement ignore

Best Answer KodeKool, 04 March 2013 - 04:58 AM

            if (varAge <= 12 && varAge >= 20)
            {
                Console.WriteLine("You're not a Teenager");
            }
            else
            {
                Console.WriteLine("You're a Teenager");
            }

your if statement is ignored because your checking to see if varAge is less than or equal to 12 AND varAge is greater than or equal to 20. change && (AND) to ||(OR) and your program should work just fine :)

 

i think it's also worth noting that this type of check is called an "outside the fence" check. meaning your checking for values outside of your range. another way to do it would obviously be called an "inside the fence" check. which consists of checking for values only within the range you want. an example using your code would be:

 

  if (varAge > 12 && varAge < 20)
  {
      Console.WriteLine("You're a Teenager");
  }
  else
  {
      Console.WriteLine("You're not a Teenager");
  }

 

basic instances such as this can be applied to most checks in most languages. :)

Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1 icandoit

icandoit

    CC Regular

  • Member
  • PipPipPip
  • 45 posts

Posted 04 March 2013 - 03:57 AM

help me guyz...can you tell me what's wrong with my code..
when I run this program the if statement is ignored...
for example if I write or input 25 the result is always "You're a Teenager"
only the else staement is executed...all answers will be appreciated THANK YOU and GOD BLESS....
 
 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace helloWorldApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please Enter your age: ");
            string age = Console.ReadLine();
            int varAge = int.Parse(age);
 
            if (varAge <= 12 && varAge >= 20)
            {
                Console.WriteLine("You're not a Teenager");
            }
            else
            {
                Console.WriteLine("You're a Teenager");
            }
 
        }
    }
}
 

 


Edited by Roger, 04 March 2013 - 03:47 PM.
tags


#2 0xDEADBEEF

0xDEADBEEF

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 724 posts

Posted 04 March 2013 - 04:27 AM

Your if statement says this:<br /><br />"If Age is less than or equal to 12 AND Age is 20 or more"<br /><br />not sure that's what you meant. Probably look there for the answer.

Creating SEGFAULTs since 1995.


#3 KodeKool

KodeKool

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 272 posts

Posted 04 March 2013 - 04:58 AM   Best Answer

            if (varAge <= 12 && varAge >= 20)
            {
                Console.WriteLine("You're not a Teenager");
            }
            else
            {
                Console.WriteLine("You're a Teenager");
            }

your if statement is ignored because your checking to see if varAge is less than or equal to 12 AND varAge is greater than or equal to 20. change && (AND) to ||(OR) and your program should work just fine :)

 

i think it's also worth noting that this type of check is called an "outside the fence" check. meaning your checking for values outside of your range. another way to do it would obviously be called an "inside the fence" check. which consists of checking for values only within the range you want. an example using your code would be:

 

  if (varAge > 12 && varAge < 20)
  {
      Console.WriteLine("You're a Teenager");
  }
  else
  {
      Console.WriteLine("You're not a Teenager");
  }

 

basic instances such as this can be applied to most checks in most languages. :)


Edited by KodeKool, 04 March 2013 - 06:02 AM.

~A program will always do what you tell it to do, and seldom what you want it to do~

Check out my latest PHP tutorial


#4 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,185 posts

Posted 04 March 2013 - 09:04 AM

It's just a simple boolean logic error.  We've all been there.


My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#5 PurityLake

PurityLake

    CC Regular

  • Member
  • PipPipPip
  • 36 posts

Posted 04 March 2013 - 11:06 AM

Generally nothing in a program is ignored in programming, except comments but as lespauled said, we all make logic errors at some stage and we sometimes become blind to little things like that



#6 icandoit

icandoit

    CC Regular

  • Member
  • PipPipPip
  • 45 posts

Posted 04 March 2013 - 08:35 PM

Thank you guyz...it helps alot....:D