While Loop

#include <iostream>
using namespace std;

int main ()
{
int x =1;
int c = 3;
while (c-- >=0)
{
x*=2;
}
x++;

cout << ++x << endl;
}//Ans is 18.

------------------------------

#include <iostream>
using namespace std;

int main ()
{
int a = 0;
int b = 0;
while (++b < 4)
{
cout << a++;
}
cout << b << endl;
}//Ans is 0124.

--Pls advise on how do I get the answers?

Pls use "insert code tag (#)" at notation.




#include <iostream>
using namespace std;

1
2
3
4
5
6
7
8
9
10
11
12
int main ()
{
int x =1;
int c = 3;
while (c-- >=0)
{
x*=2;
}
x++;

cout << ++x << endl;
}//Ans is 18. 


Your first program code causes the followings:


before 1. iteration:
int x =1;
int c = 3;

1. iteration:
c = 3
x = 2

2. iteration:
c = 2
x = 4

3. iteration:
c = 1
x = 8

3. iteration:
c = 0
x = 16

9th line increase the x:
x = 17

11th line firstly increases the x then write out the screen.
so x becomes value 18
Last edited on
Tks screw!
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main ()
{
int a = 0;
int b = 0;
while (++b < 4)
{
cout << a++;
}
cout << b << endl;
}//Ans is 0124. 


1. iteration:
b = 1
a = 0

2. iteration:
b = 2
a = 1

3. iteration:
b = 3
a = 2

4. iteration:
b = 4

Because the condition isn't satisfied so the body of loop isn't executed.

Not at all.
Topic archived. No new replies allowed.