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.

Slides:



Advertisements
Similar presentations
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Advertisements

Introduction to C Programming
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
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.
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.
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,
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.
 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
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.
 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.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a 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.
Basic Introduction to C#
Chapter Topics The Basics of a C++ Program Data Types
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
REPETITION CONTROL STRUCTURE
Elementary Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
Revision Lecture
Variables and Arithmetic Operators in JavaScript
Computing with C# and the .NET Framework
Chapter 4 – Control Structures Part 1
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
The University of Texas Rio Grande Valley
Starting JavaProgramming
The University of Texas – Pan American
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
The University of Texas – Pan American
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
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
The University of Texas – Pan American
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
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.
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects – Exercises UTPA – Fall 2012 This set of slides is revised.
CSCI 3327 Visual Basic Review: Exam I
Expressions and Assignment
elementary programming
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Chapter 4 - Control Structures: Part 1
Module 2 Variables, Data Types and Arithmetic
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises UTPA – Fall 2012 This set of slides is revised from lecture slides of Prof.
CSCI 3328 Object Oriented Programming in C# Review: Exam I
Presentation transcript:

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 of Prof. John Abraham. -- Xiang Lian

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#

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

Introduction (cont'd) Function Syntax Programming language 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#

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)

About Some Data Types in C# Value Types Size (in bits) Range sbyte 8 128 to 127 byte 8 0 to 255 short 16 -32768 to 32767 ushort 16 0 to 65535 int 32 147483648 to 2147483647 uint 32 0 to 4294967295 long 64 -9223372036854775808 to 9223372036854775807 ulong 64 0 to 18446744073709551615 char 16 0 to 65535 bool 8 true, false enum types and struct types Reference types include class types, interface types, delegate types, and array types Pointer types

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;

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

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

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 av; av = sum / num; Console.WriteLine(av); // Output: 66

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

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

Last Chapter: Arithmetic Operators Unary: +, - Multiplicative: *, /, % Additive: +, - Relational operators > < >= <= Equality operators ==, != Precedence of operators http://msdn.microsoft.com/en-us/library/6a71f45d.aspx 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 * 5 + 7

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

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

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

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

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

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): "); }

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

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

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++;

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