Learn Programming Fundamentals
1 Programming Fundamentals
2 Variables Data Types
4 Strings
5 Arrays
6 Objects
8 Functions
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:
marks
with a value of 50
.diamond shape
represents a decision point
where the program checks if the marks are greater than 50
.marks
are indeed greater than 50
, the left block with the green box prints a "Passed"
message.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.
Here are some common comparison operators:
==
): Compares if two values are equal.!=
): Compares if two values are not equal.>
): Checks if one value is greater than another.<
): Checks if one value is less than another.>=
): Checks if one value is greater than or equal to another.<=
): Checks if one value is less than or equal to another.