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

  • SEARCH
  • Home
  • 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 7632445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:37:38+00:00 2026-05-31T06:37:38+00:00

if (task1 != null) //Do something with task1 else { if (task2 != null)

  • 0
if (task1 != null)
    //Do something with task1
else
{
     if (task2 != null)
         //Do something with task2
     else
     {
         if (task3 != null)
             //Do something with task3
         else
         {
             if (task4 != null)
                 //Do something with task4
         }
     }
}

Is there an alternative to the above code? I’m looking for a sort of ‘flatter’ way of kind of switch casing on the tasks depending which is not null.

Thanks a LOT in advance for anyone that can help.

  • 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-31T06:37:39+00:00Added an answer on May 31, 2026 at 6:37 am

    It depends if your method does anything else. If it doesn’t, you can use:

    if (task1 != null)
    {
        // Do something with task1
        return;
    }
    if (task2 != null)
    {
        // Do something with task2
        return;
    }
    if (task3 != null)
    {
        // Do something with task3
        return;
    }
    if (task4 != null)
    {
        // Do something with task4
        return;
    }
    

    (I was about to add the same point that Marc was making – if you’re going to do the same thing with whichever task is first non-null, then the null-coalescing operator is indeed your friend.)

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

Sidebar

Related Questions

I have a set of data in one JSON structure: [[task1, 10, 99], [task2,
I'm having a table looking something like this: CREATE TABLE `Calls` ( `calendar_id` int(11)
Let's say I do something in Java like: RemoteResponse response = null; try {
Trying to do something like this: Task task1 = Task.Factory.FromAsync(stream.BeginWrite, stream.EndWrite, task1Data, 0, task1Data.Length,
All, there are many question on the above topic but I believe this is
Task at hand — I have three versions of some code, developed by different
I would like to do something similar what we do in ASP.NET where we
I know I'm missing something basic. I'm trying to dynamically compile a class and
I have written this code which accesses the Delete method on Web Service x,
I'm currently doing something like this in the AsyncTask's onPostExecute method, where NewTask is

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.