Jump to content




Recent Status Updates

  • Photo
      15 Nov
    duzamucha

    Hi, I am final year Interior Design Student from University of Huddersfield. I am currently working on my final major project which is going to be linked to coding. I was hoping that you could help me with my research. I have prepared a short survey, it would be a massive help if you could fill it in for me. It takes less than 2 minutes to complete, I promise. Here is the link: https://www.surveymonkey.com/s/73XLJKK Thank you so much in advance!

View All Updates

Developed by TechBiz Xccelerator
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
  • 751 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
  • 273 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,229 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






Powered by binpress