Let’s look at each error in you code.
Firstly
x=o;
Did you mean 0
( zero ) or a variable o
or the character 'o'
.
If it was 0
( zero ) or 'o'
( character ) or a variable ( having a value greater than or equal to 10 ) , then the loop will not run even once because of the condition
while (x < 10);
If o
was a variable ( and it’s value is less than 10 ), then it will be an infinite loop because you left a semicolon after the while loop.
If there were no semicolon after the while loop, then the loop would have executed until the condition x < 10
returns false.
Next
for (i= .2; i =3; i++)
cout << "i = ";
cout << i;
Firstly, is there supposed to be {}
here and maybe the actual code you meant was
for (i= .2; i =3; i++)
{
cout << "i = ";
cout << i;
}
If there are no braces {}
, then only the first line, that is cout << "i = ";
would be executed under the for loop ( it would be an infinite loop unless you change the for loop condition ) .
Also, did you mean .2
or 2
( they are different )
Next, if i
was a float ( or int ), then this would be an infinite loop because the condition in your for loop is i = 3
. That assigns the value 3
to i
and it will return true, and hence, the loop will be an infinite loop.
If the condition was changed to something like i == 3
( it will iterate 0 times ) or i < 3
( it will iterate 1 time ) , then it will work.
If i
was an int, then all the values after the decimal will be skipped and initially, i
will have the value 0
and not 0.2
, but it would still be an infinite loop unless you change the conditions as mentioned before.
But, if you change it to i < 3
, then it will iterate 3
times ( because i
will get the values 0
,1
, and 2
)
Well, these are the errors in your code. Now, the code may differ depending on your original intention, but here are some examples
int x = 0;
while (x < 10)
x++;
Resultx
will have the value 10
after the loop ends.
Next code
int i;
for ( i = 2 ; i < 3 ; i++ )
{
cout << "i = ";
cout << i;
}
OUTPUT
i = 2
solved What is the error in this code please help: