Module 2: Understanding C# Language Fundamentals

Slides:



Advertisements
Similar presentations
Neal Stublen C# Data Types Built-in Types  Integer Types byte, sbyte, short, ushort, int, uint, long, ulong  Floating Point Types.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
CMT Programming Software Applications
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2 - Java Programming Fundamentals1 Chapter 2 Java Programming Fundamentals.
Basic Elements of C++ Chapter 2.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
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.
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.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
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.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average 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.
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.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
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.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Definition of the Programming Language CPRL
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Data Types and Expressions
Data Types and Expressions
Computer Programming BCT 1113
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
Multiple variables can be created in one declaration
Computing with C# and the .NET Framework
User input We’ve seen how to use the standard output buffer
Expressions and Control Flow in JavaScript
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 3: Understanding C# Language Fundamentals
Basic Elements of C++ Chapter 2.
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
Starting JavaProgramming
Introduction to C++ Programming
An Introduction to Java – Part I, language basics
An overview of Java, Data types and variables
Expressions and Assignment
Data Types and Expressions
Engineering Problem Solving with C++ An Object Based Approach
Data Types and Expressions
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Visual Programming COMP-315
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
An Introduction to Programming with C++ Fifth Edition
In this class, we will cover:
Chap 2. Identifiers, Keywords, and Types
Module 2 Variables, Data Types and Arithmetic
Review of Java Fundamentals
Data Types and Expressions
Lecture 9: JavaScript Syntax
Data Types and Expressions
Presentation transcript:

Module 2: Understanding C# Language Fundamentals

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 */ using System; class HelloWorld { static void Main() { Console.WriteLine ("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? 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

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

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 Declared using the const keyword and a type You must assign a value at the time of declaration const int earthRadius = 6378;//km const long meanDistanceToSun = 149600000;//km const double meanOrbitalVelocity = 29.79D;//km sec

How to Create and Use Enumeration Types Defining Enumeration Types Using Enumeration Types Displaying the Variables enum Planet { Mercury, Venus, Earth, Mars } Planet aPlanet = Planet.Mars; 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 Explicit Where you explicitly ask the compiler to perform a conversion that otherwise could lose information int x = 123456; // int is a 4-byte integer long y = x; // implicit conversion to a long int x = 65537; short z = (short) x; // explicit conversion to a short, z == 1

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

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 Increment / decrement Arithmetic Relational Equality Conditional Assignment Example ++ -- * / % + - < > <= >= == != && || ?: = *= /= %= += -= <<= >>= &= ^= |=

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

Practice: Using Operators Paper-based Practice In this practice, you will predict the values calculated in expressions 10 min

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 ( sales > 10000 ) { bonus += .05 * sales; } if else if ( sales > 10000 ) { bonus += .05 * sales; } else { bonus = 0; if else if if ( sales > 10000 ) { bonus += .05 * sales; } else if ( sales > 5000 ) { bonus = .01 * sales; else { bonus = 0; if ( priorBonus == 0 ) { //ScheduleMeeting;

How and When to Use the switch Statement int moons; switch (aPlanet){ case Planet.Mercury: moons = 0; break; case Planet.Venus: case Planet.Earth: moons = 1; } Default case

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

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 continue, break bool readingFile; // . . . while ( readingFile == true ) { GetNextLine(); }

How to Use a do 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 { // something that is always going to happen //at least once } while (test is true); Example 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 10 min

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 Lab Exercise #1: Writing a Savings Calculator Exercise #2: Extending the Savings Calculator 1 hour