Conditional Statements | Bondar Academy
Course: JavaScript for Testers
Module: JavaScript Fundamentals
Instructor: Artem Bondar
Lesson Summary
This lesson covers conditional statements , which are essential for controlling the logic flow of applications. Conditional statements allow the application to execute different actions based on specific conditions, typically structured as: if (condition) { // execute this code } else { // execute this code } The if statement evaluates a condition inside parentheses. If the condition is true , the code block within the curly braces is executed; if false , the code in the else block runs. Example: Time-Based Greetings To illustrate conditional logic, we created a program that prints a greeting based on the current hour: If the hour is between 6 and 12, print "Good morning" . If the hour is between 12 and 18, print "Good afternoon" . Otherwise, print "Good evening" . The code snippet for this logic is as follows: if (hour >= 6 && hour = 12 && hour Example: Driver's License Eligibility Another example discussed was checking eligibility for a driver's license: if (age > 18 && isUSCitizen) { console.log("Customer is eligible for driver's license"); } else { console.log("Customer is not eligible for driver's license"); } In summary, conditional statements are crucial for managing the flow of execution in programming. They allow for complex decision-making by combining multiple conditions using else if and else statements.
Video Transcript
In this lesson, we will talk about conditional statements. So conditions is the main mechanism, how you can drive the logic of your application to do different things based on the required condition. So for example, if happened this, then do this, otherwise do that. This is the split of the logic flow of the application and all programming languages have this. And all programming languages have the same syntax for this type of the operation. So in this lesson, we will write our first conditional logic and I will show you how to do that. So let's get into it. So let me show you the basic syntax of the definition for the control flow. So if condition, then execute some code here, here, else, execute some code here. So this is how it is defined. So it starts with if, then inside of the parentheses, we define the condition based on what we need to make a switch. So if the evaluation inside of this parentheses is evaluated into true, then the code inside of this block will be executed. So this block is defined with the curly braces. If this condition is evaluated into false, then this code will be executed and this one will be skipped. So this is how it works. And based on this, let's create a real running example. So you will see better how to use logical operators and how to use those inside of the conditional statement. So let's say we have a requirement that we want to print a different message in the console based on the time of the day, based on what hour it is right now. Let's say that if hour between six and 12, we want to print good morning. If hour between 12 and 18, we want to print good afternoon. And otherwise, we want to print good evening. Okay, so that's the logic that we want to implement. And let's start with creation of the variable, which is hour and give it some initial value. Let's say five, it's 5 a.m. in the morning. And we're gonna start with the writing the condition. So we start with if, and then inside of if, we need to define the condition based on our requirements. So if hour between six and 12, right? So let's define this. If hour is what? More than equal six and hour is less than 12. So six is included, but less than 12. Less than 12. We want to print a message. Console.log good morning. Else if, and we define the second condition. So if this one is not satisfied, now we need to satisfy the second condition. The second condition says, if hour is more than equal 12 and less than 18, then we want to print good afternoon. And if none of that works, none of that works, so otherwise else we want to print good evening. Okay, so now let's run this code and see how it works. I will comment this out because it's gonna throw the error because condition is not defined. And let's execute just this portion of the code. So we run lesson six and I run it. And what we have, okay, I forgot hour. Okay, run it one more time. And we see a message printed, which is good evening. So none of the condition for the five were not satisfied over here and was not satisfied over here. Expression was not evaluated into the true and it went all the way to the else statement to the good evening. If I put a six over here, it's 6 a.m. in the morning and run it again. And now we see a message, good morning, right? Because our hour is more than equal six and our hour less than equal 12. It's within the boundary from six to 12. And that's why it's printed good morning. And if I put something, let's say between 12 and 18, let's say 17 and run it right now. It is printed right now, good afternoon. So this is how condition works and how can you manage the execution flow of your code. And if we take, let's say example from the previous lesson, we can rewrite this code the same way. So let me copy this code right here, paste it right here. And we want to repeat the logic from eligibility for the driver's license. And we say something like, if age is more than 18 and is US citizen, we want to console.log customer is eligible for driver's license. Else we print a message, customer is not eligible for driver's license. And we can simply run this code as well. So if we put true here and true here, we run this and we see the message, customer is eligible for driver's license. But if we put at least one value as a false and run it one more time, we see a different message. Customer is not eligible for driver's license. All right, so let's quickly summarize what we did in this lesson. In this lesson, we talked about conditional statement or conditional flow. The basic rule for conditional statement is if when you start the condition, then inside of the parentheses, you define your condition, which should be evaluated into true or into false. This should be a Boolean of data type. And then the first block of code will be executed if the conditioner is true. And otherwise you use else keyword and the other block of code is executed when the expression is false. You can combine several conditions at the same time, like if condition number one else, if condition number two, otherwise execute what is left. That's it guys and see you in the next lesson.