+ 8

#include <iostream> using namespace std; int main() { int a; for ( a = 10; a <= 100; a+=10){ cout << a << endl;

the output of the above program is 10 20 ..100 but it says...a= 10;a<=100(true);a=a+10(20) cout << a; a= 20...isn't it?? why the output is starting with 10??

6th May 2017, 2:32 AM
MUʜAMMED ɪʟʟYAS
MUʜAMMED ɪʟʟYAS - avatar
5 ответов
+ 2
For loop execution takes as follows: 1. Initialization of variable 2.The condition statements 3.If condition statements is true the code inside for will be executed. 4. After executing the code inside for loop at last the variable is incremented/decremented. So in your case after initializing (variable a=20) and checking condition(a<=100 which is true) it will execute code inside the for loop. so it prints 10 everytime you run the code. and then after printing 10 the variable a is incremented !!
6th May 2017, 4:35 AM
Ninad
Ninad - avatar
+ 6
output starts with 10 because that is the intial condition of the for loop
6th May 2017, 2:59 AM
jay
jay - avatar
+ 4
In a for loop, the increment section is typically performed last, so a=10 in the first pass through the interior of the loop. This might seem a bit counterintuitive to start (since that part is defined before the repeated code), but try rewriting this as a while, and move the a+=10 statement around among the other statements.
6th May 2017, 3:00 AM
Jim
Jim - avatar
+ 1
the given program will give the first value of a is 10 as it is the initial value of a that's why it (program )gives the output as 10
6th May 2017, 7:06 PM
Ujjwal Kashyap Singh
Ujjwal Kashyap Singh - avatar
+ 1
coz the need to start is first variable default to start so if that start from 20 it's mean 20 40 ... 100
10th May 2017, 6:25 AM
Lam Lam Loo
Lam Lam Loo - avatar