E N D
Unity University Department of Computer Science Windows Programming ( C# )
CHAPTER 3 Part One Object-Oriented Fundamentals
Variable is a location in the memory that has name and value associated with it. • C# is type – safe language. • The compiler checks the value stored in the variable is of the appropriate type. • syntax to declare and initialize variables <data_type> <variable_name> = <value>; Variables and Data Types
example of variable declaration int age = 12; • Static variable: A field with the static modifier is called as static variable. The initial value of the static variable is the default value of the variable type. public static intempno; Variables
2. Instance variable: exists when a new instance of the class is created by the user. The initial value of the instance variable is the default value of the variable type. class message {int msg1;int msg2; } Variables
3. Local variable: is declared by the local variable declaration which can be a block, control statements, lopping statements. • The life of the local variable is the portion of the program where the execution is specified. • A local variable is not initialized automatically and does not have any default value. Variables
A variable must begin with a letter or an underscore ( ‘_’ ) followed by the sequence of letters, digits, or underscores. • The first character in the variable name cannot be a digit. 3) It should not contain any spaces, symbols or special character in the declaration 4) It must be unique for every variable 5) The keywords cannot be used as variable names Rules for naming variables in C#
namespace Variable { class Program { static void Main(string[] args) { string message = "Hello World!"; Console.WriteLine(message); } } } Examples of variables in C#
Invalid Variable Assignment string message = "Hello World!!"; inti = message; inti; intj = i; Console.WriteLine(j); • You must assign a value to a variable before using it otherwise the compiler will give an error. Examples of variables in C#
A variable must be declared with the data type because C# is a strongly-typed language. • The data type tells a C# compiler what kind of value a variable can hold. • C# includes many in-built data types for different kinds of data, e.g., String, number, float, decimal, etc. C# Data types
class Program { static void Main(string[] args) { string stringVar = "Hello World!!"; intintVar = 100; float floatVar = 10.2f; char charVar = 'A'; boolboolVar = true; } } Example: Data types
In C# data types are categorized based on how they store their value in the memory. • C# includes following categories of data types: 1. Value type 2. Reference type Value Type and Reference Type
A data type is a value type if it holds a data value within its own memory space. It means variables of these data types directly contain their values. Value Type
Areference type doesn't store its value directly. Instead, it stores the address where the value is being stored. In other words, a reference type contains a pointer to another memory location that holds the data. Reference Type
It checks for the Boolean condition. A true statement is one that evaluates to a non - zero number. A false statement evaluates to zero. The C# if statement tests the condition. It is executed if condition is true. Syntax: if(Condition is TRUE)//EXECUTE this line of code If Statements
using System; publicclassIfExample { publicstaticvoid Main(string[] args) { intnum = 10; if (num % 2 == 0) { Console.WriteLine("It is even number"); } Console.Read(); } } If Example
In an If else statement if condition evaluates to true, then the statement is executed. If the condition is false, else statement is executed. Syntax: if(expression) { statements; } else { statements; } If…else
publicclassIfExample{ publicstaticvoid Main(string[] args) { Console.WriteLine("Enter a number:"); intnum = Convert.ToInt32(Console.ReadLine()); if (num % 2 == 0) { Console.WriteLine("It is even number"); } else { Console.WriteLine("It is odd number"); } Console.Read(); } } If else Example
int time = 20; if (time < 18) { Console.WriteLine("Good day."); } else { Console.WriteLine("Good evening."); } If else Example2
Short Hand of If...Else • variable = (condition) ? expressionTrue : expressionFalse; • Example int time = 20; string result = (time < 18) ? "Good day." : "Good evening."; Console.WriteLine(result); Ternary operator
An if...else statement can exist within another if...else statement. Such statements are called nested if...else statement. Nested if statements are generally used when we have to test one condition followed by another. In a nested if statement, if the outer if statement returns true, it enters the body to check the inner if statement. Nested If else
if (boolean-expression) { if (nested-expression-1) { // code to be executed } else { // code to be executed } } else { if (nested-expression-2) { // code to be executed } else { // code to be executed } } Syntax of Nested If else
classNested { publicstaticvoid Main(string[] args) { inta = 7, b = -8, c = 15; if(a > b) { if(a > c) { Console.WriteLine("{0} is the largest", a); } else { Console.WriteLine("{0} is the largest", b); } } Nested If else Example
else { if(b > c) { Console.WriteLine("{0} is the largest", b); } else { Console.WriteLine("{0} is the largest", c); } } Console.ReadKey(); } } Output: 15 is the largest Nested If else
The C# if-else-if ladder statement executes one condition from multiple statements. • Syntax: if(condition1){ //code to be executed if condition1 is true }elseif(condition2){ //code to be executed if condition2 is true } elseif(condition3){ //code to be executed if condition3 is true } else{ //code to be executed if all the conditions are false } If else If ladder Statement
publicclassGrade { publicstaticvoid Main(string[] args) { Console.WriteLine("Enter your mark out of 100 to check grade:"); Double mark = Convert.ToDouble(Console.ReadLine()); if (mark < 0 || mark > 100) { Console.WriteLine("No such Mark"); } elseif (mark >= 0 && mark < 50) { Console.WriteLine("F"); } If else If Example
elseif (mark >= 50 && mark < 60) { Console.WriteLine("Your Grade is D"); } elseif (mark >= 60 && mark < 70) { Console.WriteLine("Your Grade is C"); } elseif (mark >= 70 && mark < 80) { Console.WriteLine("Your Grade is B"); } If else If Example
elseif (mark >= 80 && mark < 90) { Console.WriteLine("Your Grade is A"); } elseif (mark >= 90 && mark <= 100) { Console.WriteLine("Your Grade is A+"); } Console.ReadKey(); } } } Output: Enter your mark out of 100 to check grade: 78.5 Your Grade is B If else If Example
The switch statement is used when you have to evaluate a variable for multiple values. Syntax: switch ( variablename) { case constantExpression_1: statements; break; case constantExpression_2: statements; break; default: statements; break; } Switch statement
classProgram { staticvoid Main(string[] args) { string month, season; Console.WriteLine("Please enter month to know its season"); month = Console.ReadLine(); switch (month) { case"December": case"January": case"February": season = "Winter"; break; Switch Example
case"March": case"April": case"May": season = "Spring"; break; case"June": case"July": case"August": season = "Summer"; break; case"September": case"October": case"November": season = "Autumn"; break; Switch Example
default: season = "Unknown"; break; } Console.WriteLine(month+" is in " + season +" season"); Console.ReadKey(); } } Output: Please enter month to know its season April April is in Spring season Switch Example
The break statement terminates the case in the switch statement where it is placed. • Control is passes to the statement followed by the terminated statement if present in the loop. Code snippet class Program { static void Main ( string [ ] args) { for ( int i=0; i<=10; i++ ) { if ( i==4) { break; } Console.WriteLine(i); } Console.Read(); } } Ex: Write a program w/c displays numbers from 20 to 40, except 25 & 35 Break statements
The continue statement is used to pass the control to the next iteration for the statement in which it appears. Code snippet class Program { static void Main ( string [ ] args) { for ( int i=0; i<=10; i++) { if(i<6) { continue; } Console.WriteLine(i); } Console.Read(); } } continue statements
The while loop statement is used to execute a block depending on the condition specified. • The condition is checked first and the statements within the loop are executed if the condition returns true. • After the last statement in the while loop is executed, the control is sent back to the start of the loop. while loop
When the condition is true, the statements present in the loop are executed. • The execution of the loop stops when the condition returns a false value. • Syntax while ( expression ) { statements; } while loop
Code Snippet • Write C# program which displays number divisible by 5 b/n 10 & 30 and their remainder is 0. class Program { static void Main ( string[ ] args ) { inti = 1; while ( i<10 ) { Console.WriteLine(“Value of the variable is :{0}”, i ); i= i+2; } Console.Read(); } } while loop
The do while loop and while loop are similar. • But the statements in the do while loop are executed at least once, as they are executed before the condition is checked. • The d/c b/n while & do while loop Do while loop
Syntax: do { statements; } while ( expression ); Do while loop
Code snippet: class Program { static void Main ( string[ ] args ) { inti=10; do { Console.WriteLine(“value of i is”+i); i = i+10; } while ( i<100 ); Console.Read(); } } Do while loop
A for loop structure is used to execute a set of statements for a specified number of times. • Syntax: for ( initialization; termination; increment/decrement) { Statements } for loop
sample code: static void Main ( string [] args ) { int i; for ( i=50; i<=60; i++ ) { Console.WriteLine( “Value of variable i is “+i); } Console.Read(); } for loop