Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 392035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:07:11+00:00 2026-05-12T16:07:11+00:00

What is the most terse syntax to combine a check for some preconditions with

  • 0

What is the most terse syntax to combine a check for some preconditions with a switch statement? Can I combine an if/else and switch statement?

if (!IsValid(text))
{
   DoSomeLogging();
}
else
{
   switch (text)
   {
     case "1":
        DoSomething();
        break;
     case "2"
        DoSomethingElse();
        break;
     default:
        break;
   }
}

Edit: Removed skip comment. I wasn’t looking to invert the statement.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:07:11+00:00Added an answer on May 12, 2026 at 4:07 pm

    The “combined” else switch is nothing new:

    if (!IsValid(text))
    {
        DoSomeLogging();
    }
    else switch (text)
    {
    }
    

    is the same as

    if (!IsValid(text))
    {
        DoSomeLogging();
    }
    else 
    {
        switch (text)
        {
        }
    }
    

    This syntax has been inherited from C via C++.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Most databases support some form of insert into select... statement. insert into a select
Most apps have some kind of preferences or user settings that can be stored
Most things are dynamic in SSRS i.e you can create a custom expression for
Most objects in SAP ERP can be double clicked to get more information in
How can I rewrite this jQuery to be more terse and without the guard
Most of the BLAS Level 1 API can be trivially written straight forward using
Most process-monitoring software can show CPU(s) load by application in real-time. But I can't
Most website I can parse its title easily with RegEx (. ) or \s
Most of us know the following syntax: function funcName($param='value'){ echo $param; } funcName(); Result:
Most of time we represent concepts which can never be less than 0. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.