SEN-442 VISUAL PROGRAMMING LECTURER: Khawaja Mohiuddin Computer Sciences Department Bahria University (Karachi Campus) Khawaja.mohiuddin@bimcs.edu.pk https://sites.google.com/site/khawajamcs Course Assessment 2 Labs+Assignments+Project …………20% Quizzes…………………………...…10% Midterm…………………………..…20% Final Examination......................………. 50% Important Announcements 3 Attendance 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) Mandatory for student to have at least 75% attendance Otherwise, they will not be allowed to sit in the final examination Visit website regularly for lectures, announcements and other material Email Address (khawaja.mohiuddin@bimcs.edu.pk) Send email only to Bahria email address 4 Basic C# Programming Constructs Objectives 5 Simple I/O Programs Data Types in C# Operators and Expressions Control Structures Arrays Simple I/O Programs 6 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); } } Simple I/O Programs (contd.) 7 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 Data Types in C# 8 C# defines an intrinsic set of fundamental data types C# data type keywords are shorthand notations for full-blown types in the System namespace. Data Types in C# (contd.) 9 Operators 10 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#. Operators (contd.) 11 Operators (contd.) 12 Operators (contd.) 13 Operators (contd.) 14 Expressions 15 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") Statements 16 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. Control Structures 17 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 (?:) 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"); 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; } Control Structures (contd.) 19 Repetition Control Structure specifies the repetition of an action while some condition remains true. Examples: do, for, foreach, in, while do { Console.WriteLine(x); x++; } while (x < 5); while (n < 6) { Console.WriteLine("Current value of n is {0}", n); n++; } Control Structures (contd.) 20 for (int i = 1; i <= 5; i++) { 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. int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 }; foreach (int i in fibarray) { System.Console.WriteLine(i); } Arrays 21 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#. Arrays (contd.) 22 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 }; } } Arrays (contd.) 23 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]); } } } /* Output: static void Main() { Element(0,0)=1 // Pass the array as an argument. Element(0,1)=2 Print2DArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }); Element(1,0)=3 Element(1,1)=4 // Keep the console window open in debug mode. Element(2,0)=5 System.Console.WriteLine("Press any key to exit."); Element(2,1)=6 System.Console.ReadKey(); Element(3,0)=7 } } Element(3,1)=8 */ Arrays (contd.) 24 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.) Conclusion 25 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#