It has been many days since I posted any article and was thinking to continuing this series. So here it is. I discussed about Variables and Primitive types in my previous article. If you haven't already read it do check it out here. In this article we will discuss about keywords and loops in different programming languages.
So, let us begin.
Keywords
Before going into keywords of different programming languages, what are keywords? To put in simple terms
They are reserved keywords in any programming language which are used by compiler, interpreters or transpilers to determine what operation to perform.
Example of most common keywords are for
, const
, var
, let
, while
are few among many.
All programming languages have some common way of declaring variables. Most of them uses common keywords like int
, char
, double
, float
and also variable modifiers like signed
, unsigned
etc. More explanation about them in my previous article. Even for declaring constants they use common keyword const
.
Also they use similar keywords for loops and might differ in function declaration and user defined type declaration for which I will have dedicated sections.
There is not much to talk about keywords as a common as when you will work with a programming language you easily come to know about them.
Lets move to loops:
Loops
Loops are sequence of code that repeats a task a given number of time until and unless exit/break conditions are met.
Taking a very very very very basic example of printing your name 10 times, you can do it with print (function or operator that outputs to stdout) statement of a given language or you can use loops. Here I will do the same as an example to printing hashnode name 10 times in different languages and show similarity between them.
For a basic loop most languages have common for loop syntax
For loop
These loops are used when you know how many times your loop will run
// common syntax
for (initialization; condition; increment/decrement) {
// body
}
C/C++/Java
for (int a = 0; i < 10; i++) {
printf("%s", "Hashnode"); // <-- for C
std::cout << "Hashnode"; // <-- for C++
System.out.println("Hashnode"); // <-- Java
}
Python
for x in range(10):
print("Hashnode") # <-- python 3 syntax
Javascript/Typescript/Dart
for (var i = 0; i < 10; i++) {
console.log("Hashnode"); // <-- for Javascript and Typescript
print("Hashnode"); // <-- for Dart
}
Typescript with Type def
for (i: int = 0; i < 10; i++) {
console.log("Hashnode");
}
Kotlin
for (x in 0..10) {
println("Hashnode")
}
Golang
for i: = 0; i < 10; i++ {
fmt.Println("Hashnode")
}
Rust/Swift
for x in 0..10 {
println!("Hashnode"); // <-- for Rust
print("Hashnode") // <-- for Swift
}
While loop
These loops are used when you don't know before hand how many times your loop will run
// common syntax
while (condition) {
// body
}
C/C++/Java/Kotlin/Javascript/Typescript/Dart/Rust/Swift
int i = 0; // <-- declaration depends on language
while (i < 10) {
printf("%s", "Hashnode"); // <-- for C
std::cout << "Hashnode"; // <-- for C++
System.out.println("Hashnode"); // <-- Java
console.log("Hashnode"); // <-- for Javascript/Typescript
print("Hashnode"); // <-- for Dart
println("Hashnode"); // <-- for Kotlin
println!("Hashnode"); // <-- for Rust
print("Hashnode") // <-- for Swift
i++;
}
Python
i = 0
while i < 10:
print("Hashnode") # <-- python 3 syntax
i += 1
Golang
i := 0
for i < 10 {
fmt.Println("Hashnode")
i++
}
Do while
do-while
statements works in same way as while loops with a difference that it will execute once and then check for condition, thats the reason why condition is written at end.
These types of loop is used where you know that one time loop will surely run but after that it needs to check condition to know it should continue or exit.
// common syntax
do/repeat/loop { // <-- different keywords
// body
} while/until (condition) // <-- different keywords
C/C++/Java/Kotlin/Javascript/Typescript/Dart
int i = 0 // <-- declaration depends on language
do {
printf("%s", "Hashnode"); // <-- for C
std::cout << "Hashnode"; // <-- for C++
System.out.println("Hashnode"); // <-- for Java
println("Hashnode") // <-- for Kotlin
console.log("Hashnode"); // <-- Javascript/Typescript
print("Hashnode");
i++
} while(i < 10);
Python, Golang, Rust and Swift don't have do-while loops in language but you can built on like as follows:
Python
i = 0
while True:
print("Hashnode")
i += 1
if i > 9:
break
Golang
i := 0
for ok := true; ok; ok = (i < 10) {
fmt.Println("Hashnode")
i++
}
// or
i := 0
for {
fmt.Println("Hashnode")
i++
if !(i < 10) {
break
}
}
Rust
let i = 0
loop {
println!("Hashnode");
i++;
if (i > 9) break;
}
Swift
var i = 0
repeat {
print("Hashnode")
i++
} while (i < 10)
So as you can see there are many similarities between different programming languages in looping where most of them share common syntax in while
and do-while
while some have different kind of declaring do-while where others do even have do-while loop but you can easily build them. for
loops are also similar in most of the languages with some sugar syntax above them.
Funtional languages
When it comes to Functional programming languages syntax is different and mainly loop are concept of Procedural/Imperative programming languages while Functional programming languages they have recursion.
Haskell
printStringNTimes 0 = return ()
printStringNTimes n =
do
putStrLn "Hashnode"
printStringNTimes (n-1)
main = printStringNTimes 10
Elixir
Enum.each(0..10, fn(x) ->
IO.puts "Hashnode"
end)
Explaining funtional languages will need its own article as its different in concept and in syntax will surely conver functional language in future article.
Next we will look at conditionals.
Hope you like the article.
Happy learning, Happy coding!