Chapter 3: Understanding C# Language Fundamentals

Slides:



Advertisements
Similar presentations
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Advertisements

CMT Programming Software Applications
Basic Elements of C++ Chapter 2.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
C-Language Keywords(C99)
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Chapter 2: Using Data.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
CPS120: Introduction to Computer Science
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
C++ Programming: Basic Elements of C++.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
C++ Lesson 1.
Chapter 1.2 Introduction to C++ Programming
Definition of the Programming Language CPRL
Chapter 2: Basic Elements of C++
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Data Types and Expressions
Computer Programming BCT 1113
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
Starting JavaProgramming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to C++ Programming
An overview of Java, Data types and variables
Chapter 7 Additional Control Structures
Module 2: Understanding C# Language Fundamentals
Expressions and Assignment
elementary programming
Chapter 2: Introduction to C++.
Data Types and Expressions
An Introduction to Programming with C++ Fifth Edition
Chap 2. Identifiers, Keywords, and Types
Module 2 Variables, Data Types and Arithmetic
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Review of Java Fundamentals
Presentation transcript:

Chapter 3: Understanding C# Language Fundamentals Microsoft® Chapter 3: Understanding C# Language Fundamentals Visual C# 2008

Overview Understanding the Fundamentals of a C# Program Using C# Predefined Types Writing Expressions Creating Conditional Statements Creating Iteration Statements

Lesson: Understanding the Fundamentals of a C# Program What Is the Structure of a C# Program? How to Format Code in C#

What Is the Structure of a C# Program? Program execution begins at Main( ) The using keyword refers to resources in the .NET Framework class library Statements are commands that perform actions A program is made up of many separate statements Statements are separated by a semicolon Braces are used to group statements using System; class HelloWorld { static void Main() { Console.WriteLine ("Hello, World"); }

How to Format Code in C# Use indentation to indicate enclosing statements C# is case sensitive White space is ignored Indicate single line comments by using // Indicate multiple-line comments by using /* and */ /* this multiple-line comments */ using System; class HelloWorld { static void Main() { Console.WriteLine ("Hello, World"); //writes Hello, World }

Lesson: Using C# Predefined Types What Are Predefined Types? How to Declare and Initialize Variables How to Declare and Initialize Strings How to Create and Use Constants How to Create and Use Enumeration Types How to Convert Between Types

What Are Predefined Types? Predefined types are those that are supplied by the C# language and the .NET Framework. Types are used to declare variables Variables store different kinds of data Let the data that you are representing determine your choice of variable Predefined types are those provided by C# and the .NET Framework You can also define your own Variables must be declared before you can use them

Predefined Type Predefined type Definition # Bytes byte Integer between 0 and 255 1 short Integer between -32768 and 32767 2 int Integer between -2147483648 and 2147483647 4 long Integer between -9223372036854775808 and 9223372036854775807 8 bool Boolean value: true or false float Single-precision floating point value (non-whole number) double Double-precision floating point value decimal Precise decimal value to 28 significant digits 12 object Base type of all other types N/A char Single Unicode character between 0 and 65535 string An unlimited sequence of Unicode characters

How to Declare and Initialize Variables Declaring Assign a type Assign a name End with a semicolon 1 2 3 int numberOfVisitors; string bear; Initializing Use assignment operator (=) Assign a value End with a semicolon 1 2 3 string bear = "Grizzly"; Assigning literal values Add a type suffix 1 decimal deposit = 100M;

Assigning literal values decimal bankBalance = 3433.20; // ERROR! decimal bankBalance = 3433.20M; //OK literal suffixes Category Suffix Description Integer U L UL Unsigned Long Unsigned long Real number F D M Float Double Decimal

Escape characters

How to Declare and Initialize Strings Example string Declaring literal strings Using escape characters Using verbatim strings Understanding Unicode string s = "Hello World"; // Hello World string s = "\"Hello\""; // "Hello" string s = "Hello\nWorld"; // a new line is added string s = @"Hello\n"; // Hello\n The character “A” is represented by “U+0041”

How to Create and Use Constants A constant is a variable whose value remains constant. Constants are useful in situations where the value that you are using has meaning and is a fixed number, such as pi, the radius of the earth, or a tax rate. Declared using the const keyword and a type You must assign a value at the time of declaration Benefits Constants make your code more readable, maintainable, and robust. For example, if you assign a value of 6378 to a constant named earthRadius, when you use this value in calculations it is immediately apparent what value you are referring to, and it is not possible for someone to assign a different value to earthRadius. Syntax You declare a constant by using the const keyword and a type. You must assign a value to your constants at the time that you declare them. const int earthRadius = 6378; // km const long meanDistanceToSun = 149600000; // km const double meanOrbitalVelocity = 29.79D; // km/sec

How to Create and Use Enumeration Types You create an enumeration type by using the enum keyword, assigning a name, and then listing the values that your enumeration can take. Benefits Defining Enumeration Types enum Planet { Mercury, Venus, Earth, Mars } Using Enumeration Types Planet aPlanet = Planet.Mars; Displaying the Variables Console.WriteLine("{0}", aPlanet); //Displays Mars

How to Convert Between Types Implicit Performed by the compiler on operations that are guaranteed not to truncate information int x = 123456; // int is a 4-byte integer long y = x; // implicit conversion to a long Explicit Where you explicitly ask the compiler to perform a conversion that otherwise could lose information int x = 65537; short z = (short) x; // explicit conversion to a short, z == 1

How to Convert Between Types

Practice: Using C# Types Hands-on Practice In this practice, you will declare and initialize variables and then examine them using the debugging tool

Lesson: Writing Expressions What Are Expressions and Operators? How to Determine Operator Precedence

What Are Expressions and Operators? Operators Are Symbols Used in Expressions Common Operators Example Increment / decrement ++ -- Arithmetic * / % + - Relational < > <= >= Equality == != Conditional && || ?: Assignment = *= /= %= += -= <<= >>= &= ^= |=

How to Determine Operator Precedence Expressions are evaluated according to operator precedence 10 + 20 / 5 result is 14 Parentheses can be used to control the order of evaluation (10 + 20) / 5 result is 6 10 + (20 / 5) result is 14 Operator precedence is also determined by associativity Binary operators are left-associative Assignment and conditional operators are right-associative

Practice: Using Operators Hands-on Practice In this practice, you will predict the values​calculated in expressions

Lesson: Creating Conditional Statements How and When to Use the if Statement How and When to Use the switch Statement

How and When to Use the if Statement if else if if ( sales > 10000 ) { bonus += .05 * sales; } if ( sales > 10000 ) { bonus += .05 * sales; } else if ( sales > 5000 ) { bonus = .01 * sales; else { bonus = 0; if ( priorBonus == 0 ) { //ScheduleMeeting; if else if ( sales > 10000 ) { bonus += .05 * sales; } else { bonus = 0;

How and When to Use the switch Statement (Cont.) Example int moons; switch (aPlanet){ case Planet.Mercury: moons = 0; break; case Planet.Venus: case Planet.Earth: moons = 1; Default: Console.WriteLine(“Not match!”); break; } Default case: The default label catches any values that are not matched by the case labels.

Practice: Using Conditional Statements Hands-on Practice In this practice, you will create conditional Statements if…else

Lesson: Creating Iteration Statements How to Use a for Loop How to Use a while Loop How to Use a do Loop

How to Use a for Loop Use when you know how many times you want to repeat the execution of the code for (initializer; condition; iterator) { statements; } Example for (int i = 0; i < 10; i++) { Console.WriteLine("i = {0}",i); } for ( int j = 100; j > 0; j -= 10 ) { Console.WriteLine("j = {0}", j);

How to Use a while Loop A Boolean test runs at the start of the loop and if it tests as False, the loop is never executed The loop executes until the condition becomes false while (true-condition) { statement-block } Example while ( readingFile == true ) { GetNextLine(); } continue, break keyword

How to Use a do Loop/ do-while Loop Executes the code in the loop and then performs a Boolean test. If the expression tests as True then the loop repeats until the expression tests as False. do { statements } while (boolean-expression); Note: The semicolon after the statement is required. Example do { statements; } while (condition); is equivalent to statements; while (condition) { statements; } which (as long as the continue statement is not used) while (true) { statements; if (!condition) break; } or to LOOPSTART: statements; if (condition) goto LOOPSTART; int i = 1; do { Console.WriteLine ("{0}", i++); } while (i <= 10);

Practice: Using Iteration Statements Hands-on Practice In this practice, you will use a for loop to calculate the sum of the integers from 1 to 1000

Review Understanding the Fundamentals of a C# Program Using C# Predefined Types Writing Expressions Creating Conditional Statements Creating Iteration Statements

Lab 2.1: Writing a Savings Account Calculator Hands-on Practice Exercise #1: Writing a Savings Calculator Exercise #2: Extending the Savings Calculator