easy
0 views

While Loop in different programming languages

Compare and understand while loop syntax and behavior across C, C++, Java, Python, JavaScript, PHP, and Go programming languages

Understand the Problem

Problem Statement

Learn and compare how while loops are implemented across different programming languages. This tutorial demonstrates the syntax and basic usage of while loops in C, C++, Java, Python, JavaScript, PHP, and Go programming languages.

Each language has its own syntax for the while loop construct, but the fundamental logic remains the same: execute a block of code repeatedly while a condition remains true.

Your task is to understand the syntax differences and similarities between these implementations.

Constraints

  • All code examples should be syntactically correct for their respective languages
  • The while loop condition should be a boolean expression
  • The loop body should contain statements to be executed repeatedly
  • There must be a way to eventually make the condition false to avoid infinite loops
  • Variable initialization and increment/decrement should be properly handled

Examples

Example 1
Input
// C example
int i = 0;
while (i < 5) {
    printf("Value is %d\n", i);
    i++;
}
Output
Value is 0
Value is 1
Value is 2
Value is 3
Value is 4
Explanation

The C while loop initializes i to 0, checks if i < 5, prints the current value, then increments i. This continues until i reaches 5, at which point the condition becomes false and the loop exits.

Example 2
Input
# Python example
i = 1
while i < 5:
    print(i)
    i += 1
Output
1
2
3
4
Explanation

Python uses indentation to define the loop body. The loop starts with i=1, prints the value, then uses the += operator to increment i. The loop continues until i reaches 5.

Example 3
Input
// JavaScript example
let i = 0;
while (i < 5) {
    console.log(i);
    i++;
}
Output
0
1
2
3
4
Explanation

JavaScript uses let/const for variable declaration. The loop follows the same pattern as C/C++ with braces for block structure and console.log for output to the browser console or Node.js terminal.

Solution

#include <stdio.h>

int main() {
    int i = 0;
    
    printf("C while loop example:\n");
    while (i < 5) {
        printf("Value is %d\n", i);
        i++;
    }
    
    return 0;
}
Time:O(n) - where n is the number of iterations (5 in this case)
Space:O(1) - uses only a constant amount of memory for the loop variable
Approach:

This C implementation demonstrates the basic while loop structure:

  • Variable Declaration: int i = 0; declares and initializes an integer variable
  • Loop Condition: while (i < 5) checks if i is less than 5 before each iteration
  • Loop Body: The code inside braces executes repeatedly while the condition is true
  • Output: printf("Value is %d\n", i); prints the current value of i
  • Increment: i++; increments i by 1 after each iteration

The loop terminates when i becomes 5, making the condition false.

Visual Explanation

Loading diagram...