Visual Programming Fundamentals Chapter 3 PDF
Document Details
![ChampionRoentgenium9927](https://quizgecko.com/images/avatars/avatar-18.webp)
Uploaded by ChampionRoentgenium9927
Tags
Summary
This document provides an introduction to Visual Programming Fundamentals, focusing on Chapter 3, which details C# syntax, semantics, statements, variables, data types, and conversions. The document assumes a prior understanding of fundamental programming concepts, making it a useful supplementary resource.
Full Transcript
CHAPTER THREE V I S UA L P R O G R A M M I N G F U N D A M E N TA L S SYNTAX AND SEMANTICS Syntax of programming languages tells us how a program looks like Semantics tells us what the program means. 1. Statements C# programs contain a basic building block named a...
CHAPTER THREE V I S UA L P R O G R A M M I N G F U N D A M E N TA L S SYNTAX AND SEMANTICS Syntax of programming languages tells us how a program looks like Semantics tells us what the program means. 1. Statements C# programs contain a basic building block named a statement. A statement could be an instruction or combination of instructions. As a rule, each C# statements are terminated by a semi colon (;) SYNTAX AND SEMANTICS… Naming Convention Identifiers are the names you use to identify the elements in your programs, such as namespaces, classes, methods, and variables. The names of the methods, variables, and constants that you declare in your code must follow these guidelines: They must begin with a letter or an underscore. You can use only letters (uppercase and lowercase), digits, and underscore characters. They can't be the same as restricted keywords (If, loop, case, …). 1. COMMENTS Comments are used for explaining code. Compiler ignores the comment entries. The multiline comments in C# programs start with as shown below: Single-line comments are indicated by the '//' symbol 2. VARIABLES In C# a variable is a location in the computer's memory that is identified by a unique name and is used to store a value. memory is allocated to a variable at the time of its creation. When you are referring to a variable, you are actually referring to the value stored in that variable. VARIABLES… Declaring Variables Note: C# does not allow implicit variable declarations. You must explicitly declare all variables before you use them. When you declare a variable, you perform the following steps: - specify its type (such as int); - specify its name (identifier, such as age); - optionally specify initial value (such as 25) but this is not obligatory. The syntax for declaring variables in C# is as follows: [= ]; Here is an example of declaring variables: string name; VARIABLES… Assigning a Value Assigning a value to a variable is the act of providing a value that must be stored in the variable. This operation is performed by the assignment operator "=". On the left side of the operator we put the variable name and on the right side – its new value. Here is an example of assigning values to variables: name = "John Smith"; age = 25; CONT.. C# does not allow you to use an unassigned variable. You must assign a value to a variable before you can use it; otherwise, your program might not compile. This requirement is called the Definite Assignment Rule. For example, the following statements generate a compile- time error because age is unassigned: int age; MessageBox.Show (age); // compile-time error 3. DATA TYPES DATA TYPE CONVERSION Type conversion is converting one type of data to another type. Implicit type conversion-These conversions are performed by C# in a type-safe manner. For example int myInt = 9; double myDouble = myInt; // Automatic casting: int to double Console.WriteLine(myInt); // Output 9 Console.WriteLine(myDouble); // Outputs 9 CONT.. Explicit type conversion-These conversions are done explicitly by users using the predefined functions. Explicit conversions require a cast operator. Example double myDouble = 9.78; int myInt = (int) myDouble; // Manual casting: double to int Console.WriteLine(myDouble); // Outputs 9.78 Console.WriteLine(myInt); // Outputs 9 CONT.. Type Conversion Methods It is also possible to convert data types explicitly by using built- in methods, such as Convert.ToBoolean, Convert.ToDouble, Convert.ToString, Convert.ToInt32 (int) and Convert.ToInt64 (long): Example int myInt = 10; double myDouble = 5.25; bool myBool = true; Console.WriteLine(Convert.ToString(myInt)); // convert int to string Console.WriteLine(Convert.ToDouble(myInt)); // convert int to double Console.WriteLine(Convert.ToInt(myDouble)); // convert double to int Console.WriteLine(Convert.ToString(myBool)); // convert bool to string CONT.. 2.3 SCOPE AND LIFE TIME OF VARIABLES The scope of a variable defines which parts of your code are aware of its existence. C# scope rules of variables can be divided into three categories as follows: Class Level Scope: Declaring the variables in a class but outside any method can be directly accessed anywhere in the class. Method Level Scope: Variables that are declared inside a method have method level scope. These are not accessible outside the method. These variables are termed as the local variables. Block Level Scope: These variables are generally declared inside the for, if, while statement etc. OPERATORS Operators are symbols (characters or keywords) that specify operations to be performed on one or two operands. Operators that take one operand are called unary operators. Operators that take two operands are called binary operators. In C#, there are a variety of operators. These are: logical operators, arithmetic operators, relational operators, ARITHMETIC OPERATORS The arithmetic operators perform the standard arithmetic operations on numeric values Operat Description or + Used to add two numbers - Used to find the difference between two numbers or to indicate the negative value of a numeric expression. * Used to multiply two numbers / Used to divide two numbers and return a floating- point result % Used to divide two numbers and return only the remainder ARITHMETIC OPERATORS Example: A program that Example: a program that checks a accepts and adds two number is a multiple of 10 or not. numbers together and private void button1_Click(object display the result sender, EventArgs e) { private void int x=30; button1_Click(object sender, int val; EventArgs e) val = x % 10; { if (val == 0) int x; { textBox1.Text = "it is a int y; multiple of 10"; int z; } x= else int.Parse(textBox1.Text); { textBox1.Text = "it is not a y= multiple of 10"; int.Parse(textBox2.Text); } RELATIONAL OPERATORS Relational operators are used to compare two values. They are called relational operators because they indicate the relation between two values i.e. which one is larger and which one is smaller or are they equal. The result of comparison is either true or false Symb Meaning Description ol > Greater than Checks if one number is greater than the other < Less than Checks if one number is less than the other == Equal to Checks if one number is equal to the other != Not equal to Checks if one number is not equal to the other >= Greater than Checks if one number is greater than or equal or equal to to the other = d ‘compares two variables called c and d num > 3+4 ‘compares variable num with result of expression a == b ‘compares two variables called a and b Example: a program that accepts a number and checks if the number is greater than 10 private void button1_Click(object sender, EventArgs e) { int a; a = int.parse(text1.text) If (a > 10 ) { messagebox.show(”greater than 10”); } Else { messagebox.show(”Less than or equal to 10”); } } LOGICAL OPERATORS Logical operators are performed on one or more expressions. They are used to combine two or more conditions together except NOT which takes only one expression/condition. They are used with IF..THEN statements most of the time. Operato Description r Used to perform a logical conjunction on two && expressions. Used to perform a logical disjunction on two || expressions Used to perform logical negation on an ! expression. CONT.. Logical Operators – Example The following example illustrates the usage of the logical operators and their actions: bool a = true; bool b = false; Console.WriteLine(a && b); // False Console.WriteLine(a || b); // True Console.WriteLine(!b); // True Console.WriteLine(b || true); // True CONT.. Syntax: IF condition1 && condition2 THEN IF condition1 || condition2 THEN IF ! condition1 THEN Example: a program that checks a number is between 5 and 10 int st; st = 20; If (st > 5 && st, >=,