TN State Board 12th Computer Applications Important Questions Chapter 6 PHP Conditional Statements
Question 1.
When is a conditional statement ended with end if?
Answer:
When the original it was followed by and then the code block without braces.
Question 2.
What will be the values of $a and $b after the code below is executed?
Answer:
$a = ‘1’;
$b = &$a;
Sb = “2$b;
output: “21”
(Both $a and $b will be equal to the string “21” after the above code is executed)
Question 3.
Draw flow chart if elseif…else statement.
Answer:
(i) This flow chart explain if, elseif…else statement, it used to take decision based on the different condition.
(ii) If condition is true, the following conditional code will be executed, if it is false exit from the conditional code.
Question 4.
Write a PHP program to find bigger, smaller, equal of any two number.
Answer:
<? php
if ($a>$b)
{
}echo “A is bigger than B”;
else if ($a = = $b)
{
echo “A is equal toB”;
}
else
{
echo “A is smaller than B”;
} .
?>
Question 5.
What is faster if or switch statement?
Answer:
- A switch statement might prove to be faster than if provided number of cases are good.
- If-else better for boolean values whereas switch statements are great for fixed data values.
- If-else statement can test expressions based on the ranges of values or conditions, whereas a switch statement tests expressions based only on a single integer, enumerated value, or string object.
Question 6.
Explain the following conditional statement PHP coding.
Answer:
<! DOCTYPE html>
<html>
<body>
<? php
$S = date (“D”);
if ($S < “15”) *
{
echo “working day”;
}
?>
</body>
</html>
Output: Working day
Explanation of code:
1. <? PHP . – starting php
2 . Ss = date(“D”-) – Declaring variable
3. if ($S < “15”) – If condition true code executes
4. echo – Built in function to print the text
5. ?> – closing of php
Question 7.
Write a simple php program to print grade of the student result using if… elseif…else conditional statement.
Answer:
coding:
<?php
$ result = 70;
if ($result > = 75)
{
echo “Grade A…Pass”;
}
elseif ($result > = 60)
{
echo “Grade B…Pass”;
}
elseif ($result > = 45)
{
echo “Grade C pass”;
}
else
{
echo “Fail”;
}
?>
Question 8.
Write a php program using switch case for string variable.
Answer:
$position = “dba”;
switch ($position)
{
case “member”:
echo “welcome member”;
break;
case “teacher”:
echo “welcome teacher”;
break;
case “student”:
echo “welcome student”;
break;
default:
echo “Welcome you all others”;
break;
}
Question 9.
Why switch statement is better for multi-way branching.
Answer:
- When compiler compiles a switch statement, if will inspect each of the case constants and create a jump table.
- A jump table that will use for selecting the path of execution depending on the value of the expression.
- Therefore, if we need to select among a large group of values, a switch statement will run much faster than the equivalent logic coded using a sequence of if…elses.
- The compiler can do this because it knows that the case constants are all the same type and simply must be compared for equality with the switch expression.
- While in case of it expressions, the compiler has no such knowledge.
Question 10.
Write a PHP program to check given character is vowel or consonant using if…else if statement.
Answer:
<? php
$char = $_post [ ‘ch’]; ,
if ($char==”a”) ‘
{
echo $char.”is vowel”;
}
elseif ($char==”e”)
{
echo $char.”is vowel”;
}
elseif ($char==”iM)
{
echo $char .”is vowel”;
elseif ($char==”o”)
{
echo $char. “is vowel”;
}
elseif ($char==”u”)
{
echo $char.”is vowel”;
}
else
{
echo $char. “is consonant”;
}
?>
Question 11.
Write a PHP program a day of the week using if…elseif…else statement.
Answer:
<?php
if ($day==”Mon”) ‘
echo “Monday”;
elseif ($day==”Tue”)
echo “Tuesday”;
elseif ($day==”Wed”)
echo “WEDNESDAY”;
elseif ($day==”Thu”)
echo “THURSDAY”;
elseif ($day==”Fri”)
echo “FRIDAY”;
elseif ($day==”Sat”)
echo “SATURDAY”;
else
echo “SUNDAY”;
?>
Question 12.
Write a PHP program day of the week using switch case statement.
Answer:
<?php
$today=date(“D”);
switch ($today)
<
case “Mon”:
echo “Today is Monday”;
break; ’
case “Tue”:
echo “Today is Tuesday”; .
break;
case “Wed”:
echo “Today is Wednesday”;
break;
case “Thu”
echo “Today is Thursday”;
break;
case “fri”
echo “Today is Friday”;
break; ‘
case “sat”
echo “Today is Saturday”;
break;
case “sun”:
echo “Today is Sunday”;
break;
default:
echo “Thank you”; break;
}
?>
Question 13.
Define Conditional Statements in PHP.
Answer:
Conditional statements in PHP are useful writing decision making logics. It is most important feature of many programming language.
Question 14.
Define if statement in PHP.
Answer:
If statement is a conditional branching statement. If statement executes a statement or a group of statements if a specific condition is satisfied as per the user expectation.
Question 15.
What is if else statement in PHP?
Answer:
If… else statement in PHP is the if statement executes a statement or a group of statements if a specific condition is satisfied by the user expectation. When the condition gets falls the else block is executed.
Question 16.
List out Conditional Statements in PHP.
Answer:
The conditional statements in PHP are
if statement, if…else statement,
if… elseif… else statement, switch statement.
Question 17.
Write Syntax of the If else statement in PHP.
Answer:
Syntax:
if (condition)
{
Execute statement(s) if condition is true;
}
else
{
Execute statement(s) if condition is false;
}
Question 18.
Define if…elseif….else Statement in PHP.
Answer:
If … elseif… else statement in PHP is a combination of if…else statement. More than one statement can execute the condition based on user needs.
Question 19.
Usage of Switch Statement in PHP.
Answer:
- The switch statement is used to perform different actions based on different conditions.
- Switch statement works the same as if statements but they can check for multiple values at a time.
Question 20.
Write Syntax of Switch statement.
Answer:
Syntax:
Switch (n)
{
case label 1:
code to be executed if n = label 1;
break;
case label 2:
code to be executed if n = label 2;
break;
case label 3:
code to be executed if n = label 3;
break;
——–
——–
default:
code to be executed if n is different from all labels;
}
Question 21.
Compare if and if else statement.
Answer:
If statement |
If… else statement |
The if statement is used to execute a block of code only if the specified condition evaluates to true. | The if…else statement allows you to execute one block of code if the specified condition is evaluates to true and another block of code if it is evaluates to false. |
The if statement is quite basic because in if statement output will display when condition must be true, if condition is false it display nothing. | If…else statements allows you to display output in both the conditions, that is if condition is true display some message otherwise display other message. |
Question 22.
Write the features Conditional Statements in PHP.
Answer:
- PHP programmers evaluate different conditions during of a program and take decisions based on whether these conditions evaluate to true or false.
- if statement – executes some code if one condition is true.
- if., .else statement – executes some code if a condition is true and another code if that condition is false.
- if…elseif… else statement – executes different codes for more than two conditions.
- Switch statement – selects one of many blocks of code to be executed.
Question 23.
Write the purpose of if elseif else statement.
Answer:
- When we want to execute different code for different set of conditions, and we have more than two possible conditions, then we use if…elseif… else statement.
- We have also used logical operator are very useful while writing multiple condition if statement.
- When use if…elseif…else statement as many as you want, each condition inside if ( ) can accept logical operators like AND, OR, NOT.
- If… elseif…else statement is a combination of if-else statement.
Question 24.
Differentiate Switch and if else statement.
Answer:
Switch statement |
if…else statement |
Switch statement is to use when the same value is compared against a set. of values. | If…else can be used for any occasion where comparison need to be done. |
In switch statement usually there are no conditions to evaluate, it can bit efficient over an elseif counter part. | Each if statement checks a condition and operands associated with the condition can be different from one if to another attached elseif. |
The switch statements work the same as if statements but they can check for multiple values at a time. | The if statement is a way to make decision based upon the result of a condition. |
Question 25.
Write Short notes on Switch statement.
Answer:
(i) Use the switch statement to select one of many blocks of code to be executed.
(ii) The switch statement is used to perform different action based on different conditions.
Syntax:
switch (n)
{
case label 1:
code to be executed if n = label 1;
break;
case label 2:
code to be executed if n = label 2;
break;
case lebel 3;
code to be executed if n = label 3;
break;
default:
code to be executed if n is different from all blocks;
}
(iii) The switch statements work the same as if statements but they can check for multiple values at a time.
(iv) The break statement is used to prevent the code from running into the next case automatically.
(v) The default statement is used if no match is found.
Question 26.
Differentiate if statement and if elseif else statement.
Answer:
if statement |
if…elseif…else statement |
The if statement is used to execute a block of code only if the specified condition evaluates to true. | The if…elseif…else is a special statement that is used to combine multiple if…else statement. |
If statement executes a statement if a specific condition is satisfied as per the user expectation. | If…elseif…else statement is a combination of if – else statement. |
If statement is used simple decision making logics. | If…elseif…else statement is more than one statement can execute the condition based on user needs. |
Question 27.
Explain Function Conditional Statements in PHP.
Answer:
The PHP conditional statements are –
(i) If statement
(ii) If…else statement
(iii) If…elseif… else statement
(iv) Switch statement
(i) If statement:
This statement executes a statement or a group of statements if a specific condition is satisfied as per the user expectation.
Syntax:
if (condition)
{
Execute statements if condition is true;
}
Eg:
<?php
$pass_mark = 35;
$student_mark =70;
if ($student_mark > = $pass_mark)
{
echo “The student is eligible for promotion”;
}
?>
(ii) If…else statement:
This statement executes a statement or a group of statements if a specific condition is satisfied by the user expectation. When the condition gets false the else block is executed.
Syntax:
if (condition)
{
Execute statement(s) if condition is true;
}
else
{
Execute statement(s) if condition is false;
)
Eg:
<?php
$pass_mark = 35;
$student_mark = 70;
if ($student_mark > = $pass_mark)
{
echo “The student passed”;
}
else
{
echo “The student failed”;
}
?>
(iii) If…elseif… else statement:
This statement is a combination of if ..else statement. More than one statement can execute the condition based on user needs..
Syntax:
if (condition-1)
{
Execute statements if condition is true;
}
elseif (condition-2)
{
Execute statements if condition is true;
}
else
{
Execute statements if both conditions are false;
}
Eg:
<?php
$pass_mark = 35;
$first_class = 60;
$student_mark =70;
if ($student_mark > = $first_class)
{
echo “The student passed with first class”;
}
elseif ($student_mark > = $pass_mark.)
{
echo “The student passed”;
}
else
{
echo “The student failed”;
}
?>
(iv) Switch statement:
Refer III- Q.No – 4
Question 28.
Discuss in detail about Switch statement with an example.
Answer:
The switch statement is used to perform different actions based on different conditions.
Syntax:
switch(n)
{
case label 1:
code to be executed if n = label 1;
break;
case label 2:
code to be executed if n = label 2;
break;
case label 3:
code to be executed if n = label 3;
break;
default:
code to be executed if n is different from all labels;
}
Eg:
<?php
” $f col.br = “red”;
switch ($fcolor)
{
case “red”;
echo “my favourite color is red”;
break;
case “blue”;
echo “my favourite color is blue”;
break;
case “green”:
echo “my favourite color is greeh”;
break;
default:
echo “my favourite color is neither Red, Blue, nor
green!”;
}
?>
Question 29.
Explain the process Conditional Statements in PHP?
Answer:
(i) PHP lets programmers evaluate different conditions during process of a program and take decisions based on whether these conditions evaluate to true or false.
(ii) These conditions, and the actions associated with them, are expressed by means of a programming construct called a process conditional statement. PHP supports different types of process conditional statements.
(a) The if statement: In if statements output will appear when only condition must be true.
(b) The if…else statement: If-else statements allows you to display output in both the condition if condition is true or. false.
(c) The if…elseif…else statement: The if…elseif…else statement lets you chain together multiple if-else statements, thus allowing the programmer to define actions for more than just two possible outcomes.
(d) The switch statement: The switch statement is similar to a series of if statements on the same expression.
Question 30.
Explain concepts of if… elseif… else statement.
Answer:
If…elseif…else statement is a combination of If…else statement. More than one statement can execute the condition based on user needs.
The if…elseif…else statement is a special statement that is used to combine multiple if., else statements.
If…elseif…else statement is more than one statement can execute the condition based on user needs.
Syntax:
If(condition-1)
{
Execute statements if condition is true;
}
elseif (condition-2)
{
Execute statements if 2nd condition is true;
}
else
{
Execute statements if both condition are false;
}
Eg:
<?php
$pass_mark =35;
$first_class = 60;
$student_mark =70;
if ($student_mark> = $first_class)
{
echo “The student passed with first class”;
}
elseif ($student_mark > = $pass_mark)
{
echo “The student passed”;
}
else
{
echo “The student failed”;
}
?>
Question 31.
Explain if else statement in PHP.
Answer:
If…else statement executes a statement or a group of statements if a specific condition is satisfied by the user expectation. When the condition gets false ‘ the else block is executed.
If…else can be used for any occasion where comparison need to be done.
If…else statement is a way to make decision based upon the result of a condition.
Syntax:
if (condition)
{
Execute statement(s) if condition is true;
}
else
Execute statement(s) if condition is false;
}
Eg:
<?php
$pass_mark = 35; .
$student_mark = 70;
if ($student_mark> = pass_mark)
{
echo “The student passed” ;
}
else
echo “The student failed”
}
?>
Question 32.
Create simple decision making scenario using if else statement in student management application.
Answer:
If…else:
If it raining outside, you don’t have to water the plants by yourself. So you can tell. If it is raining, I shall not water the plants, else I have to water them. So .now, we can write pseudo code.
if (raining==true)
do not water
else
do water .
Nested if…else:
If a boy is more than 13 years old, we call him teenager. If he is more than 21 years old, he is eligible for marriage. Using nested If…else.
If a boy is less than 13 years old he is not a teenager, else if he is less than 21 . years old he is not eligible for marriage else he is eligible.
Pseudo code:
If (age < 13)
“Not a Teenager”
else
{ if (age < 21)
“Not a eligible for marriage”
else
“eligible for marriage”
}
Question 33.
Explain real world usages in using Conditional Statements.
Answer:
A conditional statement is a statement that is stated in if… then format. This kind of statement is kind of statement is something that is often used to write a hypothesis in science.
The hypothesis can be created before a test is ever imagined, and the test is then designed to test the hypothesis. On the otherhand, the text might be known, and the conditional statement is then used to predict the outcome of the experiment.
Example I:
If you believe “money can’t buy happiness”
then
you’re not “spending it correctly”.
Example II:
If ”a person spends the money on himself”,
then
“he will be happier at the end of the day”.
else
If”a person doesn’t speñd the money on himself”,
then
“he will be happier at the end of the day”.
Example – III:
If son said “I do my homework”
then
Father represent “I get my payment”
The statement son → father is a conditional statement which represent “if son, then father”.
Choose the correct answer:
Question 1.
Which statement is a combination of if-else statement?
(a) If statement
(b) break statement
(c) If…elseif…else statement
(d) If…else statement
Answer:
(c) If…elseif…else statement
Question 2.
Which statement is used to perform different actions based on different conditions?
(a) If statement
(b) Switch statement
(c)If…elseif…else statement
(d) If.. .elsestatement
Answer:
(b) Switch statement
Question 3.
Which statement contains boolean expression inside brackets?
(a) if statement
(b) switch statement
(c) break statement
(d) default statement
Answer:
(a) if statement
Question 4.
Which statement executes a statement if a specific condition is satisfied?
(a) if statement
(b) switch statement
(c) break statement
(d) default statement
Answer:
(a) if statement
Question 5.
What type of statements are useful for writing decision making logics?
(a) looping
(b) conditional
(c) branch statement
(d) All the above
Answer:
(b) conditional
Question 6.
What will be the output in the PHP code?
<?php
$x = 0; ‘
if ($x = = 1) ’
if ($x > = O’) ‘
print “true”; .
else
print “false”;
?>
(a) true
(b) false
(c) error
(d) no output
Answer:
(d) no output
Question 7.
What will be the output in the PHP code?
<? php
$a = 1′; .
if ($a—) print “True”;
if ($a++)
print “False”; .
?>
(a) true
(b) false
(c) error
(d) no output
Answer:
(a) true
Question 8.
What will be the output in the PHP code?
<? php
$a « 1;
If (print $a)
print “true”;
else
print “false”;
?>
(a) true
(b) false
(c) error
(d) no output
Answer:
(a) true
Question 9.
What will be the output in the PHP code?
<? php
$a = ”
if ($a) .
print “all”;
if
else
print “some”;
?>
(a) all
(b) some
(c) error
(d) no output
Answer:
(b) some
Question 10.
The following syntax is correct for if conditional statement.
if condition
{
code
}
(a) true
(b) false
(c) error
(d) not correct
Answer:
(a) true
Question 11.
If <expression> Where, the expression can be of which type?
(a) True
(b) Any number
(c) Any string
(d) All the above
Answer:
(d) All the above
Question 12.
What error does the if condition gives if not terminated with end statement?
(a) Syntax error
(b) Unexpected end
(c) Expecting keyword end
(d) All the above
Answer:
(d) All the above
Question 13.
What is output?
if 1 < 2
Print “One is less than Two”;
end
(a) error
(b) syntax error
(c) one is less than two
(d) none
Answer:
(c) one is less than two
Question 14.
What is output?
if 11 < 2
Print “Eleven is less than two”
end
print “11 is greater”
(a) 11 is greater
(b) Eleven is less than two
(c) Eleven is less than two 11 is greater
(d) None
Answer:
(a) 11 is greater
Question 15.
What is output?
<? php
$x = 12; .
if ($x >0)
{
echo “positive number”
}
else
{
echo “Negative number”
}
?>
(a) positive
(b) negative
(c) error
(d) no output
Answer:
(a) positive
Question 16.
Match the following:
(A) If | (i) group of statement |
(B) If – else | (ii) a statement |
(C) If – elseif – else | (iii) multiple values |
(D) Switch | (iv) combination of if – else statement |
(a) (A) – (ii); (B) – (iii); (C) – (i); (D) – (iv)
(b) (A) – (ii); (B) – (i); (C) – (iv); (D) – (iii)
(c) (A) – (iv); (B) – (iii); (C) – (i); (D) – (ii)
(d) (A) – (iv); (B) – (i); (C) – (ii); (D) – (iii)
Answer:
(b) (A) – (ii); (B) – (i); (C) – (iv); (D) – (iii)
Question 17.
Choose the incorrect pair:
(a) condition – decision
(b) if – control statement
(c) switch – multiple statement
(d) else if – another condition
Answer:
(b) if – control statement
Question 18.
Choose the incorrect statement:
(a) Switch statement work with same as while statements.
(b) The if statement is way to make decision based statement.
(c) If statement contains boolean expression inside brackets.
(d) The else statement must follow if or else if statement.
Answer:
(a) Switch statement work with same as while statements.
Question 19.
Assertion (A):
Conditional statements are useful for writing decision making logics.
Reason (R):
It is most important feature of many programming languages, including PHP
(a) Both A and R are true, and R is the correct explanation for A.
(b) Both A and R are true, but R is not the correct explanation for A.
(c A is true, But R is false.
(d) A is false, But R is true.
Answer:
(a) Both A and R are true, and R is the correct explanation for A.
Question 20.
Pick the odd one out:
(a) If
(b) If…else
(c) break
(d) Switch
Answer:
(c) break
Question 21.
What will be the output of the following PHP code?
<?php
$x;
if ($x)
print “hi” ;
else
print “how are u”;
?>
(a) how are u
(b) hi
(c) error
(d) no output
Answer:
(a) how are u
Question 22.
What will be the output of the following PHP code ?
<?php
$x = 0;
if ($x++)
print “hi”; .
else
print “how are u”;
?>
(a) hi
(b) no output
(c) error
(d) how are u
Answer:
(d) how are u
Question 23.
What will be the output of the following PHP code?
<?php
$x;
if ($x == 0)
print “hi” ;
else
print “how are u”;
print “hello”
?>
(a) how are uhello
(b) hihello
(c) hi
(d) no output
Answer:
(b) hihello
Question 24.
Statement which is used to make choice between two options and only option is to be performed is written as:
(a) if statement
(b) if else statement
(c) then else statement
(d) else one statement
Answer:
(b) if else statement
Question 25.
What will be the output of the following PHP code ?
<?php
$a = “”;
if ($a)
print “all”;
if
else
print “some”;
?>
(a) all
(b) some
(c) error
(d) no output
Answer:
(b) some
Question 26.
What will be the output of the following PHP code?
< ?php
$a = “” ;
if ($a)
print “all”;
if
else
print “some”;
?>
(a) all
(b) some
(c) error
(d) no output
Answer:
(b) some
Question 27.
What will be the output of the following PHP code?
<?php
$x = 10;
$y = 20;
if ($x > $y + $y != 3)
print “hi” ;
else
print “how are u”;
?>
(a) how are u
(b) hi
(c) error
(d) no output
Answer:
(b) hi
Question 28.
What will be the output of the following PHP code?
<?php
$x = 10;
$y = 20;
if ($x > $y && 1||1)
print “hi” ;
else
print “how are u”;
?>
(a) how are u
(b) hi
(c) error
(d) no output
Answer:
(b) hi
Question 29.
What will be the output of the following PHP code ?
<?php
if .(-100)
print “hi” ;
else
print “how are u”;
?>
(a) how are u
(b) hi
(c) error
(d) no output
Answer:
(b) hi