1 / 23

C# Syntax and Output

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

astrid
Télécharger la présentation

C# Syntax and Output

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C# Syntax and Output Lab 0A

  2. A bare bones class! public class CompSci { } All C# programs start with a class.

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

  4. syntax rules public class CompSci { //open brace public static void Main(String[] args) { Console.WriteLine ("Comp Sci!"); } } //close brace Braces – You gotta have ‘em! Every class and every method must have a { and a } .

  5. syntax rules 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 ( ; ).

  6. { and ; rule Never put a ; before an open { brace ;{ //illegal }; //legal

  7. indentation public class CompSci { public static void Main(String[] args) { Console.WriteLine("Comp Sci!"); } } Indent all code 3 spaces to make it easier to read. 123

  8. C# Output

  9. C# Output reference command / method Console.Write("compsci"); OUTPUT compsci

  10. C# Output Console.Write("compsci"); Console.Write("compsci"); OUTPUT compscicompsci

  11. C# Output Console.WriteLine("compsci"); OUTPUT compsci

  12. C# Output Console.WriteLine("compsci"); Console.WriteLine("compsci"); OUTPUT compscicompsci

  13. C# Output Escape Sequences \n newline \t tab \r carriage return \b backspace Console.WriteLine("c\tompsci"); OUTPUT c ompsci

  14. C# Output Escape Sequences \n newline \t tab \r carriage return \b backspace Console.WriteLine("com\tpsci"); OUTPUT com psci

  15. C# Output Escape Sequences \n newline \t tab \r carriage return \b backspace Console.WriteLine("comp\nsci"); OUTPUT compsci

  16. C# Output Escape Sequences \\ outs \ \" outs " \’ outs ’ Console.WriteLine("\\compsci\"/"); OUTPUT \compsci"/

  17. C# Output Escape Sequences \\ outs \ \" outs " \’ outs ’ Console.WriteLine("\\'comp\'sci\'/"); OUTPUT \'comp'sci'/

  18. C# Comments // single-line comments /* */ block comments //this line prints stuff on the screen Console.WriteLine("stuff");

  19. C# Comments // single-line comments /* */ block comments /* this line prints stuff on the screen */ Console.WriteLine("stuff");

  20. Errors Syntax errors occur when you type something in wrong, causing the code to not compile. //missing semicolon - ; expected Console.WriteLine("stuff") //case problem – should be System Console.WriteLine("stuff")

  21. Errors Runtime errors occur when something goes wrong while the program is running. //an out of bounds exception is thrown String s = "runtime_error"; Console.WriteLine( s[15] );

More Related