2

I'm developing a simple project, but how I can do a repeat forever a If function(It's like a command-line)? Thanks.

My code is like this:

Console.Write("> ");
var Command = Console.ReadLine();
if (Command == "About") {
    Console.WriteLine("This Operational System was build with Cosmos using C#");
    Console.WriteLine("Emerald OS v0.01");
}
6
  • Can you post what you already have? Nov 4, 2009 at 0:16
  • 7
    Watch out, forever is "a long time" :-) – mjv 0 secs ago
    – mjv
    Nov 4, 2009 at 0:22
  • 1
    By the way, build should really be built.
    – SLaks
    Nov 4, 2009 at 0:57
  • Also, Operational should probably be Operating.
    – SLaks
    Nov 4, 2009 at 0:57
  • Actually I think Operational System and OS should probably be shell.
    – Artelius
    Nov 4, 2009 at 2:39

7 Answers 7

9

By any chance do you mean:

while( !(!(!(( (true != false) && (false != true) ) || ( (true == true) || (false == false) )))) == false   )
   {
       Console.Write("> ");
       if ("About" == Console.ReadLine())
       {
           Console.WriteLine("This Operational System was build with Cosmos using C#");
           Console.WriteLine("Emerald OS v0.01");
       }
   }
1
  • I laughed while I was writing it Nov 4, 2009 at 0:38
4
string Command;
while (true) {
  Command = Console.ReadLine();
  if (Command == "About") {
    Console.WriteLine("This Operational System was build with Cosmos using C#");
    Console.WriteLine("Emerald OS v0.01");
  }
}
0
3

Your question is unclear, but you probably want to do something like this:

while(true) {    //Loop forever
    string command = Console.ReadLine();
    if (command.Equals("Exit", StringComparison.OrdinalIgnoreCase))
        break;    //Get out of the infinite loop
    else if (command.Equals("About", StringComparison.OrdinalIgnoreCase)) {A
        Console.WriteLine("This Operational System was build with Cosmos using C#");
        Console.WriteLine("Emerald OS v0.01");
    }

    //...
}
2

I don't think your question is really clear. But here is an attempt :)

while (true) {
   if (i ==j ) {
     // whatever
   }
}
2

You mean this?

while(true) {
    if( ...) {
    }
}

PS: this is one of my favourite preprocessor hacks. Doesn't work in C# though, only C/C++.

#define ever (;;)

for ever {
    //do stuff
}
1

I think you just want a simple while loop with (at least) one exit point.

while(true)
{
    Console.Write("> ");
    var command = Console.ReadLine();
    if (command == "about") {
        Console.WriteLine("This Operational System was build with Cosmos using C#");
        Console.WriteLine("Emerald OS v0.01");
    } else if (command == "exit") {
        break; // Exit loop
    }
}
1

You can't use an 'if' statement by itself because when it gets to the end your program will continue executing the next statement in your code. I think what you're after is a 'while' statement that always evaluates to true.

e.g.

string Command; 
while(true)
{
    Command = Console.ReadLine(); 
    if (Command == "About")
    { 
        Console.WriteLine("This Operational System was build with Cosmos using C#"); 
        Console.WriteLine("Emerald OS v0.01");
    }
} 

This loop will be inescapable unless an exception is thrown or you execute a break statement (or whatever the equivalent is in C#, I'm a Java guy - don't hate me).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.