Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional break inside of switch case

Tags:

c#

I was wondering if it was possible to conditionally break out of a case in a switch statement in C#. Take the following example.

MediaStream photoMediaStream = null;
switch (photoSize)
{
    case PhotoSize.Normal:
        if (imageWidth >= NormalWidth && imageWidth % NormalWidth == 0)
        {
            photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = NormalWidth});
            break;
        }
    case PhotoSize.Small:
        if (imageWidth >= SmallWidth && imageWidth % SmallWidth == 0)
        {
            photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = SmallWidth});
            break;
        }
    case PhotoSize.Thumb:
        if (imageWidth >= ThumbWidth && imageWidth % ThumbWidth == 0)
        {
            photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = ThumbWidth});
            break;
        }
}

Basically, if the conditional is true I want to do something and then break out of the switch statement, but if not I just want to fall through to the next case.

like image 814
Kyle Avatar asked Mar 14 '12 15:03

Kyle


People also ask

Can we give condition inside switch case?

Switch Case In C In a switch statement, we pass a variable holding a value in the statement. If the condition is matching to the value, the code under the condition is executed. The condition is represented by a keyword case, followed by the value which can be a character or an integer. After this, there is a colon.

Can we use break in switch case?

You can use the break statement to end processing of a particular labeled statement within the switch statement. It branches to the end of the switch statement. Without break , the program continues to the next labeled statement, executing the statements until a break or the end of the statement is reached.

What break does in switch case?

4) The break statement is used inside the switch to terminate a statement sequence. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. 5) The break statement is optional. If omitted, execution will continue on into the next case.


1 Answers

Since you can't implicitly fall through to the next case, you must do it explicitly, using the goto statement. This is one of the rare cases where the use of this statement is acceptable...

MediaStream photoMediaStream = null;
switch (photoSize)
{
    case PhotoSize.Normal:
        if (imageWidth >= NormalWidth && imageWidth % NormalWidth == 0)
        {
            photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = NormalWidth});
            break;
        }
        goto case PhotoSize.Small;
    case PhotoSize.Small:
        if (imageWidth >= SmallWidth && imageWidth % SmallWidth == 0)
        {
            photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = SmallWidth});
            break;
        }
        goto case PhotoSize.Thumb;
    case PhotoSize.Thumb:
        if (imageWidth >= ThumbWidth && imageWidth % ThumbWidth == 0)
        {
            photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = ThumbWidth});
            break;
        }
}

Anyway, it would probably be better to refactor it using if statements:

MediaStream GetPhotoMediaStream(PhotoSize photoSize, /* whatever parameters you need... */)
{
    if (photoSize == PhotoSize.Normal)
    {
        if (imageWidth >= NormalWidth && imageWidth % NormalWidth == 0)
        {
            return photoMedia.GetStream(new MediaOptions {Width = NormalWidth});
        }
        photoSize = PhotoSize.Small;
    }
    if (photoSize == PhotoSize.Small)
    {
        if (imageWidth >= SmallWidth && imageWidth % SmallWidth == 0)
        {
            return photoMedia.GetStream(new MediaOptions {Width = SmallWidth});
        }
        photoSize = PhotoSize.Thumb;
    }
    if (photoSize == PhotoSize.Thumb)
    {
        if (imageWidth >= ThumbWidth && imageWidth % ThumbWidth == 0)
        {
            return photoMedia.GetStream(new MediaOptions {Width = ThumbWidth});
        }
    }
    return null;
}
like image 165
Thomas Levesque Avatar answered Oct 02 '22 17:10

Thomas Levesque