You are on page 1of 39

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
Course Content
Review of C# Syntax: Overview of Writing Applications using C#, Data types,
Operators, and Expressions
C# Programming Language Constructs, Creating Methods
Invoking Methods, Handling Exceptions, Creating overloaded Methods

Developing the Code for a Graphical Application: Implementing Structs and Enums
Implementing Type-safe Collections: Creating Classes, Organizing Data into Collections,

Department of CSE, QUEST


Dr. Irfana Memon
Handling Events, Defining and Implementing Interfaces
Creating a Class Hierarchy by Using Inheritance, Extending .NET Framework Classes,
Creating Generic Types
Accessing a Database: Creating and Using Entity Data Models, Querying and Updating Data
by Using LINQ
Designing the User Interface for a Graphical Application: Using XAML, Binding Controls to
Data, Styling a User Interface
Improving Application Performance and Responsiveness: Implementing Multitasking by
using Tasks and Lambda Expressions
2
Performing Operations Asynchronously, Synchronizing Concurrent Access to Data
Review of C# Syntax:
Overview of Writing
Applications using C#,

Department of CSE, QUEST


Dr. Irfana Memon
Data types, Operators,
and Expressions
3
C# Variables
• A variable is nothing but a name given to a storage area that
our programs can manipulate.
• Each variable in C# has a specific type, which determines the
size and layout of the variable's memory the range of values
that can be stored within that memory and the set of
operations that can be applied to the variable.

Department of CSE, QUEST


Dr. Irfana Memon
4
C# Variables
• The basic value types provided in C# can be categorized as:
Type Example
Integral sbyte, byte, short, ushort, int, uint, long, ulong, and char
types
Floating float and double
point

Department of CSE, QUEST


Dr. Irfana Memon
types
Decimal decimal
types
Boolean true or false values, as assigned
types
Nullable Nullable data types
types
5
C# also allows defining other value types of variable such as enum and reference
types of variables such as class.
Defining Variables
• Syntax for variable definition in C# is:
<data_type> <variable_list>;
• Here, data_type must be a valid C# data type including char, int,
float, double, or any user-defined data type, and variable_list may
consist of one or more identifier names separated by commas.
• Some valid variable definitions are shown here:

Department of CSE, QUEST


Dr. Irfana Memon
int i, j, k;
char c, ch;
float f, salary;
double d;
You can initialize a variable at the time of definition as:
int i = 100;

6
C# Keywords
• Keywords are predefined sets of reserved words that have
special meaning in a program.
• The meaning of keywords can not be changed, neither can
they be directly used as identifiers in a program.
• Although keywords are reserved words, they can be used as
identifiers if @ is added as prefix.

Department of CSE, QUEST


Dr. Irfana Memon
• For example, int @void;

7
List of C# Keywords
• C# has a total of 79 keywords. All these keywords are in
lowercase. Here is a complete list of all C# keywords.
abstract as base bool
break byte case catch
char checked class const
continue decimal default delegate
do double else enum

Department of CSE, QUEST


Dr. Irfana Memon
event explicit extern false
finally fixed float for
foreach goto if implicit

in in (generic modifier) int interface

internal is lock long


namespace new null object

operator out out (generic modifier) override

params private protected public


readonly ref return sbyte
sealed short sizeof stackalloc
static string struct switch 8
this throw true try
typeof uint ulong unchecked
unsafe ushort using using static
void volatile while
types
C# Exercise : Variables and data

Dr. Irfana Memon


9

Department of CSE, QUEST


Exercise
1. Write C# code to declare a variable to store the age of a
person. Then the output of the program is as shown
below:
You are 20 years old

Department of CSE, QUEST


Dr. Irfana Memon
10
Exercise
1. Write C# code to declare a variable to store the age of a
person. Then the output of the program is as shown
below: using System;
You are 20 years old namespace Csharp_exercises
{
class Program

Department of CSE, QUEST


Dr. Irfana Memon
{
static void Main(string[] args)
{
int age = 20;// declaring variable and
assign 20 to it.
Console.WriteLine("You are {0} years
old.",age);
Console.ReadLine();
11
}
}
}
Exercise
2. Write C# code to display the asterisk pattern as shown
below:
*****
*****
*****
*****

Department of CSE, QUEST


Dr. Irfana Memon
*****

12
Exercise
2. Write C# code to display the asterisk pattern as shown
below: using System;
***** namespace Csharp_exercises
***** {
***** class Program
***** {

Department of CSE, QUEST


Dr. Irfana Memon
***** static void Main(string[] args)
{
Console.WriteLine("*****");
Console.WriteLine("*****");
Console.WriteLine("*****");
Console.WriteLine("*****");
Console.WriteLine("*****");
Console.ReadLine(); 13
}
}
}
Exercise
3. Write C# code to declare one integer variables, one float
variable, and one string variable and assign 10, 12.5, and
"C# programming" to them respectively. Then display their
values on the screen.

Department of CSE, QUEST


Dr. Irfana Memon
14
Exercise
3. Write C# code to declare one integer variables, one float
variable, and one string variable and assign 10, 12.5, and
"C# programming" to them respectively. Then display their
values on the screen.
s = "C# programming";
using System;
Console.WriteLine(x);
namespace exercise

Department of CSE, QUEST


Dr. Irfana Memon
Console.WriteLine(y);
{
Console.WriteLine(s);
class Program
Console.ReadLine();
{
}
static void Main(string[] args)
}
{
}
int x;
float y;
string s; 15
x = 10;
y = 12.5f;
Exercise
4. Write C# code to prompt a user to input his/her name
and print his/her name.

Department of CSE, QUEST


Dr. Irfana Memon
16
Exercise
4. Write C# code to prompt a user to input his/her name
and print his/her name.
Console.Write("Please enter your
using System; name:");
namespace exercise name = Console.ReadLine();
{ Console.WriteLine("{0}", name);

Department of CSE, QUEST


Dr. Irfana Memon
class Program Console.ReadLine();
{ }
static void Main(string[] args) }
{ }

string name;

17
Operators
We will learn everything about different types of operators
in C# programming language and how to use them.

Table of Contents
Basic Assignment Operator
Arithmetic Operators

Department of CSE, QUEST


Dr. Irfana Memon
Relational Operators
Logical Operators
Unary Operators
Ternary Operator
Bitwise and Bit Shift Operators
Compound Assignment Operators
18
Assignment Operator

Basic assignment operator (=) is used to assign values to


variables.

Department of CSE, QUEST


Dr. Irfana Memon
For example,
double x;
x = 50.05;

Here, 50.05 is assigned to x.

19
Arithmetic Operators
• Arithmetic operators are used to perform arithmetic
operations such as addition, subtraction, multiplication,
division, etc.

Operator Operator Name Example

Department of CSE, QUEST


Dr. Irfana Memon
+ Addition Operator 6 + 3 evaluates to 9

- Subtraction Operator 10 - 6 evaluates to 4

* Multiplication Operator 4 * 2 evaluates to 8

/ Division Operator 10 / 5 evaluates to 2


20
Modulo Operator
% 16 % 3 evaluates to 1
(Remainder)
Relational Operators
• Relational operators are used to check the relationship
between two operands. If the relationship is true the result
will be true, otherwise it will result in false.
• Relational operators are used in decision making and loops.

Department of CSE, QUEST


Dr. Irfana Memon
Operator Operator Name Example

== Equal to 6 == 4 evaluates to false

> Greater than 3 > -1 evaluates to true

< Less than 5 < 3 evaluates to false

>= Greater than or equal to 4 >= 4 evaluates to true

<= Less than or equal to 5 <= 3 evaluates to false 21

!= Not equal to 10 != 2 evaluates to true


Logical Operators
• Logical operators are used to perform logical operation such as AND, OR.
• Logical operators operates on boolean expressions (true and false) and
returns boolean values. Logical operators are used in decision making and
loops.

Operand 1 Operand 2 OR (||) AND (&&)

Department of CSE, QUEST


Dr. Irfana Memon
true true true true
true false true false
false true true false
false false false false

• In simple words, the table can be summarized as:


 If one of the operand is true, the OR operator will evaluate it
to true. 22
 If one of the operand is false, the AND operator will evaluate it
to false.
Unary Operators
• Unlike other operators, the unary operators operates on a single
operand.

Operator Operator Name Description

Leaves the sign of


+ Unary Plus

Department of CSE, QUEST


Dr. Irfana Memon
operand as it is
Inverts the sign of
- Unary Minus
operand
++ Increment Increment value by 1
-- Decrement Decrement value by 1
Inverts the value of a
! Logical Negation (Not)
boolean
23
Ternary Operators
• The ternary operator ? : operates on three operands.
• It is a shorthand for if-then-else statement.
• Ternary operator can be used as follows:
 variable = Condition? Expression1 : Expression2;
• The ternary operator works as follows:

Department of CSE, QUEST


Dr. Irfana Memon
 If the expression stated by Condition is true, the
result of Expression1 is assigned to variable.
 If it is false, the result of Expression2 is assigned to
variable.

24
Bitwise and bit shift Operators
• Bitwise and bit shift operators are used to perform bit
manipulation operations.

Operator Operator Name

~ Bitwise Complement

Department of CSE, QUEST


Dr. Irfana Memon
& Bitwise AND
| Bitwise OR
^ Bitwise Exclusive OR
<< Bitwise Left Shift
>> Bitwise Right Shift

25
Compound Assignment Operators
Operator Operator Name Example Equivalent To

+= Addition Assignment x += 5 x=x+5

-= Subtraction Assignment x -= 5 x=x-5

*= Multiplication Assignment x *= 5 x=x*5

/= Division Assignment x /= 5 x=x/5

Department of CSE, QUEST


Dr. Irfana Memon
%= Modulo Assignment x %= 5 x=x%5

&= Bitwise AND Assignment x &= 5 x=x&5

|= Bitwise OR Assignment x |= 5 x=x|5

^= Bitwise XOR Assignment x ^= 5 x=x^5

<<= Left Shift Assignment x <<= 5 x = x << 5

26
>>= Right Shift Assignment x >>= 5 x = x >> 5

=> Lambda Operator x => x*x Returns x*x


Expressions
• An expression in C# is a combination of operands (variables,
literals, method calls) and operators that can be evaluated to
a single value.
• To be precise, an expression must have at least one operand
but may not have any operator.

Department of CSE, QUEST


Dr. Irfana Memon
27
Exercise
1. Write a C# program to print the sum of two numbers

Department of CSE, QUEST


Dr. Irfana Memon
28
Exercise
1. Write a C# program to print the sum of two numbers

using System;
namespace Operator
{
class ArithmeticOperator

Department of CSE, QUEST


Dr. Irfana Memon
{
public static void Main(string[] args)
{
double firstNumber = 14.40, secondNumber = 4.60,
result;
Console.WriteLine("{0} + {1} = {2}", firstNumber,
secondNumber, result); 29
}}}
Exercise
2. Write a C# program to swap two numbers

Department of CSE, QUEST


Dr. Irfana Memon
30
Exercise
2. Write a C# program to swap two numbers
NOTE: Console.ReadLine() returns string type and
to convert it as integer we need to use
Convert .ToOnt32

Department of CSE, QUEST


Dr. Irfana Memon
31
Exercise
3. Write a C# program to multiply two double numbers.

Department of CSE, QUEST


Dr. Irfana Memon
32
Exercise
3. Write a C# program to multiply two double numbers.

Department of CSE, QUEST


Dr. Irfana Memon
33
Exercise
4. Write a program to perform all airthmetic operators.

Department of CSE, QUEST


Dr. Irfana Memon
34
Exercise
5. Write a program to calculate (a+b)2.

Department of CSE, QUEST


Dr. Irfana Memon
35
Exercise
5. Write a program to calculate (a+b)2.

Department of CSE, QUEST


Dr. Irfana Memon
36
Exercise
6. Write a program to calculate area of triangle. Take
required input from user and then calculate the area of
triangle.

Department of CSE, QUEST


Dr. Irfana Memon
37
Exercise
7. Write C# code to produce the output shown below:
x value y value expression result
10 5 x=y+3 x=8
10 5 x=y-2 x=3
10 5 x=y*5 x=25

Department of CSE, QUEST


Dr. Irfana Memon
10 5 x=x/y x=2
10 5 x=x%y x=0

38
Wish You Good Luck

Dr. Irfana Memon


39

Department of CSE, QUEST

You might also like