Conditional: What Is It and How to Use It?
Conditional statements are fundamental to programming. They allow you to create logic that executes code based on whether a condition is true or false. This article will introduce you to conditional statements, show you how to use them, and give you some practical examples.
What is a Conditional Statement?
A conditional statement is a control structure that allows you to execute code based on a condition. The condition is evaluated as either true or false, and the code is executed accordingly. There are two types of conditional statements: if statements and switch statements.
If Statements
If statements allow you to execute code if a condition is true. The basic syntax of an if statement is as follows:
```
if (condition) {
// code to execute if condition is true
}
```
For example, let's say you want to execute some code if a variable x is greater than 10. You can use an if statement to do this:
```
if (x > 10) {
// code to execute if x is greater than 10
}
```
If the condition is true, the code inside the curly braces will be executed. If the condition is false, the code will be skipped.
You can also use an else statement to execute code if the condition is false:
```
if (x > 10) {
// code to execute if x is greater than 10
} else {
// code to execute if x is not greater than 10
}
```
If the condition is true, the code inside the first set of curly braces will be executed. If the condition is false, the code inside the second set of curly braces will be executed.
Switch Statements
Switch statements allow you to execute code based on the value of a variable. The basic syntax of a switch statement is as follows:
```
switch (variable) {
case value1:
// code to execute if variable is equal to value1
break;
case value2:
// code to execute if variable is equal to value2
break;
default:
// code to execute if variable is not equal to any of the values
}
```
For example, let's say you h-e a variable x that can h-e the values "red", "green", or "blue". You can use a switch statement to execute code based on the value of x:
```
switch (x) {
case "red":
// code to execute if x is "red"
break;
case "green":
// code to execute if x is "green"
break;
case "blue":
// code to execute if x is "blue"
break;
default:
// code to execute if x is not "red", "green", or "blue"
}
```
If the value of the variable matches one of the cases, the code inside the corresponding set of curly braces will be executed. If the value does not match any of the cases, the code inside the default set of curly braces will be executed.
Practical Examples
Now that you know how to use conditional statements, let's look at some practical examples.
Example 1: Temperature Converter
Let's say you want to create a program that converts temperatures from Fahrenheit to Celsius. You can use an if statement to check whether the user entered a valid temperature:
```
// get temperature from user
var temperature = prompt("Enter temperature in Fahrenheit:");
// convert temperature to Celsius if it is valid
if (!isNaN(temperature)) {
var celsius = (temperature - 32) * 5/9;
alert(temperature + "°F is " + celsius.toFixed(2) + "°C");
} else {
alert("Invalid temperature");
}
```
The isNaN function checks whether the temperature is not a number. If it is not a number, the else block is executed.
Example 2: Grade Calculator
Let's say you want to create a program that calculates a student's grade based on their score. You can use a switch statement to assign a letter grade based on the score:
```
// get score from user
var score = prompt("Enter score:");
// assign letter grade based on score
var grade;
switch (true) {
case (score >= 90):
grade = "A";
break;
case (score >= 80):
grade = "B";
break;
case (score >= 70):
grade = "C";
break;
case (score >= 60):
grade = "D";
break;
default:
grade = "F";
}
alert("Grade: " + grade);
```
The switch statement checks the value of the score variable against each case. If the score is greater than or equal to 90, the code inside the first case is executed. If the score is greater than or equal to 80 but less than 90, the code inside the second case is executed, and so on.
Conclusion
Conditional statements are essential to programming. They allow you to create logic that executes code based on whether a condition is true or false. If statements and switch statements are the two types of conditional statements. If statements allow you to execute code if a condition is true, while switch statements allow you to execute code based on the value of a variable. By using practical examples, you can see how conditional statements can be used in real-world scenarios.