Amcat Automata Fix Control Statement
A control statement is a statement that determines whether or not different
statements can be executed.
Types of Control Statements:
There are three kinds of control statement, they are given
below
1) An if statement decides whether to execute another statement or
decides which of two statements to execute.
2) A loop decides how many times to execute another statement.
There are three kinds of loops:
- while loops test whether a condition is true before executing the controlled statement.
- do-while loops test whether a condition is true after executing the controlled statement.
- for loops are used to execute the controlled statement a given numbers of times.
3) A switch statement decides which of several statements to
execute.
Let's discuss some of the problems😇
1) find errors in the below code and after execution gets output bye...
#include <stdio.h>
int main(void) {
int i;
5 = i;
if(i = 0)
{
printf("hello");
}
else
{
printf("bye");
}
return 0;
}
correct code:
#include <stdio.h>
int main(void) {
int i;
i = 5;
if(i = 0)
{
printf("hello");
}
else
{
printf("bye");
}
return 0;
}
Ans. "5 = i " is the syntax error correct assignment is i = 5;
and there is no problem in control statement 'i' value will get overwritten
with '0' which is considered as false so the output will be 'bye'.
2) Replace '?' in the below code so that it prints the factorial of
15.
#include<stdio.h>
long int fact(int n);
int main() {
int n = 15;
printf("Factorial of %ld", fact(n));
return 0;
}
long int fact(int n) {
if (?)
return n*fact(n-1);
else
return 1;
}
correct code:
#include<stdio.h>
long int fact(int n);
int main() {
int n = 15;
printf("Factorial of %ld", fact(n));
return 0;
}
long int fact(int n) {
if (n>=1)
return n*fact(n-1);
else
return 1;
}
Ans: recursion will happen till n >=1
3) Correct the bellow code so that output will be
'true'
#include<stdio.h>
int main() {
int i = 3;
int j = 9;
while(i*i = j)
{
printf("true");
i++;
}
}
correct code is
#include<stdio.h>
int main() {
int i = 3;
int j = 9;
while(i*i == j)
{
printf("true");
i++;
}
}
Ans: ' = 'is assignment operator and ' == 'is used to check
equality.
4) Correct the below code to get output
"hi".
#include<stdio.h>
int main() {
int i = 1;
int ch = i;
switch(ch)
{
case 1:
printf("hi");
case 2:
printf("hello");
default:
printf("bye");
}
}
correct code:
#include<stdio.h>
int main() {
int i = 1;
int ch = i;
switch(ch)
{
case 1:
printf("hi");
break;
case 2:
printf("hello");
default:
printf("bye");
}
}
Ans: To exit out from the switch we use the break condition.
5) Change the below code to get output 'jai' without changing variable initialization
#include<stdio.h>
int main() {
int i = 1; //don't change it
int j = 2; //don't change it
int k = 3; //don't change it
while(i != j)
{
if(i++ == j)
printf("jai");
else if(i == --k)
printf("ho");
else
printf("xxxx");
}
}
correct code:
#include<stdio.h>
int main() {
int i = 1; //don't change it
int j = 2; //don't change it
int k = 3; //don't change it
while(i != j)
{
if(++i == j)
printf("jai");
else if(i == --k)
printf("ho");
else
printf("xxxx");
}
}
No comments:
For Query and doubts!