1. What are Control structures?
Ans: These are the programming statements, which controls the sequence of program execution.
Some statements are executed based on conditions.
Some statements gets executed repeated number of times.
2. When do we use goto statement?
Ans: To terminate a deeply nested looping structure
If a condition is satisfied then go up & execute a statement
( raindrops eg – for random colors – to skip black color )
up:
rnd = random( 16 );
if( rnd == 0 )
goto up;
textcolor( rnd );
3. If a semicolon is put at the end of [for, if, else, while etc]. Explain the results?
Ans:
Semicolon at the end of for loop
for(I=1;I<=10;I++);
printf(“%d\n”, I);
output : 11
Semicolon at the end of while loop
I = 1;
while( I <= 10 );
{
printf(“%d”, I);
}
output : Program runs for infinite times without printing anything
Semicolon at the end of if condition
Case I:
if (n > 10);
printf(“Hai\n”);
else
printf(“Bye”);
Error : hanging else
Case II:
if( n > 10 );
printf(“Hai\n”);
printf(“Bye”);
Output:
If n is > 10 then
Hai
If n <= 10 then output would be:
Hai
Bye
Semicolon at the end of else
if(n > 10 )
printf(“Hai”);
else;
printf(“Bye”);
Output: Bye would be printed always, The printed of Hai depends on the condition
4. When to use what loop?
Ans:
For loop:
If we have all the three sections directly (initialization; testing; modification)
Ex1 : for( I = 1; I<=n;I++)
Also these sections should be small statements
If modification should take place at the end
If we know in advance the number of times the loop should run.
While loop:
If we don’t have all the three sections directly
ex1: reverse a number while( n )
Ex: n = n/10;
Also if modification is in the middle of the loop block.
Ex: raindrops program
If we don’t know in advance, the number of times a loop should run.
Do- while loop
Same as that of while loop, except that it runs at least once.
6. Is it necessary to define a label with every goto statement? Justify your statement?
Ans: Yes. Otherwise how can it know where to transfer the control.
Ans: These are the programming statements, which controls the sequence of program execution.
Some statements are executed based on conditions.
Some statements gets executed repeated number of times.
2. When do we use goto statement?
Ans: To terminate a deeply nested looping structure
If a condition is satisfied then go up & execute a statement
( raindrops eg – for random colors – to skip black color )
up:
rnd = random( 16 );
if( rnd == 0 )
goto up;
textcolor( rnd );
3. If a semicolon is put at the end of [for, if, else, while etc]. Explain the results?
Ans:
Semicolon at the end of for loop
for(I=1;I<=10;I++);
printf(“%d\n”, I);
output : 11
Semicolon at the end of while loop
I = 1;
while( I <= 10 );
{
printf(“%d”, I);
}
output : Program runs for infinite times without printing anything
Semicolon at the end of if condition
Case I:
if (n > 10);
printf(“Hai\n”);
else
printf(“Bye”);
Error : hanging else
Case II:
if( n > 10 );
printf(“Hai\n”);
printf(“Bye”);
Output:
If n is > 10 then
Hai
If n <= 10 then output would be:
Hai
Bye
Semicolon at the end of else
if(n > 10 )
printf(“Hai”);
else;
printf(“Bye”);
Output: Bye would be printed always, The printed of Hai depends on the condition
4. When to use what loop?
Ans:
For loop:
If we have all the three sections directly (initialization; testing; modification)
Ex1 : for( I = 1; I<=n;I++)
Also these sections should be small statements
If modification should take place at the end
If we know in advance the number of times the loop should run.
While loop:
If we don’t have all the three sections directly
ex1: reverse a number while( n )
Ex: n = n/10;
Also if modification is in the middle of the loop block.
Ex: raindrops program
If we don’t know in advance, the number of times a loop should run.
Do- while loop
Same as that of while loop, except that it runs at least once.
6. Is it necessary to define a label with every goto statement? Justify your statement?
Ans: Yes. Otherwise how can it know where to transfer the control.