c# syntax and output
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 PresentationTRANSCRIPT
![Page 1: C# Syntax and Output](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/1.jpg)
![Page 2: C# Syntax and Output](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/2.jpg)
public class CompSci{
}
All C# programs start with a class.
![Page 3: C# Syntax and Output](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/3.jpg)
public class CompSci{ public static void Main(String[] args) { Console.WriteLine("Comp Sci!"); }} OUTPUT
Comp Sci!
![Page 4: C# Syntax and Output](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/4.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/5.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/6.jpg)
Never put a ; before an open { brace
;{ //illegal}; //legal
![Page 7: C# Syntax and Output](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/7.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/8.jpg)
![Page 9: C# Syntax and Output](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/9.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/10.jpg)
Console.Write("compsci");
reference command / method
OUTPUTcompsci
![Page 11: C# Syntax and Output](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/11.jpg)
Console.Write("compsci");Console.Write("compsci");
OUTPUTcompscicompsci
![Page 12: C# Syntax and Output](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/12.jpg)
Console.WriteLine("compsci");
OUTPUTcompsci
![Page 13: C# Syntax and Output](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/13.jpg)
Console.WriteLine("compsci");Console.WriteLine("compsci");
OUTPUTcompscicompsci
![Page 14: C# Syntax and Output](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/14.jpg)
Console.WriteLine("c\tompsci");
\n newline\t tab\r carriage return\b backspace
OUTPUTc ompsci
![Page 15: C# Syntax and Output](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/15.jpg)
Console.WriteLine("com\tpsci");
OUTPUTcom psci
\n newline\t tab\r carriage return\b backspace
![Page 16: C# Syntax and Output](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/16.jpg)
Console.WriteLine("comp\nsci");
OUTPUTcompsci
\n newline\t tab\r carriage return\b backspace
![Page 17: C# Syntax and Output](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/17.jpg)
\\ outs \\" outs "\’ outs ’
Console.WriteLine("\\compsci\"/");
OUTPUT\compsci"/
![Page 18: C# Syntax and Output](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/18.jpg)
\\ outs \\" outs "\’ outs ’
Console.WriteLine("\\'comp\'sci\'/");
OUTPUT\'comp'sci'/
![Page 19: C# Syntax and Output](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/19.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/20.jpg)
// single-line comments/* */ block comments
//this line prints stuff on the screenConsole.WriteLine("stuff");
![Page 21: C# Syntax and Output](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/21.jpg)
// single-line comments/* */ block comments
/* this line prints stuff on the screen*/Console.WriteLine("stuff");
![Page 22: C# Syntax and Output](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/22.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022081421/568148ab550346895db5bd5a/html5/thumbnails/23.jpg)
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] );