CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Neal Stublen C# Data Types Built-in Types  Integer Types byte, sbyte, short, ushort, int, uint, long, ulong  Floating Point Types.
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
Introduction to Computers and Programming Lecture 5 New York University.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 3: Control Structures (Part 1) – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
1. 2 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Decisions { class.
The University of Texas – Pan American
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 1A) UTPA – Fall 2011.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
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.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Chapter 2: Using Data.
3 C# Control Statements Dr. John P. Abraham Professor UTPA.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
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 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
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.
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++.
Computing with C# and the .NET Framework
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
The University of Texas Rio Grande Valley
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I UTPA – Fall 2012 This set of slides is revised from lecture slides.
The University of Texas – Pan American
The University of Texas – Pan American
Chapter 2: Basic Elements of Java
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
The University of Texas – Pan American
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall 2012 This set of slides is revised from lecture.
Expressions and Assignment
elementary programming
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Module 2 Variables, Data Types and Arithmetic
Presentation transcript:

CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX

Objectives In this chapter, you will: – Learn the primitive data types in Visual C# – Become familiar with arithmetic operators – Explore how to design algorithms to solve problems – Learn the components of basic control structures – Study the syntax of basic sequence, selection, and repetition structures in Visual C# 2

Introduction Computer program – Sequence of statements whose objective is to accomplish a task Programming – Process of planning and creating a program 3

Introduction (cont'd) Function – Collection of statements; when executed, accomplishes something Syntax – Rules that specify which statements (instructions) are legal Programming language – A set of rules, symbols, and special words – Visual C# 4

Introduction (cont'd) Reserved words, keywords, or word symbols – Words that are reserved by Visual C# – Usually in blue color in the IDE (Visual Studio) 5

About Some Data Types in C# Value Types Size (in bits) Range sbyte to 127 byte 8 0 to 255 short to ushort 16 0 to int to uint 32 0 to long to ulong 64 0 to char 16 0 to bool 8 true, false enum types and struct types Reference types include class types, interface types, delegate types, and array types Pointer types 6

Declaration of Variables All variables must be declared before they are used in a program Declaring a variable – int product = 3; Declaring multiple variables of the same type – int number1, number2; 7

Naming Convention Consist of letters, digits, and the underscore character (_) Must begin with a letter or underscore C# is case sensitive – NUMBER is not the same as number 8

Arithmetic Operators in Visual C# Addition: + Subtraction: - Multiplication: * Division: / Modulus: % Increment: ++ Decrement: -- 9

Explicitly and Implicitly Converting Between Simple Types Integer and integer division yields integer result Suppose average is a floating point number: – Average = total/num; Average will only get an integer if total and num are integers. int sum = 200, num = 3; float avg; avg = sum / num; Console.WriteLine(avg); // Output: 66 10

Unary Cast Operator int sum = 200, num = 3; float av; av = (float) sum / num; Console.WriteLine(av); // Output: float/float or float/int or int/float will yield a float. C# implicitly promotes the one int to float 11

Division and Modulus x / y and x%y int x=7, y = 2; Console.WriteLine(x / y); Console.WriteLine(x % y); – E.g., 7.0 / 2 evaluates to

Last Chapter: Arithmetic Operators Unary: +, - Multiplicative: *, /, % Additive: +, - Relational operators – > = <= Equality operators – ==, != Precedence of operators 13 high low

Exercises What are the values of the following expressions? – 10/3 – 5.2/2.0 – 9 % 3 What is the order of the following expression? – X = 2 * 5 / 3+ 3 *

Control Statements Linear (sequential) program execution Selection structure repetition structure Structured programming – Controlled entry and exit out of a module – Avoid goto statements 15

Selection Structures in C# if – single selection statement if … else – double selection statement switch – multiple selection statement 16 display "passed" [grade>=60] [grade<60]

Examples if (grade >= 60) Console.WriteLine("Passed!"); if (grade >= 60) Console.WriteLine("Passed!"); else Console.WriteLine("Failed!"); Conditional Operator Console.WriteLine(grade >= 60 ? "Passed!": “Failed!”); 17

Nested If Statement if (grade >=90) Console.WriteLine(“A”); else if (grade >=80) Console.WriteLine(“B!”); … else Console.WriteLine(“F!”); 18

Repetition Structure - while Read LCV (initialize) while (condition) { Block Read LCV again (change value) } 19

Example length = Convert.ToInt16(Console.ReadLine()); while (length > 0) { Console.Write("Enter Height of the Wall: "); height = Convert.ToInt16(Console.ReadLine()); PaintAWall thisWall = new PaintAWall(length, height, pricePerGal); thisWall.CalculateCost(ref paintCost, ref laborCost, ref galPaint, ref sqFt); Console.Write("Enter Length and Height for Wall #: " + Convert.ToString(numWalls+1)); Console.Write("\nEnter Length of the Wall (0 to quit): "); length = Convert.ToInt16(Console.ReadLine()); } 20

Counter Controlled vs Sentinel Controlled Counter controlled while loop (use LCV as a counter) – int Counter =1; – while (Counter <=10) – { … Counter ++; – }//does it 10 times 21 increment operator

Counter Controlled vs Sentinel Controlled (cont'd) Sentinel controlled while loop – string str; – while (str != "0") – { … str = Console.ReadLine(); – }//does it until str is "0" Sentinel controlled is good when one does not know exact number of times to execute a loop 22

Example of while Loop int product = 3; while (product <= 100) product = 3*product; 23

Nested Control Statements class MultiplicationTable { static void Main(string[] args) { int i=2, j; while (i <= 12) { j = 1; while (j <= 10) { Console.WriteLine(i + " x " + j + " = " + i * j ); j++; } Console.WriteLine("\n"); i++; } 24

Exercises Write a console program using nested while loop to output the following pattern to screen: * ** *** **** ***** 25