# Thread: Similar to the Loop thread just posted(help please)

1. ## Similar to the Loop thread just posted(help please)

Hey guys, I am new to programming, and for the sake of learning how to create loops constructively I have tried to make a very simple calculator program (below) however I am having issues making it loop or 'restart'.

I read the Loop thread posted 2hours ago, however I didnt get any resolve so hence the new thread

I can create 'for'/'while' and 'do while' loops but not with this switch statement inside (the program will just crash(compiles fine))

My code is ment to;
1. Ask for a input(number)
2. Ask for a 2nd
3. ask for +, -, or *
4. Supply answer and restart
*It works fine without the for loop.
*This is just to understand loops more practically

Code:
```#include <stdio.h>

int main(int argc, char *argv[]){

int numb1;
int numb2;
int sum;
int choice;
int restart;
int restart1 = 1;

for (restart = 0; restart > restart1; NULL) {

printf("Enter a number:\n");
scanf("%d", &numb1);

printf("Enter a second number:\n");
scanf("%d", &numb2);

printf("Press 1 for Adding the two numbers\n");
printf("Press 2 for Subtraction\n");
printf("Press 3 for Multiplication\n");
scanf("%d", &choice);

switch (choice) {
case 1:
sum = numb1 + numb2;
printf("The result is %d\n", sum);
break;

case 2:
sum = numb1 - numb2;
printf("The result is %d\n", sum);
break;
case 3:
sum = numb1 * numb2;
printf("The result is %d\n", sum);
break;
default:
printf("You did enter a valid choice\n");
break;
}
}
}```
Thanks

2. First of all, you don't need NULL in your for loop - You can leave that part blank

Second, you the loop will keep going until restart is smaller than restart1 -> But they never change values

3. What you should probably do is add a do/while list

i.e.
Code:
```do
{
...
printf("Or push '4' to exit...");
...

}
while (choice != 4);```

4. I have no idea what other thread you are referring to.

In any event, your code with the loop works perfectly, assuming your intent is that the body of the loop is never executed.
Code:
```    int restart;
int restart1 = 1;

for (restart = 0; restart > restart1; NULL) {```
The first action of the loop is to set restart to zero. Then it checks if restart is greater than restart1 (i.e. if 0 > 1). That condition is false, so the loop body is never executed.

The names of the variables (restart and restart1) may mean something in your mind. But they mean nothing to the compiler. All it does is look at their values (0 and 1 in your code). A compiler does not employ any logic like "a variable is named restart, so something must be restarted".

5. Originally Posted by grumpy
I have no idea what other thread you are referring to.

In any event, your code with the loop works perfectly, assuming your intent is that the body of the loop is never executed.
Code:
```    int restart;
int restart1 = 1;

for (restart = 0; restart > restart1; NULL) {```
The first action of the loop is to set restart to zero. Then it checks if restart is greater than restart1 (i.e. if 0 > 1). That condition is false, so the loop body is never executed.

The names of the variables (restart and restart1) may mean something in your mind. But they mean nothing to the compiler. All it does is look at their values (0 and 1 in your code). A compiler does not employ any logic like "a variable is named restart, so something must be restarted".
AH thanks, I had greater and less than mixed up, I am such an idiot. haha.

And Reference you're last statement of my naming variables; yeah I do understand that, its more for ease of coding in my head. Never the less thanks for the help.

Click_here thanks aswel, getting different options on creating loops is very helpful in my learning progress (Trying to self teach)

Popular pages Recent additions