It's not a question of if, but rather, when. When will stocks come crashing down after a more than decade-long bull run? Six months from now? A year? It's anyone's guess, but that doesn't mean you shouldn't prepare for the inevitable. 

When it comes to investing with a potential recession looming, it's smart to stick with the companies churning out the necessities -- those things that are needed even when money is tight. That applies to businesses too. Sales may be down during a recession, but there are some things companies have to continue to spend on. Investors who choose to invest in those types of enterprises tend to do better during downturns. 

To be ready if, and when, a market crash occurs, consider these three stocks. 

A red arrow crashing to symbolize the stock market crash.

Image source: Getty Images.

Microsoft software exposure will be attractive in a downturn

Stock market crashes usually happen for a reason. If it's because the global economy is slowing, the tech sector typically feels a lot of pain. After all, who has the cash to purchase a new smartphone, laptop, or VR headset when money's tight? But one area of technology that tends to be resilient during slow economic times is software. A leader in that is Microsoft (NASDAQ:MSFT), whose business software is used in both boom and bust periods by scores of companies across the globe. In fact, software budgets even tick up in recessionary times if it boosts productivity and reduces costs.

That's not to say Microsoft is 100% immune to a recession -- but investors do get stability out of the software giant, something they crave in uncertain times. Customers pay to access Microsoft Office and other productivity software in the cloud on a subscription basis, paying monthly or yearly. Investors know how much Microsoft is getting paid and don't have to worry about negative surprises. 

Microsoft's cloud computing business is another strength that can help cushion any blows from a slowdown. Companies are moving data to the cloud to save money. That could pick up if there's a need to slash costs. Both businesses are growing for Microsoft. In its fiscal first quarter, revenue in Microsoft's productivity and business processes unit -- which includes cloud software -- was up 13% year-over-year while revenue in its Azure cloud computing unit was up 59%. After finishing 2019 up about 55%, shares continue to march higher, up an additional 2% so far in 2020. Any dip in the stock could be a buying opportunity to get in ahead of a crash.

Consumers will flock to cheaper retailers 

Walmart (NYSE:WMT) may be getting hit over fears that holiday sales failed to impress Wall Street, but that doesn't mean the nation's largest retailer won't fare well during a recession. After all, low prices are Walmart's middle name. 

In recessionary times consumers look for ways to save money on everything from consumer staples to clothing -- and Walmart has it all. Not to mention a large part of its sales comes from groceries. People have to eat, and may opt to eat out less, which may mean even more business for Walmart. 

As it stands now, some of the higher-end retail stocks are performing better but if a market crash is in the cards because of a slowing economy, Walmart should hold up. Even without a move toward lower-cost products, Walmart is doing well. And that's in the face of intense competition from the likes of and Target. Same-store sales increased 3.2% in the U.S. during the third quarter while e-Commerce sales jumped 41%. Traffic was up 1.3% during the third quarter.

Shares of Walmart aren't cheap. The stock ended the year up 23%. But with investors worried holiday sales will come in lower than expectations and with its India operations facing a restructuring, shares have been declining, giving investors an opportunity to start building a position in Walmart.

It's all about the cereal 

When it comes to economic downturns, consumer staple companies tend to do OK, largely because they make products consumers need. Kellogg (NYSE:K) fits that bill thanks to its cereal and other prepackaged food products. Everyone needs breakfast and dinner -- and Kellog provides a way to do that on the cheap.

But it's not just the breakfast business that can benefit in a recession (or when times are good, for that matter). The company has been placing more focus on its organic products and is diversifying into snacks, which helped organic net sales grow 2% in the third quarter even as the company remains in restructuring mode. Kellogg pointed to frozen foods and emerging markets as other drivers of growth.

On top of that, in tougher times, investors will appreciate Kellogg's dividend. It yields 3.4% and has been growing for fourteen years.

This article represents the opinion of the writer, who may disagree with the “official” recommendation position of a Motley Fool premium advisory service. We’re motley! Questioning an investing thesis -- even one of our own -- helps us all think critically about investing and make decisions that help us become smarter, happier, and richer.