Jump to content

Why doesn't my loop work?

Are Riff
Go to solution Solved by colonel_mortis,

A while loop means "at the start of each iteration of the loop, check whether the while statement evaluates to true. If it is, continue to loop, but if it isn't then stop looping and go to the next statement". Because you set retry to 0 (~= false) at the beginning of the loop, when it checks the value of retry at the start of the first pass, it is 0 ~= false, so it skips the loop. In the second variant of your code, you set it to 1 (~= true), so it runs the loop. When you want to exit the loop, you set it to 0 (~= false), so the next time it checks, it's falsy, so it skips the loop and moves onto the next code.

In C-like pseudocode, your original while loop would look like this

bool retry = false;loopstart:    if (retry == true) {        //do whatever was in your loop    }    goto loopstart

hopefully that makes it a bit more obvious that it will skip the execution of the loop.

I'm trying to do menu loop.

int retry = 0;	while (retry > 0)	{            char user_input = '0';	    cin >> user_input;	    switch (user_input)	    {		case '1':			rect_area();			break;                /*...*/		case'9':			++retry;			break;		default:			exit(0);	    }	}

1. What wrong? why didn't it work? it just exit without

But if change into these, it works.

	int retry = 1;	while (retry > 0)	{		char user_input = '0';		cin >> user_input;		switch (user_input)		{		case '1':			rect_area();			break;                /* code */		case'9':			retry = 0;			break;

How do you actually read the 'while' loop??

 

B) B) B)

Link to comment
Share on other sites

Link to post
Share on other sites

I'm trying to do menu loop.

int retry = 0;	while (retry > 0)	{            char user_input = '0';	    cin >> user_input;	    switch (user_input)	    {		case '1':			rect_area();			break;                /*...*/		case'9':			++retry;			break;		default:			exit(0);	    }	}

1. What wrong? why didn't it work? it just exit without

But if change into these, it works.

	int retry = 1;	while (retry > 0)	{		char user_input = '0';		cin >> user_input;		switch (user_input)		{		case '1':			rect_area();			break;                /* code */		case'9':			retry = 0;			break;

How do you actually read the 'while' loop??

What are you using to program? Does it give error codes to help debug?

GIGABYTE Z97MX-G516GB DDR3 | I5 4690k @ 4.4ghz | 1TB SSHD, 500GB HDD, 128GB SSD | GTX 1070 8GB | Corsair Graphite 230 | EVGA 650W | Hyper 212 EVO

 

Cinebench R15: 636(all cores), 127FPS

 

Link to comment
Share on other sites

Link to post
Share on other sites

int retry = 0;while (retry > 0)

"while" is like an "if" that keeps going

retry is not greater then zero, so the loop exits

Link to comment
Share on other sites

Link to post
Share on other sites

doesn't it need to add a value to "retry" so that it can have a value greater than zero?

 

but if its an infinite loop then I don't think its that important because its not like your counting cycles 

Current: R2600X@4.0GHz\\ Corsair Air 280x \\ RTX 2070 \\ 16GB DDR3 2666 \\ 1KW EVGA Supernova\\ Asus B450 TUF

Old Systems: A6 5200 APU -- A10 7800K + HD6670 -- FX 9370 + 2X R9 290 -- G3258 + R9 280 -- 4690K + RX480

Link to comment
Share on other sites

Link to post
Share on other sites

What are you using to program? Does it give error codes to help debug?

 

I'm using Visual Studio 2015 community.

While debugging, no error were produced, just went straight down past the loop to the end of the program.

 

This is my little practice program to calculate the area of the shape.

1. User choose the shape from menu 1 - 8.

2. User enter the value of 'height', 'radius', etc.

3. It will calculate using one of the functions such as this:

void rect_area(){	int h = 0;	int w = 0;		cout << "Print rectangle area" << endl;	cout << "Enter the height:" << endl;	cin >> h;	cout << "Enter the width:" << endl;	cin >> w;	int area = h * w;	cout << "The area is " << area << "." << endl;	main();}

4. It will return the total 'area' of the shape and return back to the main.

 

How can I do this in more efficient way?

The number of lines can easily increase porpotionally to the number of shapes.

Do I have to use a class to define the variable?

B) B) B)

Link to comment
Share on other sites

Link to post
Share on other sites

A while loop means "at the start of each iteration of the loop, check whether the while statement evaluates to true. If it is, continue to loop, but if it isn't then stop looping and go to the next statement". Because you set retry to 0 (~= false) at the beginning of the loop, when it checks the value of retry at the start of the first pass, it is 0 ~= false, so it skips the loop. In the second variant of your code, you set it to 1 (~= true), so it runs the loop. When you want to exit the loop, you set it to 0 (~= false), so the next time it checks, it's falsy, so it skips the loop and moves onto the next code.

In C-like pseudocode, your original while loop would look like this

bool retry = false;loopstart:    if (retry == true) {        //do whatever was in your loop    }    goto loopstart

hopefully that makes it a bit more obvious that it will skip the execution of the loop.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

A while loop means "at the start of each iteration of the loop, check whether the while statement evaluates to true. If it is, continue to loop, but if it isn't then stop looping and go to the next statement". Because you set retry to 0 (~= false) at the beginning of the loop, when it checks the value of retry at the start of the first pass, it is 0 ~= false, so it skips the loop. In the second variant of your code, you set it to 1 (~= true), so it runs the loop. When you want to exit the loop, you set it to 0 (~= false), so the next time it checks, it's falsy, so it skips the loop and moves onto the next code.

In C-like pseudocode, your original while loop would look like this

bool retry = false;loopstart:    if (retry == true) {        //do whatever was in your loop    }    goto loopstart

hopefully that makes it a bit more obvious that it will skip the execution of the loop.

Thanks, I still haven't learn about 'goto' though.

I actually tried 'bool retry = false' the first time, but since i've been reading loop wrong, then I change to 'int'

B) B) B)

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks, I still haven't learn about 'goto' though.

I actually tried 'bool retry = false' the first time, but since i've been reading loop wrong, then I change to 'int'

Gotos are bad, and shouldn't be used. I just put it in there because it clearer exactly what the code does. Please don't use that code as a reference for your own implementation - it's just code that I selected to explain how while loops work, and I don't think my use of goto would actually work (I think it's missing a semicolon).

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

if (retry == true)

is the same as

if (retry)

since if's evaluate whether the condition is true or false and only executes the body when the condition is true

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×