Coding Syntax and Comments in Different Languages

Objectives:

  • Learn how to use comments in your code.
  • The entry point in various programming languages.
  • The purpose of code blocks, line breaks, and white space in various programming languages.
  • Single quotes, double quotes, backticks and string definition.

Resources:

Contents

  1. Using Comments in Different Programming Languages
  2. Understanding the 'Main' Function as the Entry Point in Different Programming Languages
  3. White Spaces and New Lines in Coding Syntax
  4. Defining Code Blocks in Programming Languages
  5. Usage of Semicolons to End Lines in Programming Languages
  6. Using Single and Double Quotes to Define Strings in Programming Languages
  7. Multiple Line Strings in Coding Languages
  8. Basic Coding Formatting Guidelines
  9. Other Language Prompts

1. Using Comments in Different Programming Languages

In coding, comments are annotations or explanatory notes that are added to the source code by developers. They are not executed as part of the program and do not affect its functionality. Comments are used to provide additional information, clarification, or context to make the code more readable and understandable for developers, including the original author and other programmers who might work on the code in the future.

Comments are typically written in natural language (such as English) and can be used to explain the purpose of code segments, document assumptions or constraints, describe algorithms or data structures, provide instructions, or even disable specific lines of code temporarily.

Comments are ignored by the compiler or interpreter, which means they do not contribute to the execution of the program. They serve as a means of communication and documentation for developers. Comments can be found in various programming languages, including C, C++, Java, Python, JavaScript, Dart, and many others.

Single Line Comments:

In JavaScript/TypeScript, Dart, PHP, Java, C++, Swift, Kotlin, Objective-C and Golang, single line comments are made using //. Everything on the same line after // is considered a comment.

Multi-line Comments:

In JavaScript/TypeScript, Dart, PHP, Java, C/C++, Swift, Kotlin, Objective-C and Golang, multi-line comments are enclosed between /* and */.

For demo purposes, the code below will be automatically added a main function if necessary when running.

Python Specific:

Python uses # for single line comments. For multi-line comments, Python typically uses triple quotes ''' or """, even though it's technically a string, not a comment.

PHP can use # for single line comments too.

In C, C++, and Objective-C, the # symbol is primarily used for preprocessor directives. We will talk this later.

Guiding AI: With the advent of AI-based code completion tools, comments can provide context and intent to the AI, aiding in more accurate and context-specific code generation.

We use comments to instruct AI to calculate the area of a triangle in different languages

In short, comments provide extra information about your code, helping both you and others to understand it better.

2. Understanding the 'Main' Function as the Entry Point in Different Programming Languages

When you write a program, you need to tell the computer where to start executing your code. This is called the entry point or starting point. In most programming languages, the entry point is a function called main().

Languages with Explicit Main Functions:

Dart, Kotlin, Java, C/C++, Objective-C and Golang all have explicit main functions that serve as the entry point of a program. In these languages, the main function is where your program begins executing commands.

Languages with Different Conventions:

Python doesn't have an explicit main function. However, in Python, a "main code" block can be created by placing it within an if name == 'main': clause. This ensures that the "main code" block is not executed when the file is imported as a module.

Languages with No Entry Points: JavaScript/TypeScript, PHP, Swift don't have an explicit main function or a special convention for defining the entry point. Instead, the program starts executing from the first line of the global scope of your code.

3. Usage of Semicolons to End Lines in Programming Languages

Mandatory: Dart, PHP, Java, C/C++, Objective-C require semicolons ; at the end of each statement.

Optional: JavaScript/TypeScript, Kotlin, Swift and Golang semicolons are technically optional at the end of statements. However, their use is often considered good practice for clarity and avoiding potential issues.

Not Required: Python does not require semicolons to end statements. Semicolons can be used to separate multiple statements on a single line, but this is not common practice.

4. Defining Code Blocks in Programming Languages

JavaScript/TypeScript, Dart, PHP, Java, Swift, Kotlin, C/C++, Objective-C and Golang use curly braces {} to group related lines of code together. These grouped lines, called code blocks, are often used in functions, loops, or conditionals.

For demo purposes, the code below will be automatically added a main function if necessary when running.

Python, however, uses indentation (spaces at the start of a line) to define its code blocks. Instead of curly braces, each group of indented lines forms a block, helping Python to understand which lines of code should be treated as a unit.

5. Using Single and Double Quotes to Define Strings in Programming Languages

For demo purposes, the code below will be automatically added a main function if necessary when running.

In Dart, JavaScript/TypeScript, PHP, Python, strings can be defined using either single quotes or double quotes. Both ways are equivalent, and the choice usually comes down to programmer's preference or to avoid escaping within the string.

In languages like Java, Kotlin, C/C++, Objective-C, and Swift, and Golang strings are typically defined using double quotes. Single quotes are used to define character literals in Java, Kotlin, C/C++ and Objective-C.

In Swift, single quotes are not used for anything and double quotes can also be used for a character literal

In Go, character literals are represented using single quotes ('). However, Go does not have a dedicated character type like some other programming languages. Instead, characters in Go are represented using their corresponding Unicode code points, which are integer values.

6. Multiple Line Strings in Coding Languages

In Dart/Python/Kotlin/Swift/Java(JDK 13), use triple quotation marks to define a multiline string.

In JavaScript/TypeScript/Golang, use backticks ` (grave accent) to define a multiline string.

In PHP, use the HEREDOC or NOWDOC syntax for multiline strings. In C/C++/Objective-c, you can break the string into multiple lines and concatenate them.

7. White Spaces and New Lines in Coding Syntax

In JavaScript/TypeScript, Dart, PHP, Java, Swift, Kotlin, C/C++, Objective-C and Golang, white spaces (like spaces and tabs) and new lines don't change your code's meaning, unless they're inside text (known as "strings").

Python is a bit different: it uses white spaces at the start of lines to group parts of your code together. New lines usually mean "this instruction is done," unless your code is inside brackets.

In all these languages, inside strings, spaces and new lines count as part of the text.

8. Basic Coding Formatting Guidelines

  • Indentation: Use spaces (typically four) to show code structure. This makes your code easier to read.

  • Line Length: Try to keep your lines short, ideally under 80 characters. This prevents horizontal scrolling and makes your code more viewable.

  • Blank Lines: Use blank lines to separate sections of your code. This helps you see different parts more clearly.

  • Spacing with Operators and Commas: Put spaces around operators (like = or +) and after commas. For example, x = y + z and my_function(x, y, z) are easier to read.

  • Avoid Trailing White Spaces: Don't leave spaces at the end of lines. They don't add value and can make version control messy.

  • Spaces and Brackets: Avoid spaces inside brackets. Write my_function(x), not my_function( x ).

These guidelines help to make your code cleaner and more understandable. Remember to also check any style guides related to your specific programming language.

9. Other Language Prompts

We also provided some AI prompts for other popular coding languages that were not mentioned in this unit:

  • Using code examples to explain Ruby language's syntax: white spaces, line breaks, comments and code block etc.
  • Using code examples to explain Rust language's syntax: white spaces, line breaks, comments and code block etc.
  • Using code examples to explain C# language's syntax: white spaces, line breaks, comments and code block etc.
  • Using code examples to explain Lua script's syntax: white spaces, line breaks, comments and code block etc.
  • Using code examples to explain Bash script's syntax: white spaces, line breaks, comments and code block etc.

JuniorIT.AI

Acquire the essential skills and knowledge to kickstart your career and set off on a new path in the age of AI.

Haicam Technologies © 2023
Powered by OpenAI & Stable Diffusion