Java: Java Programming For Beginners a simple Start To Java Programming (Written By a software Engineer)
Download 0.82 Mb. Pdf ko'rish
|
Java Programming For Beginners - A Simple Start to Java Programming ( PDFDrive )
Conditional Programming
While writing code, you will faced with several situation where you need to execute a different set of instructions if the condition is true and another set of instructions if the same is false. if-else In order to implement such scenarios, you can use the if-else construct. Syntax: If(condition) { //code } else { //code } Consider a scenario in which you ask the user to enter his or her age using prompt function. Now, you must validate if the age is a valid number, before performing any computation on the value supplied. This is an ideal scenario of implementing conditional programming. Sample implementation for this scenario is: var userAge = prompt(‘Enter your age: ’, ‘’); if(isNaN(userAge)) { alert(‘Age entered is invalid!’); } else { //code } In this sample code, the if clause checks if the entered value is a number. If the condition is true, that is the object entered is not a number, the user is given an alert message. However, if the condition is false, the code for else is executed. It is important to note here that for single statements, it is not necessary to use curly braces. The above mentioned code can also be written as: var userAge = prompt(‘Enter your age: ’, ‘’); if(isNaN(userAge)) alert(‘Age entered is invalid!’); else //code However, it is a good practice to use curly braces as there is scope of adding additional code later on. if-else if Another conditional programming construct is if-else if construct. This construct allows you to declare multiple conditions and the actions associated with them. The syntax is: if(condition) { //code } else if(condition) { //code } else { //code } Switch Multiple else ifs can be implemented using this construct. The execution overhead is high for this conditional programming construct as conditions are sequentially checked for validity. As an alternative, another keyword, switch, is available, which implements multiple conditions in the form of a jump table. Therefore, the execution overhead for switch is lesser than that of if-else if. Sample implementation: var userChoice = prompt(‘Choose an alphabet: a, b, c’, ‘e’); switch (userChoice) { case ‘a’: alert(‘a chosen\n’); break; case ‘b’: alert(‘b chosen\n’); break; case ‘c’: alert(‘c chosen\n’); break; default: alert(‘None of the alphabets chosen\n’); break; }; The switch construct matches that value entered by the user with the values presented in the cases. If a matching value is found, the case is executed. However, in case, none of the case values match the entered input, the default case is executed. Besides this, you can also use conditions in case values and the case for which the condition holds true is executed. If you do not use the break statement after the code of a case, all the cases following the matching case will be executed. For example, if the user enters ‘b’ for the above example and there are no break statements after the case code, then the output will be: b chosen c chosen None of the alphabets chosen Also, it is a good practice to use a break statement in the default case as well. Note: If you wish to determine if a keyword has been assigned any value or not, you can use the following code: if(c) { //code } else { //code } If the variable c has been assigned a not-null value, then the if condition is true and the corresponding code is executed. On the other hand, if the value of variable c is undefined or null, the code within the else construct is executed. Note: The value of the following conditions will always be true: ” == 0 null == undefined ‘123’ == 123 false == 0; Please note that JavaScript converts the type of the variable concerned for compatibility in comparisons. However, if you want to compare both the value and type of two variables, then JavaScript provides another set of operators, === and !===. When the comparisons for the values mentioned in the above example are done using this operator, the resultant will always be false. Download 0.82 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling