The program is an example ofinfinite while loop. Since the value of the variable var is same (there is no ++ or – operator used on this variable, inside the body of loop) the condition var<=2 will be true forever and the loop would never terminate. Examples of infinite while loop Ex...
A while loop in C++ is an example of an entry-controlled loop wherein the condition is checked at the entry of the loop. The loop runs until the condition is true, and the statements/ block of code inside the body of the loop are executed. The loop terminates as soon as the ...
In this example, when the value of “i” is 5, the “goto” statement is encountered. It transfers the control of the program to the label named “skip”. As a result, the remaining statements within the current iteration of the loop are skipped, and the program directly proceeds to the...
Example 1: while loop // Print numbers from 1 to 5 #include <stdio.h> int main() { int i = 1; while (i <= 5) { printf("%d\n", i); ++i; } return 0; } Run Code Output 1 2 3 4 5 Here, we have initialized i to 1. When i = 1, the test expression i <= 5 ...
Example: C# while Loop Copy int i = 0; // initialization while (i < 10) // condition { Console.WriteLine("i = {0}", i); i++; // increment } Try it Output:i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8 i = 9 ...
C# While LoopThe while loop loops through a block of code as long as a specified condition is True:SyntaxGet your own C# Server while (condition) { // code block to be executed } In the example below, the code in the loop will run, over and over again, as long as a variable (i...
Example: For loop in C Compiler // Program to print numbers from 1 to 10 #include <stdio.h> int main() { int i; for (i = 0; i < 10; i++) { printf("%d\n", i+1); } return 0; } Run Code >> The above code prints the numbers from 1 to 10 using a for loop in...
Example of while Loop in CThe following program prints the "Hello World" message five times.Open Compiler #include <stdio.h> int main(){ // local variable definition int a = 1; // while loop execution while(a <= 5){ printf("Hello World \n"); a++; } printf("End of loop"); ...
Case5 (Blank Loop): This example shows that loop can execute even if there is no statement in the block for execution on each iteration. Case6 (Multiple variables and conditions): Variables ‘i’ and ‘k’ are initialized to 0; condition is to execute loop when ‘i’ is lesser than ‘...
In Ruby, the do...while loop is implemented with the help of the following syntax:loop do # code block to be executed break if Boolean_Expression #use of break statement end Example 1: Check Armstrong Number Using do-while Loop=begin Ruby program to check whether the given number is ...