1 / 25

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

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. Labs+Assignments+Project …………20%

jania
Télécharger la présentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Course Assessment • Labs+Assignments+Project …………20% • Quizzes…………………………...…10% • Midterm…………………………..…20% • Final Examination......................………. 50%

  3. 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 (khawaja.mohiuddin@bimcs.edu.pk) • Send email only to Bahria email address

  4. Basic C# Programming Constructs

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

  6. 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); } }

  7. 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 consoledo { // 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

  8. 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.

  9. Data Types in C# (contd.)

  10. Operators • 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#.

  11. Operators (contd.)

  12. Operators (contd.)

  13. Operators (contd.)

  14. Operators (contd.)

  15. 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: • ((x < 10) && ( x > 5)) || ((x > 20) && (x < 25)) • System.Convert.ToInt32("35")

  16. 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.

  17. 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 (?:) 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");

  18. Control Structures (contd.) • 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; • }

  19. Control Structures (contd.) • 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++; • }

  20. Control Structures (contd.) • 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 (inti in fibarray) • { • System.Console.WriteLine(i); • }

  21. 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#.

  22. 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 }; } }

  23. Arrays (contd.) class ArrayClass2D { static void Print2DArray(int[,] arr) { // Display the array elements. for (inti = 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(); } } /* 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 */

  24. 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.)

  25. 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#

More Related