1 / 44

CH 3 - Modified

lecture notes

Jafar16
Télécharger la présentation

CH 3 - Modified

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. Unity University Department of Computer Science Windows Programming ( C# )

  2. CHAPTER 3 Part One Object-Oriented Fundamentals

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

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

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

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

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

  8. namespace Variable { class Program { static void Main(string[] args) { string message = "Hello World!"; Console.WriteLine(message); } } } Examples of variables in C#

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

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

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

  12. Data type Ranges

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

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

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

  16. C# Control Statements

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

  18. using System;       publicclassIfExample     {   publicstaticvoid Main(string[] args)           {   intnum = 10;   if (num % 2 == 0)               {   Console.WriteLine("It is even number");               }   Console.Read();          }  } If Example

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

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

  21. int time = 20; if (time < 18) { Console.WriteLine("Good day."); } else { Console.WriteLine("Good evening."); } If else Example2

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  41. Syntax: do { statements; } while ( expression ); Do while loop

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

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

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

More Related