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
संबंधित प्रश्न
Which statement in switch case is used to exit the statement once the appropriate choice is found?
Which of the following is not a branching statement?
<script type=“text/javascript”>
x = 6 + “3”;
document.write(x);
</script>
what will be the output?
List out the various branching statements in JavaScript?
Differentiate the break and continue statement.
Write the output for the following program.
<Html>
<Head>
<Title> for statement</title>
<Head>
<Body>
<script language= "java Script" type = "text / javaScript")
var no1= prompt ("please enter table you want:", "0" );
document write ("<h2> multiplication for your need </h2>")
for (Var no2= 0; no2<=10; no2++)
{
document write (no1+ "x" + no2+ "=" + no1+no2+ "<br>);
}
</script>
</body>
</Html>Consider the following program segment and select the output of the same when n = 10:
switch(n)
{Case 10: System.out.println(n*2);
case 4: System.out.println(n*4); break;
default : Syslem.out.println(n);
}Give the output of the following program segment:
int n = 4279; int d;
while(n>0)
(d=n%10;
System.out.println(d);
n=n/100;
}Which of the following is not true with regards to a switch statement?
A student executes the following program segment and gets an error. Identify the statement which has an error, correct the same to get the output as WIN.
boolean x=true;
switch(x)
{ case 1:System.out.println("WIN");break;
case 2;System.out.println("LOOSE");
}