Making Decisions with IF Statements

Welcome to another important unit in your COBOL journey! In this lesson, we are going to explore the significance of decision-making in programming using IF statements. Just like in real life, where you evaluate conditions and make decisions (e.g., whether to bring an umbrella based on the weather), in programming, this skill is crucial for developing efficient and functional applications.

What You'll Learn

In this unit, you will learn how to use IF statements in COBOL to make decisions based on conditions. We will cover basic decision-making scenarios and teach you how to create simple and compound conditional statements. Here is a snippet of what you'll be able to understand by the end of this lesson:

This code demonstrates a simple IF statement that checks whether an account balance is below a minimum required balance and displays a message accordingly. We'll see "Balance is sufficient." printed in the output.

Let's examine the structure of an IF statement in COBOL:

  • The IF statement is followed by a condition that evaluates to either TRUE or FALSE.
  • If the condition is TRUE, the statements within the IF block are executed.
  • If the condition is FALSE, the statements within the ELSE block (if present) are executed.

In our example above, the IF statement checks if the Account-Balance is less than the Minimum-Balance. If it is, the program displays a message indicating that the balance is below the minimum required. Otherwise, it displays a message indicating that the balance is sufficient.

Note that we can achieve the same result using the LESS keyword by replacing the IF condition with the following code:

Similarly, we can use the greater than (>) operator to check if the balance is above the minimum required balance.

Using OR in IF Statements

In addition to simple IF statements, you can also connect multiple conditions using logical operators. One such operator is OR. When you use OR, the IF statement evaluates to TRUE if any one of the conditions separated by OR is true.

Let's take a look at an example:

In this example, the IF statement checks two conditions: whether the Account-Balance is less than the Minimum-Balance or less than the Warning-Balance. If either of these conditions is true, the program displays a message indicating that the account needs attention. If neither condition is true, the program displays a message indicating that the account is in good standing.

In this case as well, we can achieve the same result using the LESS keyword:

It’s important to understand how logical operators like OR can enhance the decision-making capabilities of your IF statements. Using OR can assist in creating more sophisticated and flexible conditions in your code. Similarly, we can use AND if we want two conditions to be true at the same time. Always ensure that your conditions are easy to understand and maintain.

Why It Matters

Understanding IF statements is vital for several reasons:

  • Flow Control: IF statements allow you to control the flow of your program. They enable you to execute certain sections of code based on whether a condition is true or false.

  • Error Handling: They are useful for validating data and handling errors, ensuring your program can handle different scenarios gracefully.

  • Efficiency: Making efficient decisions in your code can also optimize the performance of your application, making it run faster and use resources more effectively.

Excited to dive into the practice section? Let’s get started and equip you with these essential skills for COBOL programming!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal