How to Start Coding
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.
Table of Content
- Introduction, to Basic Syntax. Hello World
- Understanding Data Types
- Variables and Constants
- Keywords
- Operators
- Decision Making statements
- Loops
- Numbers
- Characters
- Arrays
- Strings- Basic String Concepts
- Functions
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
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
#include<stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!\n");
}
}
print("Hello, World!")
// 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.
int num = 10;
float pi = 3.14;
char grade = 'A';
int num = 10;
float pi = 3.14;
char grade = 'A';
int num = 10;
float pi = 3.14;
char grade = 'A';
num = 10
pi = 3.14
grade = 'A'
// Declaring variables in JavaScript
let num = 10; // Integer variable
let pi = 3.14; // Floating-point variable
let grade = 'A'; // Character variable (represented as a string)
// Printing the values
console.log("num:", num);
console.log("pi:", pi);
console.log("grade:", grade);
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:
int num; // Declaration
num = 10; // Initialization
int num; // Declaration
num = 10; // Initialization
int num; // Declaration
num = 10; // Initialization
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.
std::cout << "Value of num: " << num << std::endl;
printf("Value of num: %d\n", num);
System.out.println("Value of num: " + num);
print("Value of num:", 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.
#include <iostream>
int main() {
int y = 8; // 'int' is a keyword for defining an integer variable
return 0;
}
#include<stdio.h>
int main() {
int x = 5; // 'int' is a keyword indicating the variable type
return 0;
}
public class GfgClass {
public static void main(String[] args) {
int z = 12; // 'int' is a keyword indicating the variable type
}
}
def gfgKeyword():
pass # 'def' is a keyword used to define a function
// JavaScript doesn't require explicit data type declaration
// so there's no need to specify 'int' for variable declaration
// Define a variable 'y' and assign the value 8 to it
let y = 8;
// There's no need for a return statement in JavaScript for this case
// as it is not a required
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.
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
sum = a + b
difference = a - b
product = a * b
quotient = a / b
remainder = a % b
let a, b;
let sum = a + b;
let difference = a - b;
let product = a * b;
let quotient = a / b;
let remainder = a % b;
Relational Operators:
Relational operators compare values and return a boolean result.
#include <iostream>
int main() {
int a = 10;
int b = 20;
if (a == b) {
// equal
std::cout << "a is equal to b" << std::endl;
}
if (a != b) {
// not equal
std::cout << "a is not equal to b" << std::endl;
}
if (a < b) {
// less than
std::cout << "a is less than b" << std::endl;
}
if (a > b) {
// greater than
std::cout << "a is greater than b" << std::endl;
}
if (a <= b) {
// less than or equal
std::cout << "a is less than or equal to b" << std::endl;
}
if (a >= b) {
// greater than or equal
std::cout << "a is greater than or equal to b" << std::endl;
}
return 0;
}
#include <stdio.h>
int main() {
int a = 10;
int b = 20;
if (a == b) {
// equal
printf("a is equal to b\n");
}
if (a != b) {
// not equal
printf("a is not equal to b\n");
}
if (a < b) {
// less than
printf("a is less than b\n");
}
if (a > b) {
// greater than
printf("a is greater than b\n");
}
if (a <= b) {
// less than or equal
printf("a is less than or equal to b\n");
}
if (a >= b) {
// greater than or equal
printf("a is greater than or equal to b\n");
}
return 0;
}
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
if (a == b) {
// equal
System.out.println("a is equal to b");
}
if (a != b) {
// not equal
System.out.println("a is not equal to b");
}
if (a < b) {
// less than
System.out.println("a is less than b");
}
if (a > b) {
// greater than
System.out.println("a is greater than b");
}
if (a <= b) {
// less than or equal
System.out.println("a is less than or equal to b");
}
if (a >= b) {
// greater than or equal
System.out.println("a is greater than or equal to b");
}
}
}
a = 10
b = 20
if a == b:
# equal
print('a is equal to b')
if a != b:
# not equal
print('a is not equal to b')
if a < b:
# less than
print('a is less than b')
if a > b:
# greater than
print('a is greater than b')
if a <= b:
# less than or equal
print('a is less than or equal to b')
if a >= b:
# greater than or equal
print('a is greater than or equal to b')
let a = 10;
let b = 20;
if (a == b) {
// equal
console.log('a is equal to b');
}
if (a != b) {
// not equal
console.log('a is not equal to b');
}
if (a < b) {
// less than
console.log('a is less than b');
}
if (a > b) {
// greater than
console.log('a is greater than b');
}
if (a <= b) {
// less than or equal
console.log('a is less than or equal to b');
}
if (a >= b) {
// greater than or equal
console.log('a is greater than or equal to b');
}
Output
a is not equal to b a is less than b a is less than or equal to b
Logical Operators:
Logical operators perform logical operations on boolean values.
if (condition1 && condition2) {
// logical AND
}
if (condition1 || condition2) {
// logical OR
}
if (!condition) {
// logical NOT
}
if (condition1 && condition2) {
// logical AND
}
if (condition1 || condition2) {
// logical OR
}
if (!condition) {
// logical NOT
}
if (condition1 && condition2) {
// logical AND
}
if (condition1 || condition2) {
// logical OR
}
if (!condition) {
// logical NOT
}
if condition1 and condition2:
# logical AND
if condition1 or condition2:
# logical OR
if not condition:
# logical NOT
if (condition1 && condition2) {
// logical AND: Executes if both condition1 and condition2 are true
}
if (condition1 || condition2) {
// logical OR: Executes if either condition1 or condition2 (or both) are true
}
if (!condition) {
// logical NOT: Executes if condition is false
}
Unary, Binary, and Ternary Operators:
Unary operators operate on a single operand, binary operators on two, and ternary operators on three.
int unary = -a; // unary minus
int binary = a + b; // binary plus
int ternary = (a > b) ? a : b; // ternary conditional
int unary = -a; // unary minus
int binary = a + b; // binary plus
int ternary = (a > b) ? a : b; // ternary conditional
int unary = -a; // unary minus
int binary = a + b; // binary plus
int ternary = (a > b) ? a : b; // ternary conditional
unary = -a # unary minus
binary = a + b # binary plus
ternary = a if a > 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.
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
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.
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
}
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
}
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
}
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
let condition1 = true;
let condition2 = false;
if (condition1) {
console.log("Condition 1 is true.");
} else if (condition2) {
console.log("Condition 2 is true.");
} else {
console.log("All conditions are false.");
}
Switch Statements: The switch statement is utilized to create branches based on the value of an expression.
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
default:
// code block
}
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
default:
// code block
}
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
default:
// code block
}
def switch(someCase):
if case1:
// code block
elif case2:
// code block
elif case3:
// code block
else:
// code block
switch (expression) {
case value1:
// This block of code is executed if the expression equals value1
break;
case value2:
// This block of code is executed if the expression equals value2
break;
default:
// This block of code is executed if the expression doesn't match any of the cases
}
7. Loops:
Entry Condition Loop:
Loops that are controlled by an entry condition execute the code block long as the condition remains true.
#include <iostream>
int main() {
int i = 0;
while (i < 5) {
std::cout << i << " ";
i++;
}
return 0;
}
#include<stdio.h>
int main() {
int i = 0;
while (i < 5) {
printf("%d", i);
i++;
}
return 0;
}
public class EntryConditionLoop {
public static void main(String[] args) {
int i = 0;
while (i < 5) {
System.out.print(i + " ");
i++;
}
}
}
i = 0
while i < 5:
print i,
i += 1
/ 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.
#include <iostream>
int main() {
int i = 0;
do {
std::cout << i << " ";
i++;
} while (i < 5);
return 0;
}
#include <stdio.h>
int main() {
int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 5);
return 0;
}
public class ExitConditionLoop {
public static void main(String[] args) {
int i = 0;
do {
System.out.print(i + " ");
i++;
} while (i < 5);
}
}
i = 0
while i < 5:
print i,
i += 1
// 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.
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
sum = a + b
difference = a - b
product = a * b
quotient = a / b
remainder = a % b
let sum = a + b;
let difference = a - b;
let product = a * b;
let quotient = a / b;
let remainder = a % b;
9. Characters:
Special characters called escape sequences are used to represent printable characters, such, as newline and tab.
- \n: Newline character.
- \t: Tab character.
- \”: Double quote character.
- \’: Single quote character.
- \\: Backslash character.
#include <iostream>
int main() {
printf("Hello, world!\nThis is a new line.\n");
printf("Tabbed \t text.\n");
printf("Double quote: \"\n");
printf("Single quote: \'\n");
printf("Backslash: \\n");
return 0;
}
#include <stdio.h>
int main() {
printf("Hello, world!\nThis is a new line.\n");
printf("Tabbed\ttext.\n");
printf("Double quote: \"\n");
printf("Single quote: \'\n");
printf("Backslash: \\n");
return 0;
}
public class Main {
public static void main(String[] args) {
System.out.println("Hello, world!\nThis is a new line.\n");
System.out.println("Tabbed\ttext.\n");
System.out.println("Double quote: \"\n");
System.out.println("Single quote: \'\n");
System.out.println("Backslash: \\n");
}
}
print("Hello, world!\nThis is a new line.")
print("Tabbed\ttext.\n")
print("Double quote: \"\n")
print("Single quote: \'\n")
print("Backslash: \\n")
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
#include <iostream>
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 < 5; i++) {
std::cout << "Element at index " << i << ": "
<< myArray[i] << std::endl;
}
return 0;
}
#include <stdio.h>
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 < 5; i++) {
printf("Element at index %d: %d\n", i, myArray[i]);
}
return 0;
}
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 < myArray.length; i++) {
System.out.println("Element at index " + i
+ ": " + myArray[i]);
}
}
}
# 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("Element at index {}: {}".format(i, my_array[i]))
// 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.
#include <iostream>
#include <string>
int main()
{
// String declaration and initialization
std::string myString = "Hello, w3wiki!";
// 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;
}
#include <stdio.h>
#include <string.h>
int main() {
// String declaration and initialization
char myString[] = "Hello, w3wiki!";
// 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;
}
public class StringExample {
public static void main(String[] args)
{
// String declaration and initialization
String myString = "Hello, w3wiki!";
// 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));
}
}
# String declaration and initialization
my_string = "Hello, w3wiki!"
# 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])
using System;
class StringExample {
static void Main()
{
// String declaration and initialization
string myString = "Hello, w3wiki!";
// 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]);
}
}
class StringExample {
static main() {
// String declaration and initialization
let myString = "Hello, w3wiki!";
// 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, w3wiki! 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).
#include <iostream>
// Function declaration
void greet() {
std::cout << "Hello, w3wiki!" << std::endl;
}
int main() {
// Calling the function
greet();
return 0;
}
#include<stdio.h>
// Function declaration
void greet() {
printf("Hello, w3wiki!\n");
}
int main() {
// Calling the function
greet();
return 0;
}
public class FunctionExample {
// Function declaration
static void greet() {
System.out.println("Hello, w3wiki!");
}
public static void main(String[] args) {
// Calling the function
greet();
}
}
# Function definition
def greet():
print("Hello, w3wiki!")
# Calling the function
greet()
// Function declaration
function greet() {
console.log("Hello, w3wiki!");
}
// Calling the function
greet();
Output
Hello, w3wiki!
Contact Us