Java Decision Structure Basics
Tags: java, decisionstructures, controlstructures
Published: 2023-10-12 02:39:12

---
title: Java Decision Structure Basics
published: true
date: 2023-07-08 20:23:35 UTC
tags: Java,DecisionStructures,ControlStructures
canonical_url: https://crtv.dev/java-decision-structure-basics/
---

 ![Java Decision Structure Basics](https://images.unsplash.com/photo-1502298411556-0b02524812cb?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3wxMTc3M3wwfDF8c2VhcmNofDEwfHxkZWNpc2lvbnxlbnwwfHx8fDE2ODg4NDc0ODV8MA&ixlib=rb-4.0.3&q=80&w=2000)

Decision structures are sometimes known as control structures. These keywords control the flow of your code by making decisions. These decisions are based on the result of Boolean expressions, which can be constructed using a type of math called [Boolean algebra](https://en.wikipedia.org/wiki/Boolean_algebra?ref=crtv.dev).

Conditionals and loops, the two primary forms of decision structures, are foundational to modern programming. Conditionals function like a decision-making crossroad, steering program execution based on specified conditions. Loops, on the other hand, act like a kid saying, "Are we there yet?" executing a block of code over and over until a specific condition is satisfied.

### Conditionals

The most well-known decision structure is if/else if/else. Anytime you need to apply a condition to your code, you will probably use this decision structure. An "if" statement takes a condition. If the condition evaluates to true, the following code will be executed. If an "else if" follows the "if"'s code block, the code following the "else if" will be executed. Finally, if no matches are made to any of the other conditions and an "else" statement is provided, its code will be executed. It's important to remember that the first expression that evaluates to true will be executed, and no other conditions will be tried. If you want to create code where multiple conditions are evaluated independently, you should create multiple if statements.

![Java Decision Structure Basics](https://king.instructure.com/users/11006/files/2530200/preview?verifier=sPy0M61mHwuNMfmsN3TUrJE0E2U3sRP5HmAS4fqO)

The next major decision statement is called a switch. This statement takes in a value and compares it to cases. If no cases match, the default case will be executed. This statement is similar to an if but let's you write less code in certain situations.

![Java Decision Structure Basics](https://king.instructure.com/users/11006/files/2530201/preview?verifier=TWKGNMyyRbdyUNObwcILxj09atlfdLIyH3vpfjqf)

### Loops

Other types of decision structures include while loops, do-while loops, for loops, and foreach loops. These structures execute their code as long as a condition is true. When that condition is false, they stop, and the program continues on.

![Java Decision Structure Basics](https://king.instructure.com/users/11006/files/2530202/preview?verifier=W5glG92EG2RpEEMBoMdHWNLtF5HSbNdsga1q7Sbf)

### Summary

With the exception of super basic short scripts, all programs utilize decision structures. A solid grasp of the behavior associated with each structure and when to use which structure is key for any programmer. Without decision structures, we would be stuck with only linear programming. Such an approach lacks the adaptability necessary to address the complexity of real-world applications.