1 Statements © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
Programming Languages and Paradigms The C Programming Language.
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
Section 2 - Selection and Repetition. Equality and Relational Operators.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
C++ for Engineers and Scientists Third Edition
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
UNIT II Decision Making And Branching Decision Making And Looping
L EC. 03: C ONTROL STATEMENTS Fall Java Programming.
COP INTERMEDIATE JAVA Review of COP2250 Concepts.
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
Computer Programming 1.  Editor Console Application Notepad Notepad++ Edit plus etc.  Compiler & Interpreter Microsoft.NET Framework  Microsoft visual.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
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.
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.
ILM Proprietary and Confidential -
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
Chapter 05 (Part III) Control Statements: Part II.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Introduction to Java Java Translation Program Structure
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
CS360 Windows Programming
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
CIS 3301 C# Lesson 3 Control Statements - Selection.
Text Files and String Processing
By Mr. Muhammad Pervez Akhtar
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Session 02 Module 3: Statements and Operators Module 4: Programming constructs Module 5: Arrays.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Object Oriented Programming Lecture 2: BallWorld.
Chapter 7 User-Defined Methods.
C# — Console Application
ECE Application Programming
Chapter 8: Control Structures
Chapter 5: Control Structures II
Java Programming: From Problem Analysis to Program Design, 4e
Starting JavaProgramming
Introduction to Java Programming
Chapter 2: Basic Elements of Java
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Introduction to C#.net PROF. S. LAKSHMANAN,
Programming Languages and Paradigms
C# Language & .NET Platform 10th Lecture
Corresponds with Chapter 5
Presentation transcript:

1 Statements © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License

2 Simple Statements Empty statement ;// ; is a terminator, not a separator Assigment x = 3 * y + 1; Method call string s = "a,b,c"; string[] parts = s.Split(',');// invocation of an object method (non-static) s = String.Join(" + ", parts);// invocation of a class method (static)

3 if Statement if ('0' <= ch && ch <= '9') val = ch - '0'; else if ('A' <= ch && ch <= 'Z') val = 10 + ch - 'A'; else { val = 0; Console.WriteLine("invalid character " + ch); }

4 switch Statement switch (country) { case "England": case "USA": language = "English"; break; case "Germany": case "Austria": case "Switzerland": language = "German"; break; case null: Console.WriteLine("no country specified"); break; default: Console.WriteLine("don't know the language of " + country); break; } Type of the switch expression integer type, char, enum or string (null ok as a case label). No fall-through (unlike in C or in Java)! Every statement sequence in a case must be terminated with break (or return, goto, throw). If no case label matches  default If no default specified  continuation after the switch statement

5 switch with Gotos E.g. for the implementation of automata a b c c int ch = Read(); int state = startTab[ch]; switch (state) { case 0:if (ch == 'a') { ch = Read(); goto case 1; } else if (ch == 'c') goto case 2; else goto default; case 1:if (ch == 'b') { ch = Read(); goto case 1; } else if (ch == 'c') goto case 2; else goto default; case 2:Write("input valid"); break; default:Write("illegal character " + ch); break; } int ch = Read(); s0:if (ch == 'a') { ch = Read(); goto s1; } else if (ch == 'c') goto s2; else goto err; s1:if (ch == 'b') { ch = Read(); goto s1; } else if (ch == 'c') goto s2; else goto err; s2:Write("input valid"); goto ok; err:Write("illegal character " + ch); ok:... Alternatively

6 Loops while while (i < n) { sum += i; ++; } do while do { sum += a[i]; i--; } while (i > 0); forshort form for for (int i = 0; i < n; i++)int i = 0; sum += i;while (i < n) { sum += i; i++; }

7 foreach Statement For iterating over collections, arrays and strings int[] a = {3, 17, 4, 8, 2, 29}; foreach (int x in a) sum += x; string s = "Hello"; foreach (char ch in s) Console.WriteLine(ch); Queue q = new Queue(); // elements are of type object q.Enqueue("John"); q.Enqueue("Alice");... foreach (string s in q) Console.WriteLine(s);

8 Jumps break; For exiting a loop or a switch statement. There is no break with a label like in Java (use goto instead). myLab:... goto myLab; Jumps to the label myLab. Restrictions: -no jumps into a block -no jumps out of a finally block of a try statement goto case 3: Can be used in a switch statement to jump to a case label. continue; Continues with the next loop iteration.

9 return Statement Returning from a void method void Foo (int x) { if (x == 0) return;... } Returning a value from a function method int Max (int a, int b) { if (a > b) return a; else return b; } class C { static int Main() {... return errorCode;// The Main method can be declared as a function; }// the returned error code can be checked with the // system variable errorlevel }

10 Output to the Console Examples Console.Write(intVal);// overloaded for all primitive types Console.WriteLine(intVal);// for objects ToString() is called automatically Placeholder syntax "{" n ["," width] [":" format [precision]] "}" n argument number (starting at 0) width field width (exceeded if too small) positive = right-aligned, negative = left-aligned format formatting code (e.g. d, f, e, x,...) precision number of fractional digits (sometimes number of digits) Example: {0,10:f2} Console.Write("Hello {0}", name);// placeholder Console.WriteLine("{0} = {1}", x, y);

11 Formatting Codes for Numbers d, Ddecimal format (integer number with leading zeroes)-xxxxx precision = number of digits f, Ffixed-point format-xxxxx.xx precision = number of fractional digits (default = 2) n, Nnumber format (with separator for thousands)-xx,xxx.xx precision = number of fractional digits (default = 2) e, Efloating-point format (case is significant)-x.xxxE+xxx precision = number of fractional digits c, Ccurrency format$xx,xxx.xx precision = number of fractional digits (default = 2) negative values are enclosed in brackets($xx,xxx.xx) x, Xhexadecimal format (case is significant)xxx precision = number of hex digits (maybe leading 0) g, Ggeneral (most compact format for the given value; default)

12 Examples int x = 17; Console.WriteLine("{0}", x); 17 Console.WriteLine("{0,5}", x); 17 Console.WriteLine("{0:d}", x); 17 Console.WriteLine("{0,5:d3}", x); 017 Console.WriteLine("{0:f}", x); Console.WriteLine("{0:f1}", x); 17.0 Console.WriteLine("{0:E}", x); E+001 Console.WriteLine("{0:e1}", x); 1.7e+001 Console.WriteLine("{0:x}", x); 11 Console.WriteLine("{0:x4}", x); 0011

13 String Formatting With ToString for numeric types (int, long, short,...): string s; int i = 12; s = i.ToString();// "12" s = i.ToString("x4");// "000c" s = i.ToString("f");// "12.00" Culture-specific formatting s = i.ToString("c");// "$12.00" s = i.ToString("c", new CultureInfo("en-GB"));// "£12.00" s = i.ToString("c", new CultureInfo("de-AT"));// "€12.00" With String.Format for arbitrary types s = String.Format("{0} = {1,6:X4}", name, i); // "val = 000C"

14 Formatted Output to a File using System; using System.IO; class Test { static void Main() { FileStream s = new FileStream("output.txt", FileMode.Create); StreamWriter w = new StreamWriter(s); w.WriteLine("Table of sqares:"); for (int i = 0; i < 10; i++) w.WriteLine("{0,3}: {1,5}", i, i*i); w.Close(); } It is not possible to have multiple StreamWriters working on the same stream at the same time.

15 Keyboard Input int ch = Console.Read(); returns the next character. waits until the user pressed the return key. e.g. input: "abc" + return key. Read returns: 'a', 'b', 'c', '\r', '\n'. after the last character (Ctrl-Z + return) Read returns -1 There is no Tokenizer for formatted input like in Java. string line = Console.ReadLine(); returns the next line (after Ctrl-Z+CR+LF it returns null). waits until the user pressed the return key. returns the line without CR, LF.

16 Input from a File using System; using System.IO; class Test { static void Main() { FileStream s = new FileStream("input.txt", FileMode.Open); StreamReader r = new StreamReader(s); string line = r.ReadLine(); while (line != null) {... line = r.ReadLine(); } r.Close(); } It is not possible to have multiple StreamReaders working on the same stream at the same time.

17 Reading Command-line Parameters using System; class Test { static void Main(string[] arg) {// e.g. invoked as: Test value = 3 for (int i = 0; i < arg.Length; i++) Console.WriteLine("{0}: {1}", i, arg[i]);// output (tokens are separated by blanks): //0: value //1: = //2: 3 foreach (string s in arg) Console.WriteLine(s);// output: //value //= //3 }