C# Syntax and Output

23

description

C# Syntax and Output. Lab 0A. A bare bones class!. public class CompSci { }. All C# programs start with a class. bare bones + a Main!. public class CompSci { public static void Main (String[] args) { Console.WriteLine("Comp Sci!"); } }. OUTPUT Comp Sci!. - PowerPoint PPT Presentation

Transcript of C# Syntax and Output

Page 1: C# Syntax and Output
Page 2: C# Syntax and Output

public class CompSci{

}

All C# programs start with a class.

Page 3: C# Syntax and Output

public class CompSci{ public static void Main(String[] args) { Console.WriteLine("Comp Sci!"); }} OUTPUT

Comp Sci!

Page 4: C# Syntax and Output

public class CompSci{ //open brace

public static void Main(String[] args) { Console.WriteLine ("Comp Sci!"); }} //close brace

Braces – You gotta have ‘em! Every classand every method must have a { and a } .

Page 5: C# Syntax and Output

public class CompSci{ public static void main(String[] args) { Console.WriteLine("Comp Sci!"); }}You must put a semi-colon at the end of all C# program statements ( ; ).

Page 6: C# Syntax and Output

Never put a ; before an open { brace

;{ //illegal}; //legal

Page 7: C# Syntax and Output

public class CompSci{ public static void Main(String[] args) { Console.WriteLine("Comp Sci!"); }}

Indent all code 3 spaces to make it easier to read.

Page 8: C# Syntax and Output
Page 9: C# Syntax and Output

Console.frequently used methods

Name UseConsole.Write print x and stay on the current line

Console.WriteLine

print x and move to next line down

Page 10: C# Syntax and Output

Console.Write("compsci");

reference command / method

OUTPUTcompsci

Page 11: C# Syntax and Output

Console.Write("compsci");Console.Write("compsci");

OUTPUTcompscicompsci

Page 12: C# Syntax and Output

Console.WriteLine("compsci");

OUTPUTcompsci

Page 13: C# Syntax and Output

Console.WriteLine("compsci");Console.WriteLine("compsci");

OUTPUTcompscicompsci

Page 14: C# Syntax and Output

Console.WriteLine("c\tompsci");

\n newline\t tab\r carriage return\b backspace

OUTPUTc ompsci

Page 15: C# Syntax and Output

Console.WriteLine("com\tpsci");

OUTPUTcom psci

\n newline\t tab\r carriage return\b backspace

Page 16: C# Syntax and Output

Console.WriteLine("comp\nsci");

OUTPUTcompsci

\n newline\t tab\r carriage return\b backspace

Page 17: C# Syntax and Output

\\ outs \\" outs "\’ outs ’

Console.WriteLine("\\compsci\"/");

OUTPUT\compsci"/

Page 18: C# Syntax and Output

\\ outs \\" outs "\’ outs ’

Console.WriteLine("\\'comp\'sci\'/");

OUTPUT\'comp'sci'/

Page 19: C# Syntax and Output

Escape Sequencesfrequently used combinations

Name Use

\t tabs over five spaces

\n moves to front of next line

\b deletes previous character

\r moves to front of current line

\\ nets one backslash \

\" nets one double quote "

\’ nets one single quote ’

Page 20: C# Syntax and Output

// single-line comments/* */ block comments

//this line prints stuff on the screenConsole.WriteLine("stuff");

Page 21: C# Syntax and Output

// single-line comments/* */ block comments

/* this line prints stuff on the screen*/Console.WriteLine("stuff");

Page 22: C# Syntax and Output

Syntax errors occur when you type something in wrong, causing the code to not compile.

//missing semicolon - ; expectedConsole.WriteLine("stuff")

//case problem – should be SystemConsole.WriteLine("stuff")

Page 23: C# Syntax and Output

Runtime errors occur when something goes wrong while the program is running.

//an out of bounds exception is thrownString s = "runtime_error";Console.WriteLine( s[15] );