Cycles

0
(0)

C

C++

C#

Common Lisp

Dart

F#

Go

Java

JavaScript

Kotlin

Python

Rust

 

C

#include <stdio.h>

int main(void)
{
    // for loop
    for(int i = 1; i < 6; i++)
    {
        printf("%d", i);
    }

    // while loop
    int n = 1;
    while(n < 6)
    {
        printf("%d", n);
        n++;
    }

    // do-while loop
    int k = 1;
    do
    {
        printf("%d", k);
        k++;
    }
    while(k < 6);
}

C++

#include <iostream>

int main()
{
    // for loop
    for(int i{1}; i < 6; i++)
    {
        std::cout << i << std::endl;
    }

    // sequence iteration
    for(char c : "Hello")
    {
        std::cout << c << std::endl;
    }

    // while loop
    int n{1};
    while(n < 6)
    {
        std::cout << n << std::endl;
        n++;
    }

    // do-while loop
    int k{1};
    do
    {
        std::cout << k << std::endl;
        k++;
    }
    while(k < 6);
}

C#

// for loop
for(int i = 1; i < 6; i++)
{
    Console.WriteLine(i);
}

// while loop
int n = 1;
while(n < 6)
{
    Console.WriteLine(n);
    n++;
}

// do-while loop
int k = 1;
do
{
    Console.WriteLine(k);
    k++;
}
while(k < 6);

// sequence iteration
foreach(var c in "12345")
{
    Console.WriteLine(c);
}

Common Lisp

; analogous to a for loop, i is initialized to 0
(dotimes (i 6) (print i))

; analogous to a while loop
(do ((n 1 (+ n 1)))
    ((= n 6))
    (format t "~a~%" n))

; analogous to do-while/while
(let ((k 1))
    (loop
        (format t "~a~%" k)
        (setf k (+ k 1))
        (when (> k 5)
            (return))))

; list iteration
(dolist (n (list 1 2 3 4 5)) (print n))

Returning a value from a loop:

Lisp

(format t "Return value: ~a~%"
    (do ((n 1 (+ n 1)))
        ((= n 6) n)
        (format t "~a~%" n)))

Dart

void main() {
     
    // for loop
    for(int i=1; i <6; i++)
    {
        print(i);
    }

    // sequence iteration
    for(var c in [1, 2, 3, 4, 5])
    {
        print(c);
    }

    // while loop
    int n = 1;
    while(n < 6)
    {
        print(n);
        n += 1;
    }

    // do-while loop
    int k = 1;
    do
    {
        print(k);
        k++;
    }
    while(k < 6);
}

F#

// for loop
for i = 1 to 5 do 
    printfn $"{i}"

// while loop
let mutable n = 1;
while n < 6 do
    printfn $"{n}"
    n <- n + 1

// sequence iteration
for n in 1..5 do 
    printfn $"{n}"

Go

package main
import "fmt"
 
func main() {
    
    // for loop
    for i := 1; i < 10; i++ {
        fmt.Println(i);
    }

    // sequence iteration (array)
    var nums = [5]int{1, 2, 3, 4, 5}
    for _, value := range nums{
        fmt.Println(value)
    }
}

Java

class Program {

    public static void main(String[] args) {  

        // for loop
        for(int i = 1; i < 6; i++){
            System.out.println(i);
        }

        // while loop
        int n = 1;
        while(n < 6){
            System.out.println(n);
            n++;
        }

        // do-while loop
        int k = 1;
        do{
            System.out.println(k);
            k++;
        }
        while(k < 6);

        // sequence iteration
        for(var c : new int[] { 1, 2, 3, 4, 5 }){
            System.out.println(c);
        }
    }
}

JavaScript

// for loop
for(let i = 0; i < 6; i++){
    console.log(i);
}

// sequence iteration
for(let c of [1, 2, 3, 4, 5]){
    console.log(c);
}

// object property iteration
const person = {name: "Tom", age: 39};
for(let prop in person){
    console.log(prop, person[prop]);
}

// while loop
let n = 1;
while(n < 6){
    console.log(n);
    n += 1;
}

// do-while loop
let k = 1;
do{
    console.log(k);
    k++;
}
while(k < 6);

Kotlin

fun main() {
    // sequence iteration
    for(i in 1..5)
    {
        println(i);
    }

    // while loop
    var n = 1;
    while(n < 6)
    {
        println(n);
        n += 1;
    }

    // do-while loop
    var k = 1;
    do
    {
        println(k);
        k++;
    }
    while(k < 6);
}

Python

# for loop
for i in range(1, 6): 
    print(i)

# while loop
n = 1
while n < 6:
    print(n)
    n += 1 

Rust

fn main(){
  
    // sequence iteration
    for i in 1..6  {
        print!("{} ", i);
    }

    println!("");

    // while loop
    let mut n = 1;
    while n < 6 {
        print!("{} ", n);
        n += 1;
    }
    println!("");

    // loop construct
    let mut k = 1;
    loop {
        print!("{} ", k);
        k += 1;
        if k == 6 { break; }
    }
}

Next

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?


Explore More IT Terms


Share this term: Facebook X LinkedIn WhatsApp Email
CONTINUE LEARNING Next: Defining Aliases →

Leave a Reply

Your email address will not be published. Required fields are marked *

Go beyond simple links with powerful features designed for creators.