Professional Documents
Culture Documents
Lecture 6 - Loop
Lecture 6 - Loop
https://sites.google.com/a/quest.edu.pk/dr-irfana-memon/lecture-slides
C# Programming
language constructs,
Creating Methods,
while (test-expression)
{
// body of while
8
C# while loop construct
This loop is similar to the for loop, except that condition is
specified first and is used to repeat a statement as long as a
particular condition is true.
while (test-expression)
using System;
{
class Whileexample
// body of while
{
}
do {
// body of do while loop
} while (test-expression);
i++;
15
} while(i<=122);
Console.ReadLine();
}}}
C# foreach loop
In c#, Foreach loop is used to loop through an each item in
array or collection object to execute the block of statements
repeatedly.
16
C# foreach loop (Example)
using System;
namespace Tutlane
{
class Program
{
static void Main(string[] args)
Syntax: break;
19
Exercise
1. Write C# program to print multiplication table of a given number
2. Write C# program to find sum of even numbers between 1 to n
3. Write C# program to find sum of odd numbers between 1 to n
4. Write C# program to find first and last digit of any number
5. Write C# program to find the sum of first and last digit of any number
6. Write C# program to swap first and last digit of a number
7. Write C# program to calculate power using while & for loop
20
C# Encapsulation
• Encapsulation is defined 'as the process of enclosing one or
more items within a physical or logical package'.
• Encapsulation, in object oriented programming methodology,
prevents access to implementation details.
• Abstraction and encapsulation are related features in object
oriented programming.
21
C# Encapsulation
• An access specifier defines the scope and visibility of a class
member.
• C# supports the following access specifiers:
Public
Private
Protected
22
Public Access specifier
• Public access specifier allows a
class to expose its member
variables and member functions to
other functions and objects.
• Any public member can be
accessed from outside the class.
• The following example illustrates
this −
26
Private Access specifier
• Private access specifier allows a using System;
namespace RectangleApplication
class to hide its member variables {
and member functions from other class Rectangle
{ //member variables
functions and objects. private double length;
private double width;
• Only functions of the same class public void Acceptdetails()
{
can access its private members. Console.WriteLine("Enter Length: ");
length = Convert.ToDouble(Console.ReadLine());
• Even an instance of a class cannot Console.WriteLine("Enter Width: ");
access its private members. width = Convert.ToDouble(Console.ReadLine());
using System;
namespace RectangleApplication
{
class Rectangle
{ //member variables
private double length;
private double width;
public void Acceptdetails()
{
30
Internal Access specifier
using System;
• Internal access specifier allows namespace RectangleApplication
{
a class to expose its member class Rectangle
variables and member {
//member variables
functions to other functions and internal double length;
objects in the current assembly. Internal double width;
double GetArea()
• In other words, any member {
with internal access specifier return length * width;
}
can be accessed from any class