LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria University (Karachi Campus)

25
LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria University (Karachi Campus) [email protected] https:// sites.google.com/site/khawajamcs SEN-442 VISUAL PROGRAMMING

description

SEN-442 VISUAL PROGRAMMING. LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria University (Karachi Campus) [email protected] https:// sites.google.com/site/khawajamcs. Course Assessment. Labs+Assignments+Project …………20% - PowerPoint PPT Presentation

Transcript of LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria University (Karachi Campus)

Page 1: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

LECTURER:Khawaja Mohiuddin

Computer Sciences DepartmentBahria University (Karachi Campus)

[email protected]://sites.google.com/site/khawajamcs

SEN-442 VISUAL PROGRAMMING

Page 2: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Course Assessment

Labs+Assignments+Project …………20% Quizzes…………………………...…10%

Midterm…………………………..…20%

Final Examination......................………. 50%

2

Page 3: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Important Announcements

Attendance Mandatory for student to have at least 75% attendance Otherwise, they will not be allowed to sit in the final

examination Plagiarism

Taking words, ideas, figures or materials from other sources and presenting them as one's own

Students found guilty of plagiarism and academic dishonesty will be subject to disciplinary action

Website (https://sites.google.com/site/khawajamcs) Visit website regularly for lectures, announcements and

other material Email Address

([email protected]) Send email only to Bahria email address

3

Page 4: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Basic C# Programming Constructs

4

Page 5: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Objectives

Simple I/O Programs Data Types in C# Operators and Expressions Control Structures Arrays

5

Page 6: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Simple I/O Programs

ReadLine() method reads the next line of characters from the standard input stream.

WriteLine() method writes the specified data, followed by the current line terminator, to the standard output stream.

using System; public class Example {

public static void Main() { string line; Console.WriteLine("Enter one or more lines of text (press CTRL+Z to exit):"); Console.WriteLine(); do {

Console.Write(" "); line = Console.ReadLine(); if (line != null) Console.WriteLine(" " + line);

} while (line != null); } }

6

Page 7: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Simple I/O Programs (contd.)

using System; // System namespace contains class Console public class Example { // class declaration public static void Main() { // main method declaration string line; // variable declaration of type string Console.WriteLine("Enter one or more lines of text (press CTRL+Z to exit):");

// prints the specified text with line terminator Console.WriteLine(); // prints empty line on the console

do { // start of do-while loop Console.Write(" "); // prints one blank character without line terminator line = Console.ReadLine(); // reads line of characters and stores in a variable if (line != null) // if empty line was not entered Console.WriteLine(" " + line); // prints blank character, line that was read and

line terminator} while (line != null); // ends do-while loop only when line in null

7

Page 8: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Data Types in C#

C# defines an intrinsic set of fundamental data types C# data type keywords are shorthand notations for full-blown types in the

System namespace.

8

Page 9: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Data Types in C# (contd.)9

Page 10: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Operators10

In C#, an operator is a term or a symbol that takes one or more expressions, or operands, as input and returns a value.

Operators that take one operand, such as the increment operator ( ++), are referred to as unary operators.

Operators that take two operands, such as arithmetic operators ( +, -, *, /) are referred to as binary operators.

One operator, the conditional operator ( ?:), takes three operands and is the sole tertiary operator in C#.

Page 11: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Operators (contd.)11

Page 12: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Operators (contd.)12

Page 13: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Operators (contd.)13

Page 14: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Operators (contd.)14

Page 15: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Expressions

Sequence of one or more operands and zero or more operators that can be evaluated to a single value, object, method, or namespace.

Can consist of a literal value, a method invocation, an operator and its operands, or a simple name. Simple names can be the name of a variable, type member, method parameter, namespace or type.

Can use operators that in turn use other expressions as parameters, or method calls whose parameters are in turn other method calls, so expressions can range from simple to very complex.

Examples of expressions:1. ((x < 10) && ( x > 5)) || ((x > 20) && (x < 25))2. System.Convert.ToInt32("35")

15

Page 16: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Statements

The actions that a program takes are expressed in statements. A statement can consist of a single line of code that ends in a semicolon,

or a series of single-line statements in a block. A statement block is enclosed in {} brackets and can contain nested blocks. Examples:

declaring variables, assigning values, calling methods, looping through collections, and branching to one or another block of code, depending on a given condition

The order in which statements are executed in a program is called the flow of control or flow of execution. The flow of control may vary every time that a program is run, depending on how the program reacts to input that it receives at run time.

16

Page 17: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Control Structures

Specifies what has to be done by the program, when and under which circumstances.

Selection Control Structure specifies alternate courses of program flow, creating a junction in your program.Examples: single-selection structure (if), the double-selection structure (if...else), the multiple-selection structure (switch), the inline conditional operator (?:)

17

if (x > 10) if (y > 20) Console.Write("Statement_1"); else Console.Write("Statement_2");

y > 20 ? Console.Write("Statement_1") : Console.Write("Statement_2");

Page 18: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Control Structures (contd.)18

int n = 2;switch(n) {

case 1: case 2: case 3:

Console.WriteLine("It's 1, 2, or 3.");break;

default:Console.WriteLine("Not sure what it is."); break;

}

Page 19: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Control Structures (contd.)

Repetition Control Structure specifies the repetition of an action while some condition remains true.Examples: do, for, foreach, in, while

19

do {

Console.WriteLine(x);x++;} while (x < 5);

while (n < 6) {

Console.WriteLine("Current value of n is {0}", n);n++;}

Page 20: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Control Structures (contd.)20

for (int i = 1; i <= 5; i++){

Console.WriteLine(i);}

int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 };foreach (int i in fibarray){

System.Console.WriteLine(i);}

Foreach-in statement repeats a group of embedded statements for each element in an array or an object collection

Can not be used to add or remove items from the source collection. If you need to add or remove items from the source collection, use a for loop.

Page 21: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Arrays

An array is a data structure that contains several variables of the same type

Can be Single-Dimensional, Multidimensional or Jagged. A jagged array is an array of arrays, and therefore its elements are

reference types and are initialized to null. The default value of numeric array elements are set to zero, and

reference elements are set to null. Arrays are zero indexed: an array with n elements is indexed from 0 to

n-1. Array elements can be of any type, including an Array type. Array types are reference types derived from the abstract base type

Array. foreach iteration can be used on all arrays in C#.

21

Page 22: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Arrays (contd.)

class TestArraysClass { static void Main() {

int[] array1 = new int[5]; // Declare a single-dimensional array int[] array2 = new int[] { 1, 3, 5, 7, 9 }; // Declare and set array element values int[] array3 = { 1, 2, 3, 4, 5, 6 }; // Alternative syntax int[,] multiDimensionalArray1 = new int[2, 3]; // Declare a two dimensional array

// Declare and set array element values int[,] multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 } };

// Declare a jagged array int[][] jaggedArray = new int[6][];

// Set the values of the first array in the jagged array structure jaggedArray[0] = new int[4] { 1, 2, 3, 4 }; }}

22

Page 23: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Arrays (contd.)

class ArrayClass2D { static void Print2DArray(int[,] arr) { // Display the array elements. for (int i = 0; i < arr.GetLength(0); i++) { for (int j = 0; j < arr.GetLength(1); j++) { System.Console.WriteLine("Element({0},{1})={2}", i, j, arr[i, j]); } } } static void Main() { // Pass the array as an argument. Print2DArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } });

// Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey();} }

23

/* Output: Element(0,0)=1 Element(0,1)=2 Element(1,0)=3 Element(1,1)=4 Element(2,0)=5 Element(2,1)=6 Element(3,0)=7 Element(3,1)=8 */

Page 24: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Arrays (contd.)

In C#, arrays are actually objects, you can use the properties, and other class members, that Array has.

Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements.

Like all ref parameters, a ref parameter of an array type must be definitely assigned by the caller. Therefore, there is no need to be definitely assigned by the callee.(The ref keyword causes arguments to be passed by reference. The effect is that any changes to the parameter in the method will be reflected in that variable when control passes back to the calling method.)

Like all out parameters, an out parameter of an array type must be assigned before it is used; that is, it must be assigned by the callee. (The out keyword causes arguments to be passed by reference. This is like the ref keyword, except that ref requires that the variable be initialized before it is passed.)

24

Page 25: LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria  University (Karachi Campus)

Conclusion

In this Lecture, we… Learned about basic I/O operations in C# Identified the data Types provided by C# Learned about the different types of operators

available in C# Looked at expressions and statements of C# Looked at the different types of Control Structures

of C# Learned how arrays are used in C#

25