Reputation: 29507
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");
}
Upvotes: 2
Views: 4579
Reputation: 49099
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
}
Upvotes: 2
Reputation: 5855
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).
Upvotes: 1
Reputation: 6968
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");
}
}
Upvotes: 9
Reputation: 147401
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
}
}
Upvotes: 1
Reputation: 887777
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");
}
//...
}
Upvotes: 3
Reputation: 304355
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");
}
}
Upvotes: 4
Reputation: 5657
I don't think your question is really clear. But here is an attempt :)
while (true) {
if (i ==j ) {
// whatever
}
}
Upvotes: 2