Wednesday, March 4, 2009

C# Control Statements – Part 2

4. Control Statements – Part 2

Iterative statements are used to repeat a statement a number of times. E.g., if we want to print numbers from 1 to 10, or find all prime numbers between 1 to 100, we will use the technique of iteration.

C# has the following types of iteration statements:

Iterative statements
1. while for loop
2. do loop
3. for loop
4. foreach statement

while loop :
The format of the while loop is:

Initialization

while (condition)
{
body of the loop
}

1. The condition is checked first.
2. This loop is called an entry-controlled loop; the condition is checked first, and if it is true, control passes into the body of the loop and statements inside the loop are executed.
3. If the condition is initially false, the body of the loop is not executed at all.
4. After each iteration, control goes back to check if the condition is true.
5. There must be some statement in the body of the loop that makes the condition eventually false, otherwise the loop will continue forever.
6. When the condition becomes false, control passes to the statement after the while loop.

Example: The following program prints the value of n using the while loop

class WhileTest
{
static void Main()
{
int n = 1;
while (n < 6)
{
Console.WriteLine("Current value of n is {0}", n);
n++;
}
}
}
Program 1: To reverse a number entered by the user.

// Reverse a number entered by the user
using System;

namespace reverse
{
class reverse
{
static void Main()
{
int num, revnum = 0, d;

Console.WriteLine("Enter a number");
num = int.Parse(Console.ReadLine());

while (num > 0)
{
d = num % 10; // get the least significant digit
revnum = revnum * 10 + d; // reverse partially
num = num / 10; // drop the least significant digit
}

Console.WriteLine("Reversed number is {0}", revnum);
Console.ReadLine();
}
}
}


Program 2: Program to count the number of digits in an integer.

//To count the number of digits in an integer
using System;

namespace CountDigits
{
class count
{
static void Main()
{
int num, ctr = 0;

Console.WriteLine("Enter an integer");
num = int.Parse(Console.ReadLine());

while (num > 0)
{
num = num / 10;
ctr = ctr + 1;
}

Console.WriteLine("No. of digits is {0}", ctr);
Console.ReadLine();
} // end of Main
} // end of class
} // end of namespace
Program 3: Program to print the average of 10 numbers entered by the user.

//Average of 10 numbers entered by the user

using System;

namespace AverageMany
{
class average
{
static void Main()
{
int num, sum = 0, ctr = 0;
float avg;

while (ctr < 10)
{
Console.WriteLine("Pls enter a number");
num = int.Parse(Console.ReadLine());

sum = sum + num;

ctr++;
}

avg = (float)sum / ctr;

Console.WriteLine("Average is {0}", avg);
Console.ReadLine();
}
}
}


do - while loop :

The do-while loop is used when it is necessary to execute the body of the loop at least once. The general format of the do-while lop is :

initialization

do
{

body of the loop

} while (condition);

1. After the initialization, control passes to the body of the loop and the statements in the loop are executed.
2. The condition specified in the while part is then checked. If the condition is true, the body of the loop is executed once again.
3. If the condition becomes false, the body of the loop is not executed and control passes to the statement after the do-while loop..
4. There must be some statement in the body of the loop that makes the condition eventually false, otherwise the loop will continue forever.
5. Since the condition is checked at the bottom of the loop, the do-while loop is also called an exit-controlled loop.
6. The body of the loop is executed at least once.




IMPORTANT QUESTIONS
1. What is iteration? Which iteration statements are supported by C#?
2. Explain the while loop. Draw the relevant flowchart. Give suitable examples. Under what condition will the while loop become an infinite loop?
3. What precautions must be taken in using the while loop?
4. Explain the do-while loop. Draw the relevant flow chart. Under what condition will the while loop become an infinite loop?
5. Explain the for loop. Draw the relevant flowchart. Give an example to illustrate the use of this loop.
6. Compare the looping structures while and for.
7. Compare the looping structures do and for.



PROGRAMMING EXERCISES
1. Write a program to find the product of digits of an integer. E.g., if the integer is 2156, the output should give 2*1*5*6 i.e., 60.
2. Write a program to check whether a given digit d is present in an integer entered by the user.
3. Write a program to count the number of times a given digit d appears in an integer.
4. Write a program to count the number of odd and even digits in a number.
5. Write a program to print only the odd / even digits of an integer.
6. Write a program to print the following series: 2, 8, 18, 32, upto first 7 terms.
7. Write a program to compute the sum of squares of first 10 natural numbers.
8. Write a program to compute the sum of the first 10 odd / even natural numbers.
9. Write a program to print the average of n numbers entered by the user. User will also input the value of n.

No comments:

Post a Comment