Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Else if statement #4615

Open
JoshLove-msft opened this issue Apr 22, 2024 · 3 comments
Open

Else if statement #4615

JoshLove-msft opened this issue Apr 22, 2024 · 3 comments

Comments

@JoshLove-msft
Copy link
Member

JoshLove-msft commented Apr 22, 2024

Is there any support for writing an if else statement like this (inlining the second if statement so that it is an else if)

if (condition) 
{
...
}
else if (condition)
{
...
}

I couldn't figure out how to do this using an IfElseStatement. Can this be supported?

@ArcturusZhang
Copy link
Member

ArcturusZhang commented Apr 23, 2024

Yes we already support this. The IfElseStatement is designed as self-recursive. Here is a real life example:


You could write this in this way:

var statement = new IfElseStatement(
	new IfStatement(condition1)
	{
		// statements for your if
	},
	// this is the else statement
	new IfStatement(condition2)
	{
		// statements for your first else if
	}));

This will write into:

if (condition1)
{
	// statements for your if
}
else if (condition2)
{
	// statements for your first else if
}

If you want more else if, you could just use another IfElseStatement as the Else part:

var statement = new IfElseStatement(
	new IfStatement(condition1)
	{
		// statements for your if
	},
	// this is the else statement
	new IfElseStatement(
		new IfStatement(condition2)
		{
			// statements for your first else if
		},
		new IfElseStatement(
			new IfStatement(condition3)
			{
				// statements for your second else if
			},
			new MethodBodyStatement[] {
				// statements for your else
			})));

this will write:

if (condition1)
{
	// statements for your if
}
else if (condition2)
{
	// statements for your first else if
}
else if (condition3)
{
	// statements for your second else if
}
else
{
	// statements for your else
}

@ArcturusZhang
Copy link
Member

I must be doing something wrong - #4649 (files)

results in #4649 (files)

yes from the generated code, we are having:

if (...)
{
	// ...
}
else
{
	if (...)
	{
		// ...
	}
}

which just works but it is not ideal, somewhere is wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants