Intro to Conditional Statements
Not Started

The code we've written so far has run line-by-line one after another. That's about to change.

Conditionals are a fundamental concept in programming that allows us to make decisions in our code based on a Boolean expression. If the expression evaluates to true, a block of code is executed. This is a powerful way to control the flow of a program. Here's an example of a simple conditional:

Boolean codeRan = false; if(true){ codeRan = true; }

Conditionals begin with the keyword if and a pair of parentheses, containing a boolean expression that must evaluate to either true or false. In the simple example provided earlier, we explicitly used a boolean literal, which will always evaluate to true. Throughout the course, we will explore more complex examples of conditionals.

Boolean shouldRunCode = false; Boolean codeRan = false; if(shouldRunCode){ codeRan = true; // This will never run } //codeRan is still false shouldRunCode = true; if(shouldRunCode){ codeRan = true; // This will run } // codeRan is now TRUE

It is not mandatory, but to improve readability, the statements inside the conditional block should be indented with a tab. There are several types of conditionals and operators available to us in Apex, we will explore those in the upcoming lessons.

Challenge

Uncomment the code to declare your first conditional statement.