Switch Argument Outside While Loop Execution Explained

  • Thread starter Crystal037
  • Start date
  • Tags
    Switch
In summary: If not, and you need more help, please respond to the questions and suggestions that have been posted here. Otherwise I'm going to assume you don't need any more help.In summary, the code provided has multiple issues including missing break statements in the switch cases, incorrect placement of the switch statement, and an uninitialized pointer that causes the program to produce unexpected results. These issues need to be addressed in order for the program to function properly.
  • #1
Crystal037
167
7
TL;DR Summary
switch in c programming inside a while but argument of switch is outside the while
C:
#include<stdio.h>
#define MAX_SIZE 6
int pop(int a[]);
void push(int a[],int e);
int pal(int a[]);
int *top;
int stack[MAX_SIZE];
int main(){
    int choice,done=0,ele;
    printf("1.Push\t2.Pop\t3.Pal\t4.Exit\n");
    printf("Enter your choice:\t");
    scanf("%d",&choice);
    while(!done){
        switch(choice){
            case 1:printf("\nEnter the element to be pushed\n");
            scanf("%d",&ele);
            push(stack,ele);
           
            case 2: if(pop(stack)==-1)
            printf("\nStack is empty\n");
            else{
             printf("\nDeleted Element=%d",pop(stack));
                }
           
            case 3:done=1;
          
            default:printf("\nInvalid Choice\n");
        }
    }
}
void push(int a[],int e){
    if(*top==MAX_SIZE-1){
        printf("\nStack Overflow\n");
    }
    else{
        a[++*top]=e;
    }
}
int pop(int a[]){
    if(*top==-1){
        printf("\nStack Underflow\n");
        return -1;
    }
    else{
        return a[*top--];
    }
}
Here switch argument is taken outside while loop so the argument of switch should remain constant and and that case should be in a loop then why is it executing only 1 time
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
because the case statements should end with a break:

switch in C:
switch (choice) {
    case 1:
        // do something
        break;
    case 2:
        // do something else
        break;
    default:
        // do something defaulty
        break; // <-- not sure if this last break is necessary
}
 
  • Like
Likes valenumr
  • #3
I see several problems with this code, aside from the missing break statements for each case in the switch.
  1. The input to select a choice is done outside the while loop, so whatever the first choice is, that's the one that gets performed inside the loop. You need to prompt the user to enter the choice inside the while loop.
  2. The top variable is an uninitialized pointer. If the first choice is to push a value, the code attempts to dereference top, which is pointing to some garbage location.
There might be more problems, but these need to be addressed first.
 
  • #4
Ev
Arjan82 said:
because the case statements should end with a break:

switch in C:
switch (choice) {
    case 1:
        // do something
        break;
    case 2:
        // do something else
        break;
    default:
        // do something defaulty
        break; // <-- not sure if this last break is necessary
}
Even if the break isn't the switch will execute all the cases. Doesn't explain why is it executing only one time.
 
  • #5
Crystal037 said:
Ev
Even if the break isn't the switch will execute all the cases. Doesn't explain why is it executing only one time.
Yes it does: the switch will execute all the cases including the code after case 3.
 
  • #6
Crystal037 said:
Even if the break isn't the switch will execute all the cases. Doesn't explain why is it executing only one time.
Your post is unclear. Without the break statements on each case, all four cases will execute.
pbuk said:
Yes it does: the switch will execute all the cases including the code after case 3.
To be clear, the snippet posted by Arjan82 that Crystal037 quoted doesn't have this problem.

@Crystal037, see my suggestions in post #3.
 
  • #7
pbuk said:
Yes it does: the switch will execute all the cases including the code after case 3.
But when executing its only executing one case given like if I give option 1 it just executes 1 and doesn't go to 2 why and even if I put break in every statement bcz of while it should execute the choice entered before again and again bcz of the while loop
 
  • #8
Mark44 said:
Your post is unclear. Without the break statements on each case, all four cases will execute.

To be clear, the snippet posted by Arjan82 that Crystal037 quoted doesn't have this problem.

@Crystal037, see my suggestions in post #3.
Thats what it should do but all the cases are not getting executed only the choice entered that's getting executed.
Also can u please explain the uninitialised pointer and how does it work.
 
  • #9
Crystal037 said:
But when executing its only executing one case given like if I give option 1 it just executes 1 and doesn't go to 2 why and even if I put break in every statement bcz of while it should execute the choice entered before again and again bcz of the while loop
Is that code that you're running exactly the same as what is shown in post #1? If the code doesn't have break statements in each case, and you enter 1 as your choice, it will execute all three cases.

Crystal037 said:
Also can u please explain the uninitialised pointer and how does it work.
In line 6 you declare top
C:
int *top;
This means that top is a pointer, but it is uninitialized, so it doesn't contain the address of any known variable.
In lines 32 and 40 (in the bodies of the push() and pop() functions) you have
C:
void push(int a[],int e){
    if(*top==MAX_SIZE-1){                // line 32
.
.
.
int pop(int a[]){
    if(*top==-1){                             // line 40
In both lines you are dereferencing top, but since it was never initialized, you are getting access to garbage values.
 
  • Like
Likes sysprog
  • #10
@Crystal037, you posted this question on Tuesday and it's now Sunday evening. Are you making any progress on your program?
 

FAQ: Switch Argument Outside While Loop Execution Explained

What is a switch argument outside while loop execution?

A switch argument outside while loop execution is a programming concept where a switch statement is used to execute a specific block of code based on the value of a variable or expression. This switch statement is placed outside of a while loop, allowing the code to only be executed once rather than continuously within the loop.

Why would you use a switch argument outside while loop execution?

There are several reasons why a switch argument may be used outside a while loop. One reason is to limit the execution of a specific block of code to only once, rather than continuously repeating within the loop. Another reason is to improve the efficiency of the code by executing the switch statement only once, rather than each time the loop is iterated. Additionally, using a switch argument outside a while loop can make the code more readable and organized.

Can you provide an example of a switch argument outside while loop execution?

Sure! Here is an example of a switch argument being used outside a while loop in JavaScript:

let day = prompt("What day is it?");switch(day) { case "Monday": alert("It's the start of the work week."); break; case "Friday": alert("It's finally the weekend!"); break; default: alert("It's just another day.");}

In this example, the switch statement is placed outside the while loop. It will only be executed once, rather than continuously within the loop.

What are the benefits of using a switch argument outside while loop execution?

As mentioned earlier, using a switch argument outside a while loop can improve the efficiency and readability of the code. It also allows for more control over when a specific block of code is executed, rather than it being executed repeatedly within the loop. Additionally, using a switch statement outside the loop can help prevent errors or bugs that may occur from the code being executed multiple times.

Are there any downsides to using a switch argument outside while loop execution?

One potential downside to using a switch argument outside a while loop is that it may limit the flexibility of the code. If the code needs to be modified in the future to execute the switch statement within the loop, it may require more effort and time. Additionally, using a switch argument outside the loop may not be necessary for simpler code, and it may add unnecessary complexity to the program.

Similar threads

Replies
4
Views
998
Replies
5
Views
2K
Replies
18
Views
9K
Replies
5
Views
2K
Replies
22
Views
5K
Replies
3
Views
1K
Replies
2
Views
3K
Back
Top