Advertisements
Advertisements
प्रश्न
What is if statement and write its types.
Advertisements
उत्तर
The if statement is the fundamental control statement that allows JavaScript to make decisions to execute statements conditionally. This statement has two forms. The first form is for only true conditions.
The syntax is
if (condition)
{
True block;
}
In (fie if form, condition contains relational/ logical expression is evaluated. If the resulting value is true the true block is executed. True block may contain one or more than one statement.
For example:
<Html>
<Head>
<Title>Demo Program - To test if command in JavaScript </Title>
</Head>
<Body>
<script language="javascript" type="text/javascript">
var age = prompt("Please enter your Age :", "0");
if(age>=16)
{
alert("You Are Eligible to Vote ....");
}
</script>
</Body>
</Html>
The Output will be:


The second form of the if statement is an else clause that is the program to follow either of two branches depending on the condition. In the simple if construction, no special processing is performed when the condition evaluates to false. But if processing must follow one of two paths, hence need to use if…else format.
Its syntax is:
if (expression)
{ statements if true }
else
{ statements if false }
This form is similar to if statement but the only difference is the else keyword, which provides an alternate path for execution to follow if the condition evaluates to false.
Example:
<Html>
<Head>
<Title>Demo Program - To test if..else command in JavaScript </Title>
</Head>
<Body>
<script language="javascript" type="text/javascript">
var age = prompt("Please enter your Age :", "0");
if(age>=18)
{
alert("You Are Eligible to get Driving Licence..");
}
else
{
alert("You Are Not Eligible to get Driving Licence..");
}
</script>
</Body>
</Html>
Output:




APPEARS IN
संबंधित प्रश्न
_______ statement can be used as alterative to if-else statement.
What will be the output for the following snippet:
For (var n=0; n<10; n++)
{
if (n==3)
{
break;
}
document write (n+”<br>”);
}In which loop the condition is evaluated, before executing a statement?
The _______ statement is especially useful when testing all the possible results of an expression.
List out the various branching statements in JavaScript?
Write the syntax for else-if statement.
What message will be displayed, if the input for age is given as 20, for the following snippet?
Explain switch case statement with example.
Consider the following program segment and select the output of the same when n = 10:
switch(n)
{Case 10: System.out.println(0*2);
case 4: System.out.println(n*4); break;
default : Syslem.out.println(n);
}Which of the following data type cannot be used with switch case construct?
