Вы находитесь на странице: 1из 3

Ch6: Control Structures (Contd.

1. What is the difference between selection and repetition?


Ans: Selection statements test conditions and selectively execute code depending on the outcome of the test
condition, whereas repetition statements repeat a set of statements till a test condition is satisfied.

2. Explain the use of for statement along with its syntax.


Ans: The for loop repeat a set of statements till a test condition is satisfied. The syntax of the for loop is:
Syntax:
for( initialization; test exp; increment/decrement exp)
{
statements;
}

3. What is the main difference between a while loop and a do while loop?
Ans. While loop is entry control loop and do…while loop is exit control loop. While loop works only if the
given condition is true where as do…while loop works minimum works for one time and then check for
the condition, it works further if given condition is true.

4. What are loops?


Ans. A loop decides how many times to execute another statement. There are three kinds of loops:
i. For loop - are (typically) used to execute the controlled statement a given number of times.
ii. While loop - test whether a condition is true before executing the controlled statement.
iii. Do…while loop - test whether a condition is true after executing the controlled statement.

5. What are Entry Control loops? Give example.


Ans, In Entry controlled loop the test condition is checked first and if that condition is true than the
block of statement in the loop body will be executed.
Example: For loop and While loop are the example of Entry Control Loop

6. What is Exit Control loop? Give example.


Ans. In exit controlled loop the body of loop will be executed first and at the end the test condition
is checked, if condition is satisfied than body of loop will be executed again.
Example: Do…While loop is the example of Exit Control Loop.

7. What is the problem of dangling else? How can it be overridden?


Ans. The dangling else is a problem in computer programming in which an optional else clause in an if–
then(–else) statement results in nested conditionals being ambiguous.
Example:
if ( num > 0 )
if ( num < 10 )
System.out.println( "Number in between 0 and 10" ) ;
else
System.out.println( "Number is out of Range" ) ;

In this example else statement belongs to which if statement? It can match with both if statement. To
avoid this problem we use { } braces.

if ( num > 0 )
{
if ( num < 10 )
System.out.println( "Number in between 0 and 10" ) ;
}
else
System.out.println( "Number is out of Range" ) ;

8. Which expression is optional in for loop? When empty loops are useful?
Ans. All three options of for loop are optional. We use empty loops when we want delay in the program.

9. Name the controls that you will use to enter your Phone number of a student in a form.
Ans. Text Field

10. Name the controls that you will use to enter your Stream in a form.
Ans. Radio Button

11. Name the controls that you will use to get student email address in a form.
Ans. Text Field.

12. What is infinite loop in Java?


Ans. An infinite loop (also known as an endless loop or unproductive loop) is a sequence of instructions in a
computer program which loops endlessly, either due to the loop having no terminating condition, having
one that can never be met, or one that causes the loop to start over.

13. Answer the following questions:


a. Rewrite the following program using if statement.
int c=jComboBox1.getSelectedIndex(); int c=jComboBox1.getSelectedIndex();
switch(c) if (c= =0)
{ case 0: FinalAmt= billAmt; break; FinalAmt= billAmt;
case 1: FinalAmt= 0.9*billAmt; break; else if (c= =1)
case2: FinalAmt=0.8* billAmt; break; FinalAmt= billAmt;
default: FinalAmt= billAmt; break; else if (c= =2)
} FinalAmt= billAmt;
else
FinalAmt= billAmt;

b. How many times the following while loop get executed?


int p=5; Rough Work
int q=36; P=5, 11, 17, 23, 29, 35
while(p<=q) q=36
{ P+=6; The value of P changes six times, ans is 6.
}
Ans. 6

c. Rewrite the following code using a for loop :


int i=1, sum=0; int i=1, sum=0;
while (i<10) for(;i<10;)
{ {
sum+=i; sum+=i;
i+=2; i+=2;
} }

Try it yourself
1. How many times will the following loop get executed?
x = 5;
y = 36;
while ( x <= y)
{
x+=6;
}
2. What will be the content of the jTextArea1 after executing the following code?
int Num = 1;
do
{
jTextArea1.setText(Integer.toString(++Num) + "\n");
Num = Num + 1;
}while(Num<=10)
3. What are relational operators? Explain with the help of suitable examples.
4. What will be the content of jTextField1 after executing the following code:
int Num = 6;
Num = Num + 1;
if ( Num > 5)
jTextField1.setText(Integer.toString(Num));
else
jTextField1.setText(Integer.toString(Num+5));

5. What will be the corresponding outputs of the following code segment if the possible inputs in the
jTextField1 are:
(i) 10 (ii) 20 (iii) 30 (iv) 40 (v) 50
String str = jTextField1.getText();
Number = Integer.parseInt(str);
Switch (Number)
{
case 10:jTextField1.setText("Ten Thousand");break;
case 20:jTextField1.setText ("Twenty Thousand");
case 30:jTextField1.setText ("Thirty Thousand"); break;
case 40:jTextField1.setText ("Forty Thousand");
default:jTextField1.setText ("Not enough!!!");
}
6. Find the output of the following code:
int First = 7;
int Second = 73;
First++;
if (First+Second > 90)
jlabel1.setText("value is 90 ");
else
jlabel1.setText("value is not 90 ");
int Number1 = 7,Number2=8;
int Second = 73;
if (Number1>0 || Number2>5)
if (Number1>7)
jTextField1.setText("Code Worked");
else
jTextField1.setText("Code MightWork");
else
jTextField1.setText("Code will not Work");

Вам также может понравиться