| |

VerySource

 Forgot password?
 Register
Search
View: 823|Reply: 6

How to use a loop to get rid of GOTO? Expert advice!

[Copy link]

2

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-3 20:40:01
| Show all posts |Read mode
#include <stdio.h>

void main ()
{
const int N = 4;
unsigned int s [N], d [N], e [N];

int p, q, r, i, k;

s [0] = s [1] = 1;
for (i = 2; i <N; ++ i)
{
s [i] = d [i] = i;
e [i] =-1;
}

loop:
{
q = 0;
for (i = 1; i <N; ++ i) printf ("% d", s [i]);
printf ("\n");
for (k = N-1; k> = 2;-k)
{
d [k] = d [k] + e [k];
p = d [k];
if (p == k)
e [k] =-1;
else if (p == 0)
{
e [k] = 1; ++ q;
}
else
{
p + = q; r = s [p]; s [p] = s [p + 1]; s [p + 1] = r;
Ranch
goto loop;
}
}
Ranch
Ranch
}
}
Reply

Use magic Report

0

Threads

17

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-1-3 22:09:02
| Show all posts
int main ()
{
const int N = 4;
int flag = 1;
unsigned int s [N], d [N], e [N];

int p, q, r, i, k;

s [0] = s [1] = 1;
for (i = 2; i <N; ++ i)
{
s [i] = d [i] = i;
e [i] =-1;
}

do

{
q = 0;
for (i = 1; i <N; ++ i) printf ("% d", s [i]);
printf ("\n");
for (k = N-1; k> = 2;-k)
{
d [k] = d [k] + e [k];
p = d [k];
if (p == k)
e [k] =-1;
else if (p == 0)
{
e [k] = 1; ++ q;
}
else
{
p + = q; r = s [p]; s [p] = s [p + 1]; s [p + 1] = r;
Ranch
break;
}
Ranch
}
if (k <2)
flag = 0;
Ranch
}
while (flag == 1);

return 0;
}
Reply

Use magic Report

0

Threads

19

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-1-4 02:54:01
| Show all posts
After reading the logic of the program, you can write it yourself.
Reply

Use magic Report

0

Threads

24

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-1-5 10:39:01
| Show all posts
goto is a do while
Reply

Use magic Report

0

Threads

24

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-1-5 11:00:01
| Show all posts
#include <stdio.h>

void main ()
{
const int N = 4;
unsigned int s [N], d [N], e [N];

int p, q, r, i, k;

s [0] = s [1] = 1;
for (i = 2; i <N; ++ i)
{
s [i] = d [i] = i;
e [i] =-1;
}

bool loop = false;

do {
q = 0;
lop = false;
for (i = 1; i <N; ++ i) printf ("% d", s [i]);
printf ("\n");
for (k = N-1; k> = 2;-k)
{
d [k] = d [k] + e [k];
p = d [k];
if (p == k)
e [k] =-1;
else if (p == 0)
{
e [k] = 1; ++ q;
}
else
{
p + = q; r = s [p]; s [p] = s [p + 1]; s [p + 1] = r;
 loop = true;
}
} while (loop);


}
}
Reply

Use magic Report

0

Threads

18

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-1-28 18:36:01
| Show all posts
What I do with FOR without a flag:
#include <stdio.h>

void main ()
{
const int N = 5;
unsigned int s [N], d [N], e [N];
Ranch
int p, q, r, i, k;
Ranch
s [0] = s [1] = 1;
for (i = 2; i <N; ++ i)
{
s [i] = d [i] = i; e [i] =-1;
}
for (;;)
{
q = 0;
for (i = 1; i <N; ++ i) printf ("% d", s [i]);
printf ("\n");
for (k = N-1; k> = 2;-k)
{
d [k] = d [k] + e [k];
p = d [k];
if (p == k)
e [k] =-1;
else if (p == 0)
{
e [k] = 1; ++ q;
}
else
{
p + = q; r = s [p]; s [p] = s [p + 1]; s [p + 1] = r;
break;
}
}
if (k <2)
break;
}
}
Reply

Use magic Report

0

Threads

15

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-2-1 14:36:02
| Show all posts
while (1)
{……; // using break; once meet the condition to stop the loop
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list