Procedural Programming in C# Chapters 2 - 5. 2 Objectives You will be able to: Describe the most important data types available in C#. Read numeric values.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Basic Java Constructs and Data Types – Nuts and Bolts
Copyright © 2003 Pearson Education, Inc. Slide 1.
2. C# Language Fundamentals
1 CSE 331 Enumerated types ( enum ) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
1 / / / /. 2 (Object) (Object) –, 10 (Class) (Class) –, –, – (Variables) [ Data member Field Attribute](, ) – (Function) [ Member function Method Operation.
Module 4: Statements and Exceptions. Overview Introduction to Statements Using Selection Statements Using Iteration Statements Using Jump Statements Handling.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Execute Blocks of Code Multiple Times Svetlin Nakov Telerik Corporation
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
COMP 14 Introduction to Programming
Introduction to Programming G51PRG University of Nottingham Revision 1
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Air Force Institute of Technology Electrical and Computer Engineering
Execute Blocks of Code Multiple Times Telerik Software Academy C# Fundamentals – Part 1.
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Introduction to Computers and Programming Lecture 7:
Basic Elements of C++ Chapter 2.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
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.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Chapter 2: Java Fundamentals
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Chapter One Lesson Three DATA TYPES ©
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Computer Programs CS 1400 Dennis A. Fairclough Version 1.1 CS 1400 Dennis A. Fairclough Version 1.1.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Object Oriented Programming Lecture 2: BallWorld.
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.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
C++ Lesson 1.
Chapter Topics The Basics of a C++ Program Data Types
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 4 Assignment Statement
Elementary Programming
Chapter 4 – Fundamental Data Types
Lecture 2 Data Types Richard Gesick.
2.5 Another Java Application: Adding Integers
Basic Elements of C++.
Multiple variables can be created in one declaration
Chapter 3 Assignment Statement
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Chapter 2.
Chapter 2: Basic Elements of Java
Chapter 2: Java Fundamentals
Recap Week 2 and 3.
Primitive Types and Expressions
Chapter 2 Primitive Data Types and Operations
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Agenda Types and identifiers Practice Assignment Keywords in Java
Presentation transcript:

Procedural Programming in C# Chapters 2 - 5

2 Objectives You will be able to: Describe the most important data types available in C#. Read numeric values from the keyboard. Describe the major differences between C# and C for procedural programming.

3 Changes to Standard C Familiar Data Types int 32 bit integer Like int in C Adequate for most purposes long 64 bit integer Unlike long in C float 32 bit floating point Like float in C Dont use unless space is critical. double 64 bit floating point Like double in C Right for most floating point needs

4 New and Changed Data Types char uses Unicode representation. 16 bits Normally transparent, except for size of variables. char and byte are not equivalent. string is a type Can be assigned, compared. decimal 28 significant digits Integer with decimal scaling factor Works as expected for dollars and cents bool is a type values are true and false See page 33 for examples.

5 How to Input Numeric Values Every numeric type has a Parse method. Converts text string values to that type Replaces sscanf() in C Example: String Input_Line; double length;... Console.Write ("Enter length: "); Input_Line = Console.ReadLine (); length = double.Parse(Input_Line);

6 Changes to Standard C The Definite Assignment Rule (page 34) You must assign a value to a variable before using it. It is impossible to use an uninitialized variable. Enforced by the compiler. It must be able to determine that the variable is initilized on every possible exectution path to a statement that uses the variable.

7 Changes to Standard C/C++ Functions defined as part of a class are called methods. In C# this is the only way functions can be defined. So method and function are essentially synonymous. A method can return a value, or can be declared as void just like functions in C. If a method is not declared as void, it must include a return statement on every execution path.

8 if statements require a boolean expression or a variable of type bool. class Program { static void Main(string[] args) { bool allOK; allOK = true; if (allOK) { System.Console.WriteLine("All OK!"); } } } Changes to Standard C

9 This works as expected: class Program { static void Main(string[] args) { int a = 1; int b = 2; if (a == b) { System.Console.WriteLine("Something is wrong!"); } else { System.Console.WriteLine("All OK!"); }

10 Changes to Standard C This gets a compile error: class Program { static void Main(string[] args) { int a = 1; int b = 2; if (a = b) { System.Console.WriteLine("Something is wrong!"); } else { System.Console.WriteLine("All OK!"); }

11 Changes to Standard C This gets a compile error: class Program { static void Main(string[] args) { int a = 0; if (a) { System.Console.WriteLine("Something is wrong!"); } else { System.Console.WriteLine("All OK!"); } You cant use an integer in an if statement in C#

12 Changes to Standard C In switch statements, fallthrough (after a statement) is prohibited. Every case must have a break. (Or return or throw) This works as expected: static void Main(string[ ] args) { string color = ""; suit trumps = suit.clubs; switch (trumps) { case suit.hearts: case suit.diamonds: color = "Red"; break; case suit.clubs: case suit.spades: color = "Black"; break; } System.Console.WriteLine(color); } This is OK. (Not fallthrough)

13 Changes to Standard C This gets a compile error: static void Main(string[ ] args) { string color = ""; suit trumps = suit.clubs; switch (trumps) { case suit.hearts: case suit.diamonds: color = "Red"; break; case suit.clubs: case suit.spades: color = "Black";// No break! } System.Console.WriteLine(color); } This is OK. (Not fallthrough)

14 Changes to Standard C This also gets a compile error: static void Main(string[ ] args) { string color = ""; suit trumps = suit.clubs; switch (trumps) { case suit.hearts: color = "Red"; // No break! case suit.diamonds: color = "Red"; break; case suit.clubs: case suit.spades: color = "Black"; break; } System.Console.WriteLine(color); }

15 Assignment Read Chapters 1 – 5. Compile and run several examples. Especially any examples in the book that do not look familiar to you. End of Presentation