Open In App

how to learn how to code

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this ever-evolving digital landscape, the ability to code is not just a valuable asset but a gateway to creative expression and innovation. This brief guide will outline practical steps and essential principles to guide aspiring learners on their path to mastering the art of coding.

1. Introduction, to Basic Syntax. Hello World:

Lets learn the basic syntax of hello world program in different programming languages such as C, C++, Java and Python

C++
#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}
C
#include &lt;stdio.h&gt;

int main() {
    printf(&quot;Hello, World!\n&quot;);
    return 0;
}
Java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println(&quot;Hello, World!&quot;);
    }
}
Python
print(&quot;Hello, World!&quot;)
Javascript
// JavaScript code to print "Hello, World!"
function main() {
    console.log("Hello, World!");
}

// Calling the main function to execute the code
main();

Output
Hello, World!




2. Understanding Data Types:

Data types are crucial, in specifying the type of data that can be stored within a variable. Common data types encompass integers, floating point numbers, characters and various others.

C++
int num = 10;
float pi = 3.14;
char grade = 'A';
C
int num = 10;
float pi = 3.14;
char grade = 'A';
Java
int num = 10;
float pi = 3.14;
char grade = 'A';
Python
num = 10
pi = 3.14
grade = 'A'

3. Variables and Constants:

In programming variables act as containers for data that can be altered during the execution of a program. Conversely constants store data that remains unchanged throughout its course.

  • Declaration and Initialization Process:
C++
int num;          // Declaration
num = 10;         // Initialization
C
int num;          // Declaration
num = 10;         // Initialization
Java
int num;          // Declaration
num = 10;         // Initialization
Python
num = 10          # Declaration and Initialization
  • Access Values stored in Variables and Constants:

Now lets see how we can access the values stored in variables and constants in different programming languages.

C++
std::cout &lt;&lt; &quot;Value of num: &quot; &lt;&lt; num &lt;&lt; std::endl;
C
printf(&quot;Value of num: %d\n&quot;, num);
Java
System.out.println(&quot;Value of num: &quot; + num);
Python
print(&quot;Value of num:&quot;, num)

4. Keywords:

Keywords are reserved words within a programming language that hold predefined meanings. It’s basically important to note that they cannot be used as names or identifiers.

C++
#include &lt;iostream&gt;

int main() {
    int y = 8; // 'int' is a keyword for defining an integer variable
    return 0;
}
C
#include &lt;stdio.h&gt;

int main() {
    int x = 5; // 'int' is a keyword indicating the variable type
    return 0;
}
Java
public class GfgClass {
    public static void main(String[] args) {
        int z = 12; // 'int' is a keyword indicating the variable type
    }
}
Python
def gfgKeyword():
    pass  # 'def' is a keyword used to define a function
  

Output

Here, in the above code we can see there are some reserved keywords that have some already defined meaning such as int, return in C, C++ and Java and def and if..else in python.

5. Operators:

Operators are tools for performing operations on variables and values within code.

Arithmetic operators facilitate tasks, like addition subtraction, multiplication and division.

C++
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
C
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
Java
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
Python
sum = a + b
difference = a - b
product = a * b
quotient = a / b
remainder = a % b


Relational Operators:
Relational operators compare values and return a boolean result.

C++
if (a == b) {
    // equal
}
if (a != b) {
    // not equal
}
if (a &lt; b) {
    // less than
}
if (a &gt; b) {
    // greater than
}
if (a &lt;= b) {
    // less than or equal
}
if (a &gt;= b) {
    // greater than or equal
}
C
if (a == b) {
    // equal
}
if (a != b) {
    // not equal
}
if (a &lt; b) {
    // less than
}
if (a &gt; b) {
    // greater than
}
if (a &lt;= b) {
    // less than or equal
}
if (a &gt;= b) {
    // greater than or equal
}
Java
if (a == b) {
    // equal
}
if (a != b) {
    // not equal
}
if (a &lt; b) {
    // less than
}
if (a &gt; b) {
    // greater than
}
if (a &lt;= b) {
    // less than or equal
}
if (a &gt;= b) {
    // greater than or equal
}
Python
if a == b:
    # equal
if a != b:
    # not equal
if a &lt; b:
    # less than
if a &gt; b:
    # greater than
if a &lt;= b:
    # less than or equal
if a &gt;= b:
    # greater than or equal


Logical Operators:
Logical operators perform logical operations on boolean values.

C++
if (condition1 &amp;&amp; condition2) {
    // logical AND
}
if (condition1 || condition2) {
    // logical OR
}
if (!condition) {
    // logical NOT
}
C
if (condition1 &amp;&amp; condition2) {
    // logical AND
}
if (condition1 || condition2) {
    // logical OR
}
if (!condition) {
    // logical NOT
}
Java
if (condition1 &amp;&amp; condition2) {
    // logical AND
}
if (condition1 || condition2) {
    // logical OR
}
if (!condition) {
    // logical NOT
}
Python
if condition1 and condition2:
    # logical AND
if condition1 or condition2:
    # logical OR
if not condition:
    # logical NOT


Unary, Binary, and Ternary Operators:
Unary operators operate on a single operand, binary operators on two, and ternary operators on three.

C++
int unary = -a; // unary minus
int binary = a + b; // binary plus
int ternary = (a &gt; b) ? a : b; // ternary conditional
C
int unary = -a; // unary minus
int binary = a + b; // binary plus
int ternary = (a &gt; b) ? a : b; // ternary conditional
Java
int unary = -a; // unary minus
int binary = a + b; // binary plus
int ternary = (a &gt; b) ? a : b; // ternary conditional
Python
unary = -a  # unary minus
binary = a + b  # binary plus
ternary = a if a &gt; b else b  # ternary conditional

6. Decision Making statements:

Decision statements are used to control the flow of a program based on conditions.

If…else
The if…else Statement enables the execution of different blocks of code depending on a condition.

C++
if (condition) {
    // code to execute if condition is true
} else {
    // code to execute if condition is false
}
C
if (condition) {
    // code to execute if condition is true
} else {
    // code to execute if condition is false
}
Java
if (condition) {
    // code to execute if condition is true
} else {
    // code to execute if condition is false
}
Python
if condition:
    # code to execute if condition is true
else:
    # code to execute if condition is false


if…else if…
The if…else if…else statement allows checking multiple conditions one, after another.

C++
if (condition1) {
    // code to execute if condition1 is true
} else if (condition2) {
    // code to execute if condition2 is true
} else {
    // code to execute if all conditions are false
}
C
if (condition1) {
    // code to execute if condition1 is true
} else if (condition2) {
    // code to execute if condition2 is true
} else {
    // code to execute if all conditions are false
}
Java
if (condition1) {
    // code to execute if condition1 is true
} else if (condition2) {
    // code to execute if condition2 is true
} else {
    // code to execute if all conditions are false
}
Python
if condition1:
    # code to execute if condition1 is true
elif condition2:
    # code to execute if condition2 is true
else:
    # code to execute if all conditions are false


Switch Statements
The switch statement is utilized to create branches based on the value of an expression.

C++
switch (expression) {
    case value1:
        // code block
        break;
    case value2:
        // code block
        break;
    default:
        // code block
}
C
switch (expression) {
    case value1:
        // code block
        break;
    case value2:
        // code block
        break;
    default:
        // code block
}
Java
switch (expression) {
    case value1:
        // code block
        break;
    case value2:
        // code block
        break;
    default:
        // code block
}
Python
def switch(someCase):
    if case1:
        // code block
    elif case2:
        // code block
    elif case3:
        // code block
    else: 
        // code block

7. Loops:

Entry Condition Loop:

Loops that are controlled by an entry condition execute the code block long as the condition remains true.

C++
#include &lt;iostream&gt;

int main() {
    int i = 0;

    while (i &lt; 5) {
        std::cout &lt;&lt; i &lt;&lt; &quot; &quot;;
        i++;
    }

    return 0;
}
C
#include &lt;stdio.h&gt;

int main() {
    int i = 0;

    while (i &lt; 5) {
        printf(&quot;%d &quot;, i);
        i++;
    }

    return 0;
}
Java
public class EntryConditionLoop {
    public static void main(String[] args) {
        int i = 0;

        while (i &lt; 5) {
            System.out.print(i + &quot; &quot;);
            i++;
        }
    }
}
Python
i = 0

while i &lt; 5:
    print i,
    i += 1
    
Javascript
// Initialize a variable i with the value 0.
let i = 0;

// Use a while loop to iterate while i is less than 5.
while (i < 5) {
    // Print the current value of i to the console.
    console.log(i + " ");

    // Increment the value of i by 1.
    i++;
}

Output
0 1 2 3 4 







Exit Condition Loop:

Loops that are controlled by an exit condition execute the code block until the condition becomes false.

C++
#include <iostream>

int main() {
    int i = 0;

    do {
        std::cout << i << " ";
        i++;
    } while (i < 5);

    return 0;
}
C
#include <stdio.h>

int main() {
    int i = 0;

    do {
        printf("%d ", i);
        i++;
    } while (i < 5);

    return 0;
}
Java
public class ExitConditionLoop {
    public static void main(String[] args) {
        int i = 0;

        do {
            System.out.print(i + " ");
            i++;
        } while (i < 5);
    }
}
Python
i = 0

while i < 5:
    print i,
    i += 1
Javascript
 

// Initialize a variable i with the value 0.
let i = 0;

// Use a do-while loop to ensure that the code inside the loop block is executed at least once.
do {
    // Print the current value of i to the console.
    console.log(i + " ");

    // Increment the value of i by 1.
    i++;
} while (i < 5);

Output
0 1 2 3 4 







8. Numbers:

Performing operations, on numbers involves addition, subtraction, multiplication and division.

C++
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
C
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
Java
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
Python
sum = a + b
difference = a - b
product = a * b
quotient = a / b
remainder = a % b


9. Characters:

Special characters called escape sequences are used to represent printable characters, such, as newline and tab.

  1. \n: Newline character.
  2. \t: Tab character.
  3. \”: Double quote character.
  4. \’: Single quote character.
  5. \\: Backslash character.
C++
#include &lt;iostream&gt;

int main() {
    printf(&quot;Hello, world!\nThis is a new line.\n&quot;);
    printf(&quot;Tabbed\ttext.\n&quot;);
    printf(&quot;Double quote: \&quot;\n&quot;);
    printf(&quot;Single quote: \'\n&quot;);
    printf(&quot;Backslash: \\n&quot;);
    return 0;
}
C
#include &lt;stdio.h&gt;

int main() {
    printf(&quot;Hello, world!\nThis is a new line.\n&quot;);
    printf(&quot;Tabbed\ttext.\n&quot;);
    printf(&quot;Double quote: \&quot;\n&quot;);
    printf(&quot;Single quote: \'\n&quot;);
    printf(&quot;Backslash: \\n&quot;);
    return 0;
}
Java
public class Main {
    public static void main(String[] args) {
        System.out.println(&quot;Hello, world!\nThis is a new line.&quot;);
        System.out.println(&quot;Tabbed\ttext.&quot;);
        System.out.println(&quot;Double quote: \&quot;&quot;);
        System.out.println(&quot;Single quote: \'&quot;);
        System.out.println(&quot;Backslash: \\&quot;);
    }
}
Python
print(&quot;Hello, world!\nThis is a new line.&quot;)
print(&quot;Tabbed\ttext.&quot;)
print(&quot;Double quote: \&quot;&quot;)
print(&quot;Single quote: \'&quot;)
print(&quot;Backslash: \\&quot;)
Javascript
console.log("Hello, world!\nThis is a new line.");
console.log("Tabbed\ttext.");
console.log("Double quote: \"");
console.log("Single quote: \'");
console.log("Backslash: \\");

Output
Hello, world!
This is a new line.
Tabbed    text.
Double quote: "
Single quote: '
Backslash: \n







10. Arrays:

Arrays are data structures that are utilized to store and manipulate collections of elements. In languages, like C and C++ arrays are declared with a predetermined size. Initialized with values while in Java, dynamic arrays can be initialized using braces. Python on the hand provides a syntax for creating arrays. Regardless of the programming language used elements, within arrays can be accessed through indices, enabling retrieval and manipulation of data stored in the arrays.

  • Create Arrays
  • Initialize Arrays
  • Access elements in arrays
C++
#include &lt;iostream&gt;

int main() {
    // Creating and Initializing an array
    int myArray[5] = {1, 2, 3, 4, 5};

    // Accessing elements in the array
    for (int i = 0; i &lt; 5; i++) {
        std::cout &lt;&lt; &quot;Element at index &quot; &lt;&lt; i &lt;&lt; &quot;: &quot; &lt;&lt; myArray[i] &lt;&lt; std::endl;
    }

    return 0;
}
C
#include &lt;stdio.h&gt;

int main() {
    // Creating and Initializing an array
    int myArray[5] = {1, 2, 3, 4, 5};

    // Accessing elements in the array
    for (int i = 0; i &lt; 5; i++) {
        printf(&quot;Element at index %d: %d\n&quot;, i, myArray[i]);
    }

    return 0;
}
Java
public class Array {
    public static void main(String[] args) {
        // Creating and Initializing an array
        int[] myArray = {1, 2, 3, 4, 5};

        // Accessing elements in the array
        for (int i = 0; i &lt; myArray.length; i++) {
            System.out.println(&quot;Element at index &quot; + i + &quot;: &quot; + myArray[i]);
        }
    }
}
Python
# Creating and Initializing an array
my_array = [1, 2, 3, 4, 5]

# Accessing elements in the array
for i in range(len(my_array)):
    print(&quot;Element at index {}: {}&quot;.format(i, my_array[i]))
Javascript
// Creating and Initializing an array
let myArray = [1, 2, 3, 4, 5];

// Accessing elements in the array
for (let i = 0; i < 5; i++) {
    console.log("Element at index " + i + ": " + myArray[i]);
}

Output
Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Element at index 3: 4
Element at index 4: 5








11. Strings- Basic String Concepts

Strings, which are sequences of characters are data types, in programming languages. They serve the purpose of representing and manipulating information. Common tasks performed on strings include combining them extracting substrings determining their length and comparing them.

In C and C++ strings are represented as arrays of characters that end with a character (‘\0’). On the hand in Python and Java strings are treated as objects with built in methods, for performing operations.

C++
#include <iostream> 
#include <string> 

int main() { 
    // String declaration and initialization 
    std::string myString = "Hello, GeeksforGeeks!"; 

    // String length 
    std::cout << "Length of the string: " << myString.length() << std::endl; 

    // String concatenation 
    std::string anotherString = " How are you?"; 
    myString += anotherString; 
    std::cout << "Concatenated string: " << myString << std::endl; 

    // Accessing individual characters 
    std::cout << "First character: " << myString[0] << std::endl; 

    return 0; 
} 
C
#include <stdio.h> 
#include <string.h> 

int main() { 
    // String declaration and initialization 
    char myString[] = "Hello, GeeksforGeeks!"; 

    // String length 
    printf("Length of the string: %lu\n", strlen(myString)); 

    // String concatenation 
    char anotherString[] = " How are you?"; 
    strcat(myString, anotherString); 
    printf("Concatenated string: %s\n", myString); 

    // Accessing individual characters 
    printf("First character: %c\n", myString[0]); 

    return 0; 
} 
Java
public class StringExample { 
    public static void main(String[] args) { 
        // String declaration and initialization 
        String myString = "Hello, GeeksforGeeks!"; 

        // String length 
        System.out.println("Length of the string: " + myString.length()); 

        // String concatenation 
        String anotherString = " How are you?"; 
        myString = myString.concat(anotherString); 
        System.out.println("Concatenated string: " + myString); 

        // Accessing individual characters 
        System.out.println("First character: " + myString.charAt(0)); 
    } 
} 
Python
# String declaration and initialization 
my_string = "Hello, GeeksforGeeks!"

# String length 
print("Length of the string:", len(my_string)) 

# String concatenation 
another_string = " How are you?"
my_string += another_string 
print("Concatenated string:", my_string) 

# Accessing individual characters 
print("First character:", my_string[0]) 
C#
using System;

class StringExample {
    static void Main()
    {
        // String declaration and initialization
        string myString = "Hello, GeeksforGeeks!";

        // String length
        Console.WriteLine("Length of the string: "
                          + myString.Length);

        // String concatenation
        string anotherString = " How are you?";
        myString += anotherString;
        Console.WriteLine("Concatenated string: "
                          + myString);

        // Accessing individual characters
        Console.WriteLine("First character: "
                          + myString[0]);
    }
}
Javascript
class StringExample { 
    static main() { 
        // String declaration and initialization 
        let myString = "Hello, GeeksforGeeks!"; 

        // String length 
        console.log("Length of the string: " + myString.length); 

        // String concatenation 
        let anotherString = " How are you?"; 
        myString += anotherString; 
        console.log("Concatenated string: " + myString); 

        // Accessing individual characters 
        console.log("First character: " + myString.charAt(0)); 
    } 
} 

// Call the main method 
StringExample.main(); 

Output
Length of the string: 21
Concatenated string: Hello, GeeksforGeeks! How are you?
First character: H








12. Functions:

Functions are, like building blocks of code that can be reused to accomplish tasks. They play a role in organizing code making it easier to read and maintain. When defining a function you enclose a set of instructions within braces. You can also specify parameters (inputs). Return values (outputs).

C++
#include &lt;iostream&gt;

// Function declaration
void greet() {
    std::cout &lt;&lt; &quot;Hello, GeeksforGeeks!&quot; &lt;&lt; std::endl;
}

int main() {
    // Calling the function
    greet();
    return 0;
}
C
#include &lt;stdio.h&gt;

// Function declaration
void greet() {
    printf(&quot;Hello, GeeksforGeeks!\n&quot;);
}

int main() {
    // Calling the function
    greet();
    return 0;
}
Java
public class FunctionExample {
    // Function declaration
    static void greet() {
        System.out.println(&quot;Hello, GeeksforGeeks!&quot;);
    }

    public static void main(String[] args) {
        // Calling the function
        greet();
    }
}
Python
# Function definition
def greet():
    print(&quot;Hello, GeeksforGeeks!&quot;)

# Calling the function
greet()
Javascript
// Function declaration
function greet() {
    console.log("Hello, GeeksforGeeks!");
}

// Calling the function
greet();

Output
Hello, GeeksforGeeks!










Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads