Master AI & Build your First Coding Portfolio with SkillReactor | Sign Up Now

3 Conditional Structures

1 Decision Marking

In our daily lives, decisions are often based on the information or experience available to us. Similarly, in computer programming, we can make decisions about what messages to display or which actions to take based on the information provided.

In the above image, you can see a flowchart processing some information:

  • There's a variable named marks with a value of 50.
  • Moving to the next step, the diamond shape represents a decision point where the program checks if the marks are greater than 50.
  • If the marks are indeed greater than 50, the left block with the green box prints a "Passed" message.
  • If the marks are less than or equal to 50, the condition is false, leading to the "Failed" message being printed.

In programming languages, we have conditional statements to implement decision making.

If we translate the above flowchart example to JavaScript code, it will look like this:

var marks = 50;
if (marks > 50) {
    console.log("Passed");
} else {
    console.log("Failed");
}

In the if statement, the condition (marks > 50) checks if the value of marks is greater than 50. If it is, the code inside the if block is executed (printing "Passed"). Otherwise, the code inside the else block is executed (printing "Failed"). You don't need to worry about the code for now; we will learn about the if-else structure later in the chapter.

Before we learn about the if-else statement, let's understand the basic comparison operators based on which we create conditions in programming.

Comparison Operators

Here are some common comparison operators:

  • Equal to (==): Compares if two values are equal.
  • Not equal to (!=): Compares if two values are not equal.
  • Greater than (>): Checks if one value is greater than another.
  • Less than (<): Checks if one value is less than another.
  • Greater than or equal to (>=): Checks if one value is greater than or equal to another.
  • Less than or equal to (<=): Checks if one value is less than or equal to another.