Looping Questions

here are some question i don't understand please some one explain to me. Im new to C++ im sorry. Thank you for your help.

1. What is the output of the following program segment? And what are the values of i and n after execution?



int i = 2;

int n = 18;

while (i < n)

{

if ((i % 2) == 0)

i++;

else

n--;

}

cout << i << endl;





2. What is the output of the following program segment? And what are the values of i and n after execution?



int i = 1;

int n = 10;

while (i < n)

{

if ((i % 2) != 0)

i++;

}

cout << i << endl;





3. What is the output of the following program segment? And what are the values of i and j after execution?



int i = 1;

int j = 3;

while (i < 15 && j < 20)

{

i++;

j += 2;

}

cout << (i + j) << endl;





4. What is the output of the following program segment? And what is the value of x after execution?



int x = 10;

while (x > 0)

{

cout << x << endl;

x -= 3;

}








5.What is the output of the following program segment? And what are the values of i and n after execution?



int i = 1;

int n = 0;

while (i <= n)

{

cout << i;

i++;

}


I don't understand how can you tell what are the values of those listed aboved and how you can tell the output of each of those questions.
Sounds suspiciously like exam questions to me.
Is it too hard to write those snips and test them?
The worst thing that could happen is an infinite loop.
For the first problem, the whole code should be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int main(){
int i = 2;

int n = 18;

while (i < n)

{

if ((i % 2) == 0)

i++;

else

n--;

}

cout << i << endl;
cout << n << endl;

return 0;
}


Compile and run that, it's going to give you 3 and 3 for i and n. just copy and paste each snippet, not hard.

also,
http://www.cplusplus.com/doc/tutorial/control/
helped me a lot.
Last edited on
I pasted it and nothing shows up on my screen the program run perfectly but it exit the display black screen and show no answer. If it work in the first place i wouldn't have a problem in the first place. And no this is not an exam question it is a worksheet fyi mr. Browni3141. I use the Microsoft Visual Studio 2005 because i don't know y the teacher want that, newer version is so much better smh.
Last edited on
Okay, if its not an exam/quiz then you should be able to test them with your compiler.
You can use cin.get() to pause the program.
@demonkyo3

If it work in the first place i wouldn't have a problem in the first place


If you knew the basics of the language you wouldn't have to ask.

I know that sounds mean, but it's the truth. If you want to learn the basics you can read the tutorial here or find a different one, not too hard just takes time. Then when you want to learn the language a little more go out and find some books...

If this sounds mean I'm not trying to make it that way, I just want to be frank with you.

Also the above quote dosn't make a whole lot of grammatical sense.
Last edited on
Topic archived. No new replies allowed.