This question already has answers here:
What does "for(;;)" mean?
(5 answers)
Closed 4 years ago.
I am looking at some example codes and I saw someone did this
for (;;) {
// ...
}
Is that equivalent to while(1) { } ?
And what does while(condition); do? I don't get the reason behind putting ';' instead of {}
yes,
for(;;){}
is an infinite loop
And what does while(condition); do? I don't get the reason behind putting ';' instead of {}
Well, your question is what happens if you put or you do not put a semicolon after that while condition? The computer identifies the semicolon as an empty statement.
Try this:
#include<stdio.h>
int main(void){
int a = 5, b = 10;
if (a < b){
printf("True");
}
while (a < b); /* infinite loop */
printf("This print will never execute\n");
return 0;
}
for(;;) and while(1) are both infinite loops, and compile to the same opcodes:
L2:
jmp L2
Which means there is no speed difference, as the disassembly is exactly the same.
while just loops though a single statement until the condition is false. It doesn't have to be a compound statement (this thing: {}), it can be any statement. ; is a statement that does nothing.
while(getchar() != '\n');
will loop until you hit enter, for example. Though, this is bad practice since it will hog the thread; adding a call to a sleep method in the loop is better.
Related
This question already has answers here:
Is there ever a need for a "do {...} while ( )" loop?
(19 answers)
Closed 1 year ago.
For and while loop can be used anywhere in replace of do-while then why the C language has do-while loop, what is it's real use .
#include <stdio.h>
int main()
{
do{
statements;
}
while(condition);
}
return 0;
The main use of using a do while loop
is that even if the condition that you pass is false it will execute the loop at least once.
While Loop: First condition then execute the code
Do While Loop: First execute the code then condition
This question already has answers here:
What does the comma operator , do?
(8 answers)
Double For Loop syntax in C
(3 answers)
Closed 5 years ago.
I want to know how for loop is processed in below condition.
void main()
{
int i,j;
For(i=1,j=1;i<=5,j<=10,i++,j++)
{
printf("%d%d",i,j);
}
}
sorry for typo mistake I correct my syntax here
For(i=1,j=1;i<=5,j<=10;i++,j++)
answer of this -1122334455667788991010
How's that possible because loop for I will be iterate for only 5 times how's that possible ?
I want to know how loop will be executed ?
This won't compile, there's only one ; in the for which is a syntax error.
I'll assume it should read like this:
for(i=1, j=1; i<=5, j<=10; i++, j++)
then it would step both i and j to 10.
This is because the for-loop's middle part, the condition, reads i<=5,j<=10 which is a use of the comma operator where perhaps a boolean and (&&) would be better.
It will evaluate i<=5, throw away that result, and then evaluate j<=10, running the loop for as long as that value is non-zero.
#include <stdio.h>
int main(int argc, char** args){
for(int i = 0, j=0; i<10&&j<10; i++, j++){
printf("%d, %d\n", i, j);
}
}
The semi colon's seperate the terms of the for statement. (intializer; condition; action at end of loop) You can do what you like for the sections.
This question already has answers here:
Why can't I use a "break" statement inside a ternary conditional statement in C++?
(2 answers)
Closed 7 years ago.
just wondering if, for the following code lines, it is possible to replace using a ternary operator:
if( current->chbits[i] != '\0')
printf("%c\n",current->chbits[i]);
else
break;
If so, how would I parse it correctly?
You can't have break in a ternary operator.
See this for more information.
The ternary operator is used to evaluate an expresson conditionally:
result = condition ? first : second;
In your example is no conditional expression but an conditional statement.
Yes, you can replace the code with ternary operator by making some modifications to eliminate the break statement.
for(int i=0, int flag=1 ;flag!=0; i++){
flag=(current->chbits[i]!='\0') ? printf("%c\n",current->chbits[i]) : 0;
}
Here flag won't be zero since printf() returns the number of characters successfully written on the output. When current->chbits[i] != '\0' becomes false flag is set to 0 and for terminates as per the condition flag!=0.
This question already has an answer here:
What does the following for loop syntax mean in C?
(1 answer)
Closed 8 years ago.
For example,
for (;;)
{
//do something
}
How is this different from
{
//do something
}
It's an infinite loop. Pretty much the same as writing
while (true)
{
// do something
}
This is used as an infinite loop. It is equivalent to while(1) { ... }.
It is equivalent to while(true).
A for-loop has three elements:
initializer
condition (or termination expression)
increment expression
Since this doesn't set any of them, it continues to run.
In C,
for (;;){
//do something
}
is the equivalent of
while(1){
//do something
}
(Similarly, in other languages:)
while(true){
//do something
}
This question already has answers here:
Does `break` work only for `for`, `while`, `do-while`, `switch' and for `if` statements?
(6 answers)
Closed 9 years ago.
If i write break statement in for loop then will variable be updated and then for loop exits,or after just excecution of break statement for loop exits?
for e.g
for(i=0;i<100;i++){
//do something something
if(i==50){
break;
}
what will be the value of i after for loop exit?
When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop.It can be used to terminate a case in the switch statement
The value will be 50.
The for loop can be described in general terms like this:
for(INIT; CONDITION; UPDATE)
BODY
and it can be replaced with the equivalent while loop, like this:
INIT
while(CONDITION)
{
BODY
UPDATE
}
So, since your break is in the BODY, the UPDATE is not run, and the value 50 remains.
If loop ready to exit by the break statement , that mean the i value should be equal to the condition .
if(i==50);
i Will be 50
...