-7

If I run the following code,

while(true)
{
    if(true)
    {
        Console.WriteLine("True");
    }
    else
    {
        Console.WriteLine("False")
    }
}

How reliable is it that the Console will print "True" ? Will if ever print "False" ? Why ?

Edit: I had no idea that physical components of a computer were so perfect.

theUser
  • 1,346
  • 3
  • 21
  • 40
  • What use would a computer be if `if()` were random? Can you explain the actual problem? – CodeCaster Oct 09 '14 at 10:28
  • 1
    An if statement identifies which statement to run based on the value of a Boolean expression. In the example, the Boolean variable result is set to true and then checked in the if statement. – arin1405 Oct 09 '14 at 10:28
  • @CodeCaster he would rather make condescending comments than expand – David Pilkington Oct 09 '14 at 10:34
  • 1
    @DavidPilkington I think OP is genuinely interested. – CodeCaster Oct 09 '14 at 10:36
  • 1
    I am. From the answers it looks like this code would never print false. Ever. If so, that's great. I thought maybe once in every billion loops it might get it wrong. Due to some factor I didn't know, like heat or CPU being imperfect. – theUser Oct 09 '14 at 10:38
  • 2
    In that case, yes, random results _do_ occur. For example due to a damaged CPU, faulty memory or overheating. This will usually pretty soon halt your machine, as not only your application's state and logic will be corrupted but also that of the operating system. – CodeCaster Oct 09 '14 at 10:43
  • The vast majority of "wrong" answers from computers are programming mistakes by us silly humans. – David Crowell Oct 09 '14 at 15:12

5 Answers5

1

The question is a bit more interesting than downvoting peple think.

Sure, that conditional logic is really perfect. Visual studio generates a warning: Unreachable code detected, so the else branch will never ever be executed.

Even in case of hardware fault, computer / operating system / program itself is more likely to crash than the second brunch to execute.

But it does not mean that "False" will not be printed. String literals in dotnet normally are interned. Look at this hack:

static unsafe void Main(string[] args)
{
    // "True" is interned now
    string s = "True";

    // unsafe access to the interned string
    fixed (char* h = s)
    {
        // We have to change the length of the string.
        // The length is stored as integer value right before the first char
        ((int*)h)[-1] = 5;

        // Next operations change content of interned string
        h[0] = 'F';
        h[1] = 'a';
        h[2] = 'l';
        h[3] = 's';

        // But next one is dangerous - they can damage memory contents 
        h[4] = 'e';
    }

    // the most reliable code ever:)
    while (true)
    {
        if (true)
        {
            Console.WriteLine("True");
        }
        else
        {
            Console.WriteLine("False");
        }
    }
}

The else branch is not executed, which can be ensured with debugger, so the conditional logic is still consistent. However the program prints "False" even through the System.Console's method.

This trick requres enabling the unsafe context in the compiler options, moreover, it can damage process's memory.

Diligent Key Presser
  • 4,183
  • 4
  • 26
  • 34
1

I'd say reliable enough that you don't have to worry about it, but not perfect.

For example there is a slight chance that the bit that says true is flipped to false (cosmic rays interference, faulty ram, bad luck..) and you're program would then write False. These types of errors are called soft errors.

Studies by IBM in the 1990s suggest that computers typically experience about one cosmic-ray-induced error per 256 megabytes of RAM per month.[1]

Note that most modern compilers would remove your if branch due to optimization, thus further reducing chance :).

And of course memory is not the only thing that could go wrong. CPU could probably make mistake when evaluating compare instruction, hard drive might store the program itself wrongly, OS could fail to protect memory of your process from some other faulty process, etc.. All these things are extremely unlikely.

Related Question: https://softwareengineering.stackexchange.com/questions/34120/how-often-do-cpus-make-calculation-errors

Community
  • 1
  • 1
Ivan
  • 1,735
  • 1
  • 17
  • 26
0

It will ALWAYS print "True" because that is the way that it is designed.

David Pilkington
  • 13,528
  • 3
  • 41
  • 73
  • Really ? That's amazing. I could never have imagined that a logically sound program would never make a mistake. Thank you for such a thorough answer. – theUser Oct 09 '14 at 10:29
  • 2
    @theIrishUser your condescending tone and manner aside. You may as well ask if 1 + 1 will always equal 2 and why – David Pilkington Oct 09 '14 at 10:33
  • 1
    I just can't imagine that a computer would always get it right. – theUser Oct 09 '14 at 10:34
  • Well that is the advantage of a computer it computes things exactly according to rules you give it, the same every time. Unlike humans who will vary, vacillate, change there minds, suffer from wooly thinking etc.... get side tracked, take ages.... jump to conclusions, misinterpret the instructions etc. – AnthonyLambert Oct 10 '14 at 12:27
0

If it didn't display true I think the whole of computing would suddenly become flawed. There is an area of computing where this might apply look at "fuzzy logic" in google.

AnthonyLambert
  • 8,768
  • 4
  • 37
  • 72
  • How is fuzzy logic related to conditional logic? – arin1405 Oct 09 '14 at 10:30
  • Compared to traditional logic (where variables may take on true or false values), fuzzy logic variables may have a truth value that ranges in degree between 0 and 1. Fuzzy logic has been extended to handle the concept of partial truth, where the truth value may range between completely true and completely false. Obviously his example will always return true because it is testing a constant true being true which it always will be using conventional boolean logic. – AnthonyLambert Oct 09 '14 at 10:32
0

The only way it could print "False" is if you did something like this:

using System;

namespace Demo
{
    public static class Console
    {
        public static void WriteLine(string unused)
        {
            System.Console.WriteLine("False");
        }
    }

    public static class Program
    {
        private static void Main(string[] args)
        {
            if(true)
            {
                Console.WriteLine("True");
            }
            else
            {
                Console.WriteLine("False");
            }        
        }
    }
}

(Yes, this does indeed print "False" - but clearly I cheated. ;))

But aside from cheating, an if statement, like all other boolean logic, is completely reliable and deterministic.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276