While is NOT a replacement for SWITCH -- CASE
However , still if this is the requirement then , you can do this :
While (1)
{
if (case1 ) {}
if (case2 ) {}
:
:
:
if (case n ) {}
if (case default ) {}
} //end of while loop
A "while" loop is appropriate in this case - one in which the condition is evaluated at the beginning of the loop.
If you want to execute a statement which is in while loop at least one time you can use do- while loop. this is why because initially first statements will be executed then condition will be checked. but in case of while first condition will be check then statements will be executed
A while loop repeats until the condition becomes false, and may never execute: int a = 4; while (a > 5) { //Do something } or int a = 4; while (a > 0) { //Do something a--; }
It's best to use a for loop.For example, if you want 10 iterations:for (int i = 1; i
A Do-While loop looks like this: do { loop body } while (condition); and a While loop looks like this: while (condition) { loop body } The main difference is that the loop body is always run once in the Do-While loop, then the condition is checked to see if the loop should keep running. In a While loop, the condition is checked first, and it will not run the loop body at all if the condition is false.
UNIX has no bearing on the C language; it is cross-platform. There is no select/case in C, you probably meant switch/case. However, a switch/case is a conditional jump while a nested loop is a loop within a loop. Besides the C language they have nothing in common with each other.
A "while" loop is appropriate in this case - one in which the condition is evaluated at the beginning of the loop.
Yes. while loop consist of only condition statement to make for loop look as while loop we can use syntax shown below: for(;condition;) eg: for(;i<=n;)
Both for and while belongs to repetitive controls structure. for is used when a user does not know how manytimes the looping needs to be executed i.e. infinite in some cases.Also in case of for initialization,condition and step decrement/ increment can take place in a single loop. In viceversa case while is used. In case of while we first check the condition if it is satisfied , only then execute the statements present below the loop. Both for and while belongs to repetitive controls structure. for is used when a user does not know how manytimes the looping needs to be executed i.e. infinite in some cases.Also in case of for initialization,condition and step decrement/ increment can take place in a single loop. In viceversa case while is used. In case of while we first check the condition if it is satisfied , only then execute the statements present below the loop.
Yes, it's easy:Old: while (expression)statementNew: for (; expression;)statement
The most important differences are: a. The while loop starts with a condition whereas the condition is the line of code in case of a do while loop b. The do while loop is guaranteed to run the loop body atleast once even if the condition is an impossible to satisfy but such a guarantee is not available with the normal while loop
The For loops and While loops are similar in functionality aspects. They perform a specified set of instructions repeatedly until a certain condition is met. The only difference being, the loop variable modification happens in the for loop declaration whereas in case of a while loop, we must include it in the block of code within the loop. Ex: For Loop: for(int i=0; i<10;i++){ ... } While Loop: int i = 0; while(i < 10){ i++ ... } If you see the code the variable initialization, modification happens in the same line for a For loop whereas in case of a while loop we do it in separate lines of code.
If you want to execute a statement which is in while loop at least one time you can use do- while loop. this is why because initially first statements will be executed then condition will be checked. but in case of while first condition will be check then statements will be executed
A nested loop is a (inner) loop that appears in the loop body of another (outer) loop. The inner or outer loop can be any type: while, do while, or for. For example, the inner loop can be a while loop while an outer loop can be a for loop.
They both loop
A while loop repeats until the condition becomes false, and may never execute: int a = 4; while (a > 5) { //Do something } or int a = 4; while (a > 0) { //Do something a--; }
It's best to use a for loop.For example, if you want 10 iterations:for (int i = 1; i