tags:

views:

360

answers:

7

Hello,

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");
}
+1  A: 

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
}
Artelius
+2  A: 

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

while (true) {
   if (i ==j ) {
     // whatever
   }
}
Martin Clarke
+1  A: 

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).

Catchwa
+3  A: 
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");
  }
}
gnibbler
It compiles, but ok.
Nathan Campos
@Slaks. I changed it
gnibbler
+2  A: 

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");
    }

    //...
}
SLaks
+1  A: 

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
    }
}
Noldorin
+6  A: 

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");
       }
   }
instanceofTom
Is this an attempt at humor? lol
Simucal
I laughed while I was writing it
instanceofTom
Weak, but +1 anyway. haha
Dusty