TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 1.
When to use while loops?
Answer:

  • While loops are used to execute a block of code until a certain condition becomes true.
  • We can use a while loop to read records returned from a database query.

Question 2.
Write the type of while loops.
Answer:
The two types of while loops are

  1. Do..while – Executes the block of code atleast once before evaluating the condition.
  2. While – Check the condition first. If it evaluates to true, the block of code is executed as long as the condition is true. If it evaluates to false, the execution of the while loop is terminated.

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 3.
What will be the output in the php code?
Answer:
<?php $1 = 0;
while ($i < 3)
{
. echo $i +1. “<br>”;
$i ++;
}
?>
Output: 1
2
3

Question 4.
What will be the output in the php code?
Answer:
<?php
Scolors = array (“red”, “green”, “blue”, “yellow”); foreach (Scolors as Svalue)
{
echo “Svalue <br>;
>
?>
Output: red
green
blue
yellow

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 5.
What is output in the php code?
Answer:
<?php
for($i = 1; $i < 10; $i++)
{
echo $i;
} –
Output: 1 2 3 4 5 6 7 8 9

Question 6.
Write a php program using for loop in the following pattern.
1
2 3
4 5 6
7 8 9 10
Answer:
<?php
$k = 1;
for ($i =0; $i < 4; $i++)
{
for ($j = 0;$j <= $i; $j++)
{
echo $k ”
$k++;
}
echo “<br>”;
}
?>

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 7.
Write a php program using for loop in the following pattern?
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Answer:
<?php
for ($i = 0; $i >= 5; $i++)
{
for ($j = 1; $j >= $i, $j++)
{
echo $i;
} ‘
echo “<br>”;
}
?>

Question 8.
Write a PHP program using for statement in the following pattern.
Answer:
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
Answer:
<?php
for($i = 0; $i <= 5; $i++) {
for($j = 1; $j <= $i; $j++) {
echo “1”;
}
echo “<br>”;
}
?>

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 9.
Write a PhP program using for statement in the following pattern.
1 1 1 1 1
1 1 1 1
1 1 1
1 1
1
Answer:
<?php
for($i = 0; $i <= 5; $i++)
{ . for($j = 5 – $i; $j >= 1; $j–)
{
echo “1”;
}
echo “<br>”;
}
?>

Question 10.
Write a PhP program using for statement in the following pattern.
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
Answer:
<?php
for ($i = 5; $i >= 1; $i —)
{
for ($j = $i; $j >= 1; $j —)
{
echo $i.” “;
}
echo “<br>”;
}
?>

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 11.
Write a PHP program using for statement in the following pattern.
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Answer:
<?php
for ($i =1; $i <= 5; $i++)
{
for ($j = 1; $j <= 5; $j++)
{
echo ;
}
echo “<br>”;
?>

Question 12.
What will be output?
Answer:
<?php
$n = ( );
while ($n <= 10)
{
echo “$n <br/>”;
$n++;
}
?>
Output: 0 1 2 3 4 5 6 7 8 9 10

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 13.
Define Looping Structure in PHP.
Answer:
In PHP programming it is often necessary to repeat the same block of code a given number of times, or until a certain condition is met. This can be accomplished using looping structure.

Question 14.
Define For loop in PHP.
Answer:
For loop is an important function looping system which is used for iteration logics when the programmer know in advance how many times the loop should run.

Question 15.
What is Foreach loop in PHP?
Answer:

  1. Foreach loop is exclusively available in PHP. It works only with arrays. The loop iteration depends on each KEY value pair in the array.
  2. Foreach, loop iteration the value of the current array element is assigned to $ value variable and the array pointer is shifted by one, until it reaches the end of the array element.

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 16.
List out Looping Structure in PHP.
Answer:

  • For loop
  • Foreach loop
  • While loop

Question 17.
Write Syntax of For Loop in PHP.
Answer:
The syntax of For Loop in PHP is
for (init counter; test counter; increment counter)
{
code to be executed;
}
Where,
init counter – Initialize the loop initial counter value,
test counter – Evaluated for every iteration of the loop,
increment counter – Increases the loop counter

Question 18.
Write Syntax of Foreach Loop in PHP.
Answer:
The syntax of Foreach loop in PHP is
for each ($array as $value)
{
code to be executed:
}

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 19.
Write Syntax of while loop in PHP.
Answer:
The syntax of while loop in PHP is while (condition is true)
{
code to be executed;
}

Question 20.
Write Syntax of Do while loop in PHP.
Answer:
The syntax of Do while loop in PHP is
do
{
code to be executed;
}
while (condition is true);

Question 21.
Compare For loop and Foreach loop.
Answer:

For loop

 For each loop

For loop is a functional looping system which is used for iteration logics.  Foreach loop works only with arrays. The loop iteration depends on each KEY value pair in the array.
For loop executes a block of code a specified number of times.  The Foreach construct provides an easy way to iterate over arrays.

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 22.
Usage of Foreach loop in PHP.
Answer:
Foreach loop in PHP is used loop iteration the value of the current array element is assigned to $ value variable and the array pointer is shifted by one, until it reaches the end of the array element.

Question 23.
Write the features Looping Structure.
Answer:
Looping structure in PHP are used to execute the same block of code a specified number of times. The features of looping structure are

  1. For – loops through a block of code a specified number of times.
  2. While – loops through a block of code if and as long as a specified condition is true.
  3. Do.. while – loops through a block of code once, and then repeats the loop as long as long as a special condition is true.
  4. Foreach – loops through a block of code for each element in an array.

Question 24.
Write the purpose of Looping Structure in PHP.
Answer:

  1. Looping structure in PHP is main purpose to execute a statement or a block of statements, multiple times until and unless a specific condition is met.
  2. Looping structure, the user to save both time and effort of writing the same code multiple times.
  3. PHP supports four types of looping structure for loop, while loop, do- while loop and foreach loop.

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 25.
Differentiate Foreach and While loop.
Answer:

Foreach loop

 While loop

Foreach loops through a block of code for each element in an array.  While loops through a block of code until the condition is evaluate true.
Foreach loop, we don’t have any idea about the number of iterations.  While loop, we know already that howmany times the loop should be run.
Foreach loop is used to iterate only arrays and objects.  A while loop executes orders until a certain condition is met.

Question 26.
Write short notes on Do While Loop.
Answer:
Do while loop in PHP is a variant of while loop, which evaluates the condition at the end of each loop iteration.

A Do while loop the block of code executed one, and then the condition is evaluated, if foe condition is true, the statement is repeated as long as the specified condition evaluated to is true.
Do while syntax:
do
{
code to be executed;
}
while (condition is true);
Eg:
<?php
$i = I
do
{
$i++;
echo “The number is” .$i. “<br>”;
}
while ($i <= 3);
?>

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 27.
Differentiate While and Do while loops.
Answer:

While loop

 Do while loop

While loop, the condition to be evaluated is tested at the begining of each loop iteration. Do.. while loop, the condition is evaluated at the end of the loop iteration rather than the begining.
If the conditional expression evaluates to false, the loop will never be executed. With a do – while loop, on the other hand the loop will always be executed once, even if the conditional expression is false.
While loops execute a block of code while the specified condition is true. Do – while – loops through a block of code once, and then repeats the loop as long as the specified condition is true.

Question 28.
Explain Looping Structure in PHP.
Answer:

TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure 1

This flow chart explain, if it evaluates true, the loop continues, otherwise the loop exit to outside block.
Looping structures are useful for writing iteration logics. It is the most important feature of many programming languages, including PHP.
They are implemented using the following loop structure.
• For loop
• Foreach loop
• While loop

(i) For loop:
For loop is an important functional looping system which is used for iteration logics when the programmer know in advance how many times the loop should run.
Syntax:
for (init counter; test counter; increment counter!
{
code to be executed;
}

(ii) Foreach loop:
Foreach loop is exclusively available in PHP. It works only with arrays. The loop iteration depends on each key value pair in the array.
Foreach, loop iteration the value of the current array element is assigned to $ value variable and the array pointer is shifted by one, until it reaches the end of the array element.
Syntax:
foreach ($ array as $ value)
{
code to be executed;
}

(iii) While loop:
While loop is an important feature which is used for simple iteration logics. It is checking the condition whether true or false. It executes the loop if specified condition it true.
Syntax:
while(condition)
{
code to be executed;
}

(iv) Do While loop:
Do while loop always run the statement inside of the loop block at the firstime execution. Then it is checking the condition whether true or false. It executes the loop, if the specified condition is true.
Syntax:
do
{
code to executed;
}
while (condition is true);

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 29.
Discuss in detail about For each loop.
Answer:
(i) Foreach loop is used to iterate over arrays.
(ii) Foreach loop is exclusively available in PHP. It works only with arrays.
(iii) The loop iteration depends on each key value pair in the array.
(iv) Foreach, loop iteration the value of the current array element is assigned to $ value variable and the array pointer is shifted by one, until it reaches the end of the array element.
Syntax:
foreach ($ array as $ value)
{
code to be executed;
}
Eg: seq = [50, 100, 150]
foreach X of seq
Print x
end
Foreach – supports iteration over three different kind of values arrays, Normal objects, Traversable objects.

Question 30.
Explain the process Do while loop.
Answer:
Do while loop always run the statement inside of the loop block at the first execution. Then it is checking the condition whether true or false. It executes the loop, if the specified condition is true.

TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure 2

In the above, Do while flowchart is working, run the looping code first and then check for the condition. If the specified condition is true to execute the block of code, else stop or exit the loop.
Syntax:
do
{
code to be executed;
}
while (condition is true);
Eg:
<?php
$count = 10;
$number =1;
do
{
echo “number”; $number++;
}
while ($number <= $count)
?>
The $count variable value is 10, and Snumber variable initial value 1, it’s incremented every times by 1, when it reached the Snumber value is 10, then the loop will be closed.

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 31.
Explain concepts of For loop with example.
Answer:
For loop:
For loop is an important functional looping system which is used for iteration logics when the programmer know in advance how many times the loop should run.
Syntax:
forfinit counter; test counter; increment counter)
{
code to be executed;
}
Where,
init counter – Initialize the loop initial counter value,
test counter – Evaluated for every iteration of the loop, if it evaluates true, the loop continues. If it evaluates to FALSE, the loop ends.
Increment counter – Increases the loop counter value.
Eg:
<?php
for ($i = 0; $i< = 10; $i++)
{
echo “The number is : $i <br>”;
}
?>

For loop structure is

TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure 3

The for structure will be executed again and again when the condition is true, otherwise exit from the loop.

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 32.
Explain array concepts in Looping Structure.
Answer:
(i) The Foreach loop is mainly used for looping through the values of an array.
(ii) It loops over the array, and each value for the current array element is assigned to $ value, and the array pointer is advanced by one to go the next element on the array.
(iii) Foreach loop is exclusively available in PHP. It works only with arrays.
(iv) The loop iteration depends on each key value pair in the array.
(v) Foreach, loop iteration the value of the current array element is assigned to $ value variable and the array pointer is shifted by one, until it reaches the end of the array element.
(vi) The Foreach construct provides an easy way to iterate over arrays.
Syntax for Foreach loop:
foreach ($ array as $ value)
{
code to be executed;
}
Eg:
m = [10, 20, 30]
foreach i of m
print i
end

Question 33.
Create simple array element and display the values using Foreach.
Answer:
<?php
$marks = array (“98”, “78”, “88”, “90”);
foreach($marks as $value)
{
echo “$value <br>”;
}
?>
Output:
98
78
88
90

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 34.
Explain real world usages in using looping structure.
Answer:

  1. Using facebook as an example for real world usage in using looping structure.
  2. Chatting, Sending message, Photos, articles etc., are using in our account that are may reached around the world wide.
  3. Click to view our friends list. Every body has a variable number of friends, so need code to iterate over each friend to display their photo, name, and profile link.
  4. In facebook update our friend request/ messages/ notification at the top of the page.
  5. This would generally use a loop as we would not want to be requesting an update constantly yet we do want the browser to check periodically for an update.
  6. As far as how loops would be used for facebook – style application, it would mainly displaying a sequence of entries, such as list of friends or people.
  7. We would retrieve the list or array structure, and then use a loop to walk through the list and generate something like to display.

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Choose the correct answer:

Question 1.
What will be output in the PHP code?
<?php
$i = 0
while ($i < 5)
{
echo $i + 1. “<br>”;
$i++;
}
?>
(a) 1
2
3
4

(b) 1
2
3
4
5

(c) 1
2
3
4
5
6

(d) 0
1
2
3
4
5
Answer:
(b) 1
2
3
4
5

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 2.
What will be the output in the PhP code?
<?php
$i = 9;
do
{
echo “$i is”. “<br>”;
}
while ($i < 9);
?>
(a) 10
(b) 8
(c) 9
(d) 11
Answer:
(c) 9

Question 3.
Which loop works only arrays and objects?
(a) For
(b) Foreach
(c) While
(d) Do while
Answer:
(b) Foreach

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 4.
Which loop always run the statement inside of the loop block?
(a) For
(b) Foreach
(c) While
(d) Do while
Answer:
(d) Do while

Question 5.
Which loop is used for simple iteration logic?
(a) For
(b) Foreach
(c) While
(d) Do while
Answer:
(c) While

Question 6.
Which loop is exclusively available in PHP?
(a) For
(b) Foreach
(c) While
(d) Do while
Answer:
(b) Foreach

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 7.
Which loops execute through a block of code as long as the specified condition is true?
(a) For
(b) Foreach
(c) While
(d) Do while
Answer:
(c) While

Question 8.
Which loops execute through a block of code once, and then repeats the loop as long as the specified condition is true?
(a) For
(b) Foreach
(c) While
(d) Do while
Answer:
(d) Do while

Question 9.
Which loops execute through block of code a specified number of times?
(a) For
(b) Foreach
(c) While
(d) Do while
Answer:
(a) For

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 10.
Which loops execute through a block of code for each element in an array?
(a) For
(b) Foreach
(c) While
(d) Do while
Answer:
(b) Foreach

Question 11.
Match the following:

(A) For  (i) Arrays
(B) For each  (ii) again and again
(C) While  (iii) check at the end
(D) Do while  (iv) check initially

(a) (A) – (iv); (B) – (i); (C) – (iv); (D) – (iii)
(b) (A) – (ii); (B) – (iii); (C) – (i); (D) – (iv)
(c) (A) – (iv); (B) – (iii); (C) – (ii); (D) – (i)
(d) (A) – (iv); (B) – (i); (C) – (iii); (D) – (i)
Answer:
(a) (A) – (iv); (B) – (i); (C) – (iv); (D) – (iii)

Question 12.
Choose the incorrect pair: (php variable assigning)
(a) $num = 111;
(b) num = 111;
(c) $num = “111”;
(d) $num =1+2;
Answer:
(b) num = 111;

Question 13.
Choose the incorrect statement:
(a) In For loop, init counter to initialize the loop value.
(b) In For loop, Evaluated for every iteration of the loop.
(c) Increment counter, Increase the loop counter value.
(d) Do-while loop works only with arrays.
Answer:
(d) Do-while loop works only with arrays.

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 14.
Assertion (A):
Do..while loops through a block of code once, and then repeats the loop as long as the specified condition is true.
Reason (R):
The while loops execute a block of code while the specified condition is true.
(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:
(b) Both A and R are true, but R is not the correct explanation for A.

Question 15.
Pick the odd one out.
(a) For
(b) Foreach
(c) If.. else
(d) While
Answer:
(c) If., else

Question 16.
Most complicated looping structure is:
(a) While
(b) Do While
(c) For
(d) None of them
Answer:
(c) For

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 17.
Loops that iterate for fixed number of times is called:
(a) Unbounded loops
(b) Bounded loops
(c) While loops
(d) For loops
Answer:
(d) For loops

Question 18.
Which loop evaluates condition expression as Boolean, if it is true, it executes statements and when it is false it will terminate?
(a) For loop
(b) Foreach loop
(c) While loop
(d) All of them
Answer:
(c) While loop

Question 19.
Which loop evaluates condition expression as Boolean, if it is true, it executes statements and when it is false it will terminate?
(a) For loop
(b) For each loop
(c) While loop
(d) All of them
Answer:
(c) While loop

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 20.
What will be displayed in a browser when the following PHP code is executed:
<?php
for ($counter = 20; $counter < 10; $counter++)
{
echo “Welcome to Tamilnadu”;
}
echo “Counter is: Scounter”;
?>
(a) Welcome to Tamilnadu
(b) Counter is: 20
(c) Welcome to Tamilnadu Counter is: 22
(d) Welcome to Tamilnadu Welcome to Tamilnadu Counter is: 22
Answer:
(b) Counter is: 20

Question 21.
What will be displayed in a browser when the following PHP code is executed:
<?php
for ($counter = 10; $counter < 10;
$counter = $counter + 5){
echo “Hello”;
}
?>
(a) Hello Hello Hello Hello Hello
(b) Hello Hello Hello
(c) Hello
(d) None of the above
Answer:
(d) None of the above

Question 22.
PHP supports four types of looping techniques:
(a) For loop
(b) While loop
(c) Foreach loop
(d) All the above
Answer:
(d) All the above

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 23.
Consider the following code
<? php
$count=12;
do{
printf(“%d squared=%d<br/>”,
$count, pow ($count,2));
} while($count<4);
?>
What will be the output of the code?
(a) 12 squared 141
(b) 12 squared=144
(c) “12 squared= 141”
(d) Execution error
Answer:
(b) 12 squared=144

 

Question 24.
What will be the output of the following PHP code ?
<?php
for ($x =1; $x < 10; ++$x)
print “*\t”;
}
?>
(a) * * * * * * * * * *
(b) * * * * * * * * *
(c) * * * * * * * * * * *
(d) Infinite loop
Answer:
(b) * * * * * * * * *

Samacheer Kalvi TN State Board 12th Computer Applications Important Questions Chapter 7 Looping Structure

Question 25.
What will be the output of the following PHP code ?
<?php
for ($x = -1; $x < 10;—$x)
{
print $x;
}
?>
(a) 123456713910412
(b) 123456713910
(c) 1234567139104
(d) Infinite loop
Answer:
(d) Infinite loop

TN Board 12th Computer Applications Important Questions