Tutorial 2
[Link] whether the following are true or false.
a) A switch statement can always be replaced by a series of if else
statement:True
b)The break statement is an optional in the default case when it is the last
one:True
c)A loop construct may be implemented using if and go to statement:True
d)It is legal to have negative increment in for statement:true
e)The body of a do-while statement is always executed atleast once:true
[Link] the errors
a) if (x+y=z && y>0)
Ans: if (x+y==z && y>0)
b) if (p<0) || (q<0)
Ans:if((p<0) || (q<0))
3a)
if ( code==1)
[Link](“pass”);
else
[Link](“Fail”);
Ans:No error
b)
if(code>1)
a=b+c;
else
a=0;
Ans:No error
4 Find errors if any each of the following looping statements. Assume that all variables
have been declared and assigned values.
a)
count=1;
sum=sum+x;
count=count+1;
Ans:No error
b)
name=0;
do
{
name=name+1;
[Link](“India \n”);
}while (name=1);
Ans:while(name==1);
c)
for(x=1;x>10;x=x+1)
{
……….
……….
}
Ans:No error
d)
m=1;
n=0;
for( ,m+n<19;++n)
[Link](“Hello\n”);
m=m+10;
Ans:for( ;m+n<19;++n)
[Link] is the output of the following code?
int m=100;
while(true)
{
if(m<10);
break;
m=m-10;
}
[Link](“m is”+m);
Ans:120
[Link] is the output for the following.
int m=100;
int n=300;
while(++m<-n)
[Link](m);
Ans:Control will not enter to loop since (101<-300) is not true.
[Link] a program to compute the sum of digits of a given integer number?
Using system;
Class sum
{
public staic void Main()
{
string n=[Link]();
int m,sum=0;
while(n!=0)
{
n=n/10;
m=n%10;
sum=sum+m;
}
[Link](“Sum of digits”+sum);
}
}
[Link] a program to print full output using for loop
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Ans:
using system;
class format
{
public static void Main()
{
int i,j;
for(i=1;i<=5;i++)
{
[Link](“\t”);
for(j=1;j<=i;j++)
{
[Link](“\t”+i);
}
}
}
9What is wrong with the fullcode?
jnt j=0;
While(j<10)
{
j++;
if(j==5)
continue loop;
[Link](“j is “+J);
}
Ans:Continue statement should not have lable.
[Link] the syntax for ternary operator.
Ans:(condition)?statement1:statement2;