Monday, December 19, 2022

For and while loops in java, python 3, c++ and javascript

 Here is how you can write a for loop and a while loop in each of the languages you mentioned:

Java:

For loop:

for (int i = 0; i < 10; i++) { 
    System.out.println(i); 
}

While loop:

int i = 0
while (i < 10) { 
    System.out.println(i); 
    i++; 
}

Python 3:

For loop:

for i in range(10): 
    print(i)

While loop:

i = 0 
while i < 10
    print(i) 
    i += 1

C++:

For loop:

for (int i = 0; i < 10; i++) { 
    std::cout << i << std::endl
}

While loop:

int i = 0
while (i < 10) { 
    std::cout << i << std::endl
    i++; 
}

JavaScript:

For loop:

for (let i = 0; i < 10; i++) 
    console.log(i); 
}

While loop:

let i = 0
while (i < 10) { 
    console.log(i); 
    i++; 
}

I hope this helps! Let me know if you have any questions.

No comments:

Post a Comment

meta.ai impression

Meta.ai is released by meta yesterday, it is super fast you can generate image while typing! You can ask meta.ai to draw a cat with curvy fu...