Writing Your First Code
Here is your first code in different languages. These programs all achieve the same goal: printing “Hello, world!” to the console. However, they use different syntax and conventions specific to each language.
Printing “Hello world” in C++:
C++
// Includes the iostream library for input and output. #include <iostream> // Defines the main function, where the program execution starts. int main() { // Prints the string "Hello, world!" to the console. std::cout << "Hello, world!" << std::endl; // Exits the program and returns an exit code of 0, indicating success. return 0; } |
Hello, world!
Explanation of above C++ code:
- #include: This keyword includes the <iostream> library, which provides functions for input and output.
- int main(): This defines the main function, which is the entry point of the program.
- std::cout <<: This keyword prints the following expression to the console.
- “Hello, world!” This is the string that is printed to the console.
- std::endl: This keyword inserts a newline character after the printed string.
- return 0; This statement exits the program and returns a success code (0).
Printing “Hello world” in Java:
Java
// Defines a public class named HelloWorld. public class HelloWorld { // Declares the main function. public static void main(String[] args) { // Prints the string "Hello, world!" to the // console. System.out.println( "Hello, world!" ); } } |
Hello, world!
Explanation of above Java code:
- public class HelloWorld: This keyword defines a public class named HelloWorld.
- public static void main(String[] args): This declares the main function, which is the entry point of the program.
- System.out.println(“Hello, world!”); This statement prints the string “Hello, world!” to the console.
Printing “Hello world” in Python:
Python
# Simply prints the string "Hello, world!" to the console. print ( "Hello, world!" ) |
Hello, world!
Explanation of above Python code:
- print: This keyword prints the following argument to the console.
- “Hello, world!” This is the string that is printed to the console.
Printing “Hello world” in Javascript:
Javascript
// Prints the string "Hello, world!" to the console using the console object's log method. console.log( "Hello, world!" ); |
Hello, world!
Explanation of above Javascript code:
- console.log: This object’s method prints the following argument to the console.
- “Hello, world!” This is the string that is printed to the console.
Printing “Hello world” in PHP:
PHP
<?php // Starts the PHP code block. // Prints the string "Hello, world!" to the console using the echo function. echo "Hello, world!" ; // Ends the PHP code block. ?> |
Hello, world!
Explanation of above PHP code:
- <?php: This tag initiates a PHP code block.
- echo: This keyword prints the following expression to the console.
- “Hello, world!” This is the string that is printed to the console.
- ?>: This tag ends the PHP code block.
Programming Tutorial | Introduction, Basic Concepts, Getting started, Problems
This comprehensive guide of Programming Tutorialor Coding Tutorial provides an introduction to programming, covering basic concepts, setting up your development environment, and common beginner problems. Learn about variables, data types, control flow statements, functions, and how to write your first code in various languages. Explore resources and tips to help you to begin your programming journey. We designed this Programming Tutorial
or Coding Tutorial to empower beginners and equip them with the knowledge and resources they will need to get started with programming.