0% found this document useful (0 votes)
114 views

C # Tutorial2

This document contains examples of switch statements, if/else statements, for loops, while loops, do/while loops, and ternary operators. It tests the reader's ability to identify errors in loop and conditional logic, write programs to calculate sums and print patterns, and describe the syntax of programming constructs like the ternary operator. Key concepts covered include break statements, continue statements, loop initialization and conditions, and basic programming exercises.

Uploaded by

Vijaiyesh Babu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views

C # Tutorial2

This document contains examples of switch statements, if/else statements, for loops, while loops, do/while loops, and ternary operators. It tests the reader's ability to identify errors in loop and conditional logic, write programs to calculate sums and print patterns, and describe the syntax of programming constructs like the ternary operator. Key concepts covered include break statements, continue statements, loop initialization and conditions, and basic programming exercises.

Uploaded by

Vijaiyesh Babu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Tutorial 2

1.State 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
2.Find 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)
Console.WriteLine(“pass”);
else
Console.WriteLine(“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;
Console.WriteLine(“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)
Console.WriteLine(“Hello\n”);
m=m+10;
Ans:for( ;m+n<19;++n)

5.What is the output of the following code?


int m=100;
while(true)
{
if(m<10);
break;
m=m-10;
}
Console.Writeline(“m is”+m);
Ans:120
6.What is the output for the following.
int m=100;
int n=300;
while(++m<-n)
Console.WriteLine(m);

Ans:Control will not enter to loop since (101<-300) is not true.

7.Write a program to compute the sum of digits of a given integer number?


Using system;
Class sum
{
public staic void Main()
{
string n=Console.ReadLine();
int m,sum=0;
while(n!=0)
{
n=n/10;
m=n%10;
sum=sum+m;
}
Console.WriteLine(“Sum of digits”+sum);
}
}
8.Write 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++)
{
Console.WriteLine(“\t”);
for(j=1;j<=i;j++)
{
Console.Write(“\t”+i);
}
}
}

9What is wrong with the fullcode?


jnt j=0;
While(j<10)
{
j++;
if(j==5)
continue loop;
Console.WriteLine(“j is “+J);
}
Ans:Continue statement should not have lable.

10.Give the syntax for ternary operator.


Ans:(condition)?statement1:statement2;

You might also like