To allow our boy to play the rest of the afternoon if his homework is done AND he got an “A” on his test we would use the AND operator. To write a compound condition you need to decide how the condition would operate to determine whether you use the AND or the OR operators in the condition. How to use AND, OR in the JavaScript IF statementĪs mentioned above, the condition for the if statement need not be a single variable but can consist of a compound condition. To write this set of code we need to write a compound if else statement using Boolean logic to test if both conditions are true. If he got a “B” for the test and his homework is done then he can only go and play for two hours. If he got an “A” for his test and his homework is complete then he can go out and play for the rest of the afternoon. Let us say for example, that not only must our boy have his homework done, but that his playtime will depend on the grade he got for the test he did as well. Not only can it be expressed using all of the other logical program operators, but it can also be expressed as a compound statement. But the if statement can be expressed using other operators as well. The above code was also based on using the true and false Boolean values for the if statement. But you could add a condition to the else statement in which case the program will use that condition to determine whether it should perform the else statement code or move on. This means that the program will perform either the statement which is contained within the if statement or the code contained within the else statement. The above code also contains no conditions for the else statement. If it was then it would have the following output: The above code would ask for confirmation that homework was done. So to set up a system of both reward and punishment for homework done or not done the code would like something like this: The syntax of the “if else” statement builds on the above syntax and can be expressed as follows: But what happens if he says no? This is where the else part of the “if else” statement comes into play. If he answers ok, then the answer is recorded as true and he gets the reward. The “confirm” code in the above creates a popup box which asks the boy to confirm if his homework is done. The pseudo code to do this would look a lot like this: Once we have defined the variable we can implement the if statement to reward our boy if his homework is done. If you don’t know how to define a variable in JavaScript, then a course like JavaScript made easy from udemy will teach you how to define variables as well as how to can use them. To write the code we would first need to define a variable. If we use our original example of homework we could write a program to reward our young boy, if he’d completed his homework. If not, the program moves onto the next set of code within the program. If the condition is true the program moves onto the code block within the brackets. When the program reaches the if statement, it evaluates whether the condition is true. The syntax of the statement can be expressed as follows: If the condition is false, then the code will not be executed. The “if” statement allows the programmer to execute a set of instructions if a condition is true. The if statement is the foundation for the if else statement as well as the “if, else if” statement. For a basic course on the principle of JavaScript and how to apply them as well as how to implement JavaScript on your website, udemy offers a JavaScript prep course. This is a basic tutorial but to understand the tutorial and to apply the concepts, a basic understanding of JavaScript is required. This tutorial will explain how the “if” and “if else” statements are used in JavaScript to make web applications more functional and useful. The if else conditional statement in JavaScript allows the programmer to write code that performs different functions based on various possibilities, in the same way that we perform actions based on different conditions. Actually, I would say that conditional statements like if then else, for and while loops form the backbone of most programming, irrespective of which language you choose as your programming language. So it’s no wonder programmers rely on conditional statement to write logical useful code. In fact, we based most of our choices on if then else statements. If your homework is done you can go out and play, else you’re in trouble young man! If its good weather we’re going to the beach today, else if it’s raining we’re staying home.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |