Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Visual Programming

Department of CSE, QUEST


Dr. Irfana Memon
Dr. Irfana Memon
Department of CSE, QUEST

https://sites.google.com/a/quest.edu.pk/dr-irfana-memon/lecture-slides
C# Programming
language constructs,
Creating Methods,

Department of CSE, QUEST


Dr. Irfana Memon
Invoking Methods,
Overloaded Methods,
and Handling
Exceptions 2
C# for loop construct
• This loop is used to iterate a value fixed number of times.
for (initialization; condition; iterator)
{
// body of for loop
}
using System;

Department of CSE, QUEST


Dr. Irfana Memon
class Forexample
{
public static void Main()
{
for(int I = 1; I<=10;I++)
{
Console.WriteLine(I);
3
}
}
}
Exercise : for loop
1. Write C# code to produce the output shown below:
*******
******
*****
****
***
**
*

Department of CSE, QUEST


Dr. Irfana Memon
4
Exercise : for loop
1. Write C# code to produce the output shown below:
*******
****** using System;
***** namespace Csharp_exercises
**** {
*** class Program
** {
*

Department of CSE, QUEST


Dr. Irfana Memon
static void Main(string[] args)
{
int i,j;
for(i=0;i<=6;i++){
for(j=1;j<=7-i;j++)
Console.Write("*");
Console.Write("\n");
}
Console.ReadLine();
} 5
}
}
Exercise : for loop
Exercise 2: Write C# code to print the following pattern:
1******
12*****
123****
1234***
12345**
123456*
1234567

Department of CSE, QUEST


Dr. Irfana Memon
6
Exercise : for loop
Exercise 2: Write C# code to print the following pattern:
1****** using System;
12***** namespace Csharp_exercises
123**** {
1234*** class Program
12345** {
123456* static void Main(string[] args)
1234567 {

Department of CSE, QUEST


Dr. Irfana Memon
int i,j,k;

for (i = 1; i <= 7; i++)


{
for (j = 1; j <= i; ++j)
Console.Write(j);

for (k = 7 - i; k >= 1; k--)


Console.Write("*");
Console.Write("\n");
} 7
Console.ReadLine();
}
}}
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)
{
// body of while

Department of CSE, QUEST


Dr. Irfana Memon
}

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
{
}

Department of CSE, QUEST


Dr. Irfana Memon
public static void Main()
{
int I = 1;
while(I <=10)
{
console.WriteLine(I);
}
9
}
}
Exercise : while loop
Write C# program to print the table of characters that are equivalent to the
Ascii codes from 1 to 122.
The program will print the 10 characters per line.

Department of CSE, QUEST


Dr. Irfana Memon
10
Exercise : while loop
Write C# program to print the table of characters that are equivalent to the
Ascii codes from 1 to 122.
The program will print the 10 characters per line.
using System;
namespace Csharp_exercises
{
class Program
{

Department of CSE, QUEST


Dr. Irfana Memon
static void Main(string[] args)
{
int i =1;
while (i <=122)
{
Console.Write((char)i+"\t");
if (i % 10 == 0)
Console.Write("\n");
i++;
} 11
Console.ReadLine();
} }}
C# do while loop construct
This looping statement is also similar to that of the previous
one but the condition is specified last.

do {
// body of do while loop
} while (test-expression);

Department of CSE, QUEST


Dr. Irfana Memon
12
C# do while loop construct
This looping statement is also similar to that of the previous
one but the condition is specified last.
using System;
do { class Whileexample
// body of do while loop {
} while (test-expression); public static void Main()

Department of CSE, QUEST


Dr. Irfana Memon
{
do
{
int I = 1;
}
while(I<=10);
Console.WriteLine(I); 13
}
}
Exercise: do while loop
Write C# program to print the table of characters that are equivalent to the
Ascii codes from 1 to 122.
The program will print the 10 characters per line.

Department of CSE, QUEST


Dr. Irfana Memon
14
Exercise: do while loop
Write C# program to print the table of characters that are equivalent to the
Ascii codes from 1 to 122.
The program will print the 10 characters per line.
using System;
namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)

Department of CSE, QUEST


Dr. Irfana Memon
{
int i =1;
do
{
Console.Write((char)i+"\t");
if (i % 10 == 0)
Console.Write("\n");

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.

foreach (Type var_name in Collection_Object)


{

Department of CSE, QUEST


Dr. Irfana Memon
// Statements to Execute

16
C# foreach loop (Example)
using System;

namespace Tutlane
{
class Program
{
static void Main(string[] args)

Department of CSE, QUEST


Dr. Irfana Memon
{
string[] names = new string[3] { "Suresh Dasari", "Rohini Alavala", "Trishika D
asari" };
foreach (string name in names)
{
Console.WriteLine(name);
}
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
} 17
}
}
C# foreach loop (Exercise)
using System;
namespace Tutlane
{
class Program
{
static void Main(string[] args)
{

Department of CSE, QUEST


Dr. Irfana Memon
string[] names = new string[3]
{ "Suresh Dasari", "Rohini Alavala", "Trishika Dasari" };
foreach (string name in names)
{
Console.WriteLine(name);
}
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
} 18
}
C# break statement
In c#, Break statement is used to break or terminate the
execution of loops (for, while, do-while, etc.) or switch
statement and the control is passed immediately to the next
statements that follows a terminated loops or statements.

In c# nested loops also we can use break statement to stop or

Department of CSE, QUEST


Dr. Irfana Memon
terminate the execution of inner loops based on our
requirements.

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

Department of CSE, QUEST


Dr. Irfana Memon
8. Write C# program to find factorial of any number
9. Write C# program to print number in words
10. Write C# program to find HCF of two numbers
11. Write C# program to find LCM of two numbers

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.

Department of CSE, QUEST


Dr. Irfana Memon
• Abstraction allows making relevant information visible and
encapsulation enables a programmer to implement the
desired level of abstraction.
• Encapsulation is implemented by using access specifiers.

21
C# Encapsulation
• An access specifier defines the scope and visibility of a class
member.
• C# supports the following access specifiers:
 Public
 Private
 Protected

Department of CSE, QUEST


Dr. Irfana Memon
 Internal
 Protected internal

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 −

Department of CSE, QUEST


Dr. Irfana Memon
23
Public Access specifier
using System;
• Public access specifier allows a namespace RectangleApplication
{
class to expose its member class Rectangle
variables and member functions to {
//member variables
other functions and objects. public double length;
• Any public member can be public double width;
public double GetArea()
accessed from outside the class. {
• The following example illustrates return length * width;
}
this −

Department of CSE, QUEST


Dr. Irfana Memon
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}//end class Rectangle
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.length = 4.5;
r.width = 3.5;
r.Display(); 24
Console.ReadLine();
}
}
}
Public Access specifier
• Member variables length and width are using System;
declared public, so they can be accessed from namespace RectangleApplication
{
the function Main() using an instance of the class Rectangle
Rectangle class, named r. {
//member variables
• Member function Display() and GetArea() can public double length;
also access these variables directly without using public double width;
public double GetArea()
any instance of the class. {
• The member functions Display() is also return length * width;
}

Department of CSE, QUEST


Dr. Irfana Memon
declared public, so it can also be accessed public void Display()
from Main() using an instance of the Rectangle {
Console.WriteLine("Length: {0}", length);
class, named r. Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}//end class Rectangle
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.length = 4.5;
r.width = 3.5;
r.Display(); 25
Console.ReadLine();
}
}
}
Private Access specifier
• Private access specifier allows a
class to hide its member variables
and member functions from other
functions and objects.
• Only functions of the same class
can access its private members.
• Even an instance of a class cannot
access its private members.

Department of CSE, QUEST


Dr. Irfana Memon
• 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());

Department of CSE, QUEST


Dr. Irfana Memon
}
• The following example illustrates public double GetArea()
{
this − return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}//end class Rectangle
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.Acceptdetails(); 27
r.Display();
Console.ReadLine();
}
}
}
Private Access specifier
• In the preceding example, the member variables length and width are declared private, so they cannot
be accessed from the function Main().
• The member functions AcceptDetails() and Display() can access these variables.
• Since the member functions AcceptDetails() and Display() are declared public, they can be accessed
from Main() using an instance of the Rectangle class, named r.

using System;
namespace RectangleApplication
{
class Rectangle
{ //member variables
private double length;
private double width;
public void Acceptdetails()
{

Department of CSE, QUEST


Dr. Irfana Memon
Console.WriteLine("Enter Length: ");
length = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter Width: ");
width = Convert.ToDouble(Console.ReadLine());
}
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}//end class Rectangle
class ExecuteRectangle
{
static void Main(string[] args)
28
{
Rectangle r = new Rectangle();
r.Acceptdetails();
r.Display();
Console.ReadLine();
Protected Access specifier

• Protected access specifier allows a child class to access the


member variables and member functions of its base class.
• This way it helps in implementing inheritance.

Department of CSE, QUEST


Dr. Irfana Memon
29
Internal Access specifier
• Internal access specifier allows
a class to expose its member
variables and member
functions to other functions and
objects in the current assembly.
• In other words, any member
with internal access specifier
can be accessed from any class

Department of CSE, QUEST


Dr. Irfana Memon
or method defined within the
application in which the
member is defined.
• The following program
illustrates this −

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

Department of CSE, QUEST


Dr. Irfana Memon
public void Display()
or method defined within the {
Console.WriteLine("Length: {0}", length);
application in which the Console.WriteLine("Width: {0}", width);
member is defined. Console.WriteLine("Area: {0}", GetArea());
}
• The following program }//end class Rectangle
illustrates this − class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.length = 4.5;
r.width = 3.5;
r.Display(); 31
Console.ReadLine();
}
}
}
Wish You Good Luck

Dr. Irfana Memon


32

Department of CSE, QUEST

You might also like