how a variable declaration works inside a case in switch statement [duplicate]Declaring and initializing variables within Java switchesReplacements for switch statement in Python?Multiple cases in switch statementWhy can't variables be declared in a switch statement?Variable declaration in a C# switch statementFastest way to determine if an integer's square root is an integerWhy can't I use switch statement on a String?How to write a switch statement in RubyHow do I declare and initialize an array in Java?Declaring variables inside a switch statementSwitch statement multiple cases in JavaScript

What do "Sech" and "Vich" mean in this sentence?

Voltage Balun 1:1

Mug and wireframe entirely disappeared

What do I do if my advisor made a mistake?

Is Benjen dead?

Out of scope work duties and resignation

Would a small hole in a Faraday cage drastically reduce its effectiveness at blocking interference?

Will 700 more planes a day fly because of the Heathrow expansion?

Is it normal for gliders not to have attitude indicators?

Should I simplify my writing in a foreign country?

Are the Night's Watch still required?

Install LibreOffice-Writer Only not LibreOffice whole package

My first c++ game (snake console game)

When an imagined world resembles or has similarities with a famous world

Why aren't nationalizations in Russia described as socialist?

Trigonometry substitution issue with sign

Is there a word that describes the unjustified use of a more complex word?

Hostile Divisor Numbers

What was Bran's plan to kill the Night King?

Is the book wrong about the Nyquist Sampling Criterion?

Dihedral group D4 composition with custom labels

Why is my arithmetic with a long long int behaving this way?

How long would it take for people to notice a mass disappearance?

Does "Captain Marvel" contain spoilers for "Avengers: Infinity War"?



how a variable declaration works inside a case in switch statement [duplicate]


Declaring and initializing variables within Java switchesReplacements for switch statement in Python?Multiple cases in switch statementWhy can't variables be declared in a switch statement?Variable declaration in a C# switch statementFastest way to determine if an integer's square root is an integerWhy can't I use switch statement on a String?How to write a switch statement in RubyHow do I declare and initialize an array in Java?Declaring variables inside a switch statementSwitch statement multiple cases in JavaScript






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2
















This question already has an answer here:



  • Declaring and initializing variables within Java switches

    5 answers



I'm having trouble understanding how the switch statement works. The following code works even the value of a is 4. I understand that each case inside a switch statement is not a block with its own local scope but does that mean that the variable x was declared even a is not equal to 2?



int a = 2;
switch(a)
case 2:
int x = 4;
case 3:
x = 5;
case 4:
x = 7;










share|improve this question















marked as duplicate by Community Mar 23 at 2:11


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 1





    As noted in the answer to the other question it boils down to this: A declaration isn't really "executable" although initialization is. So even if int x = 4 is never executed, you've still declared a variable x in the scope of the switch. So x will be declared even if case 2 never runs

    – Nikos Tzianas
    Mar 23 at 2:07


















2
















This question already has an answer here:



  • Declaring and initializing variables within Java switches

    5 answers



I'm having trouble understanding how the switch statement works. The following code works even the value of a is 4. I understand that each case inside a switch statement is not a block with its own local scope but does that mean that the variable x was declared even a is not equal to 2?



int a = 2;
switch(a)
case 2:
int x = 4;
case 3:
x = 5;
case 4:
x = 7;










share|improve this question















marked as duplicate by Community Mar 23 at 2:11


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 1





    As noted in the answer to the other question it boils down to this: A declaration isn't really "executable" although initialization is. So even if int x = 4 is never executed, you've still declared a variable x in the scope of the switch. So x will be declared even if case 2 never runs

    – Nikos Tzianas
    Mar 23 at 2:07














2












2








2









This question already has an answer here:



  • Declaring and initializing variables within Java switches

    5 answers



I'm having trouble understanding how the switch statement works. The following code works even the value of a is 4. I understand that each case inside a switch statement is not a block with its own local scope but does that mean that the variable x was declared even a is not equal to 2?



int a = 2;
switch(a)
case 2:
int x = 4;
case 3:
x = 5;
case 4:
x = 7;










share|improve this question

















This question already has an answer here:



  • Declaring and initializing variables within Java switches

    5 answers



I'm having trouble understanding how the switch statement works. The following code works even the value of a is 4. I understand that each case inside a switch statement is not a block with its own local scope but does that mean that the variable x was declared even a is not equal to 2?



int a = 2;
switch(a)
case 2:
int x = 4;
case 3:
x = 5;
case 4:
x = 7;





This question already has an answer here:



  • Declaring and initializing variables within Java switches

    5 answers







java switch-statement






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 2:16









Tomin B Azhakathu

2,09011022




2,09011022










asked Mar 23 at 1:30









glennmarkglennmark

708




708




marked as duplicate by Community Mar 23 at 2:11


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Community Mar 23 at 2:11


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 1





    As noted in the answer to the other question it boils down to this: A declaration isn't really "executable" although initialization is. So even if int x = 4 is never executed, you've still declared a variable x in the scope of the switch. So x will be declared even if case 2 never runs

    – Nikos Tzianas
    Mar 23 at 2:07













  • 1





    As noted in the answer to the other question it boils down to this: A declaration isn't really "executable" although initialization is. So even if int x = 4 is never executed, you've still declared a variable x in the scope of the switch. So x will be declared even if case 2 never runs

    – Nikos Tzianas
    Mar 23 at 2:07








1




1





As noted in the answer to the other question it boils down to this: A declaration isn't really "executable" although initialization is. So even if int x = 4 is never executed, you've still declared a variable x in the scope of the switch. So x will be declared even if case 2 never runs

– Nikos Tzianas
Mar 23 at 2:07






As noted in the answer to the other question it boils down to this: A declaration isn't really "executable" although initialization is. So even if int x = 4 is never executed, you've still declared a variable x in the scope of the switch. So x will be declared even if case 2 never runs

– Nikos Tzianas
Mar 23 at 2:07













1 Answer
1






active

oldest

votes


















0














Case 1



 int a = 2;
switch(a)

case 2:
int x = 4;
// executes
case 3:
x = 5;
// executes
case 4:
x = 7;
// executes




in this value of x will be 7 because the switch won't break if it find a matching case.



Case 2



If you want to stop execution on 2 do the following



 int a = 2;
switch(a)

case 2:
int x = 4; // variable declaration works even if case wont match. Validity inside switch for every line of code after this line
break;
// executes and break
case 3:
x = 5;
break;
// executes and break
case 4:
x = 7;
break;
// executes and break
default:
// write default case here



You need not use int in every x because of the scope of the declaration throughout the scope.



The scope of the declaration of a variable in the switch will affect the following statements or line of code in the switch. It's because, if the variable declaration it will if the scope is not defined by braces.



Case 3



 int a = 3;
switch(a)

case 2:

int x = 4;
break; // u can skip this break to. Then also it will throw error.
// x is local variable inside this braces

// executes
case 3:
x = 5;
// error
case 4:
x = 7;
// error
System.out.println(""+x);





In the above case, it will through the error. Because its scope is only in that case.
In this scenario, the scope of a variable is declared inside the braces. so outside that braces, x won't work. Because x is a local variable in that case condition.






share|improve this answer




















  • 1





    i think you misunderstood the question.. if a == 4, then x = 7.. but there must be some error because x is only declared in case 2.. my question is why does it not throw an error..

    – glennmark
    Mar 23 at 1:41






  • 1





    The scope of decleartion of a variable in switch will affect the following statements or line of code in switch

    – Tomin B Azhakathu
    Mar 23 at 1:43






  • 1





    @glennmark this wont throw error.

    – Tomin B Azhakathu
    Mar 23 at 1:46






  • 1





    do you mean that every variable declaration inside a switch statement is invoked even though they don't fall in the right case?

    – glennmark
    Mar 23 at 1:49






  • 1





    @glennmark Can you check the explaination with comment is enough. Else we can discuss in chat

    – Tomin B Azhakathu
    Mar 23 at 2:03

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Case 1



 int a = 2;
switch(a)

case 2:
int x = 4;
// executes
case 3:
x = 5;
// executes
case 4:
x = 7;
// executes




in this value of x will be 7 because the switch won't break if it find a matching case.



Case 2



If you want to stop execution on 2 do the following



 int a = 2;
switch(a)

case 2:
int x = 4; // variable declaration works even if case wont match. Validity inside switch for every line of code after this line
break;
// executes and break
case 3:
x = 5;
break;
// executes and break
case 4:
x = 7;
break;
// executes and break
default:
// write default case here



You need not use int in every x because of the scope of the declaration throughout the scope.



The scope of the declaration of a variable in the switch will affect the following statements or line of code in the switch. It's because, if the variable declaration it will if the scope is not defined by braces.



Case 3



 int a = 3;
switch(a)

case 2:

int x = 4;
break; // u can skip this break to. Then also it will throw error.
// x is local variable inside this braces

// executes
case 3:
x = 5;
// error
case 4:
x = 7;
// error
System.out.println(""+x);





In the above case, it will through the error. Because its scope is only in that case.
In this scenario, the scope of a variable is declared inside the braces. so outside that braces, x won't work. Because x is a local variable in that case condition.






share|improve this answer




















  • 1





    i think you misunderstood the question.. if a == 4, then x = 7.. but there must be some error because x is only declared in case 2.. my question is why does it not throw an error..

    – glennmark
    Mar 23 at 1:41






  • 1





    The scope of decleartion of a variable in switch will affect the following statements or line of code in switch

    – Tomin B Azhakathu
    Mar 23 at 1:43






  • 1





    @glennmark this wont throw error.

    – Tomin B Azhakathu
    Mar 23 at 1:46






  • 1





    do you mean that every variable declaration inside a switch statement is invoked even though they don't fall in the right case?

    – glennmark
    Mar 23 at 1:49






  • 1





    @glennmark Can you check the explaination with comment is enough. Else we can discuss in chat

    – Tomin B Azhakathu
    Mar 23 at 2:03















0














Case 1



 int a = 2;
switch(a)

case 2:
int x = 4;
// executes
case 3:
x = 5;
// executes
case 4:
x = 7;
// executes




in this value of x will be 7 because the switch won't break if it find a matching case.



Case 2



If you want to stop execution on 2 do the following



 int a = 2;
switch(a)

case 2:
int x = 4; // variable declaration works even if case wont match. Validity inside switch for every line of code after this line
break;
// executes and break
case 3:
x = 5;
break;
// executes and break
case 4:
x = 7;
break;
// executes and break
default:
// write default case here



You need not use int in every x because of the scope of the declaration throughout the scope.



The scope of the declaration of a variable in the switch will affect the following statements or line of code in the switch. It's because, if the variable declaration it will if the scope is not defined by braces.



Case 3



 int a = 3;
switch(a)

case 2:

int x = 4;
break; // u can skip this break to. Then also it will throw error.
// x is local variable inside this braces

// executes
case 3:
x = 5;
// error
case 4:
x = 7;
// error
System.out.println(""+x);





In the above case, it will through the error. Because its scope is only in that case.
In this scenario, the scope of a variable is declared inside the braces. so outside that braces, x won't work. Because x is a local variable in that case condition.






share|improve this answer




















  • 1





    i think you misunderstood the question.. if a == 4, then x = 7.. but there must be some error because x is only declared in case 2.. my question is why does it not throw an error..

    – glennmark
    Mar 23 at 1:41






  • 1





    The scope of decleartion of a variable in switch will affect the following statements or line of code in switch

    – Tomin B Azhakathu
    Mar 23 at 1:43






  • 1





    @glennmark this wont throw error.

    – Tomin B Azhakathu
    Mar 23 at 1:46






  • 1





    do you mean that every variable declaration inside a switch statement is invoked even though they don't fall in the right case?

    – glennmark
    Mar 23 at 1:49






  • 1





    @glennmark Can you check the explaination with comment is enough. Else we can discuss in chat

    – Tomin B Azhakathu
    Mar 23 at 2:03













0












0








0







Case 1



 int a = 2;
switch(a)

case 2:
int x = 4;
// executes
case 3:
x = 5;
// executes
case 4:
x = 7;
// executes




in this value of x will be 7 because the switch won't break if it find a matching case.



Case 2



If you want to stop execution on 2 do the following



 int a = 2;
switch(a)

case 2:
int x = 4; // variable declaration works even if case wont match. Validity inside switch for every line of code after this line
break;
// executes and break
case 3:
x = 5;
break;
// executes and break
case 4:
x = 7;
break;
// executes and break
default:
// write default case here



You need not use int in every x because of the scope of the declaration throughout the scope.



The scope of the declaration of a variable in the switch will affect the following statements or line of code in the switch. It's because, if the variable declaration it will if the scope is not defined by braces.



Case 3



 int a = 3;
switch(a)

case 2:

int x = 4;
break; // u can skip this break to. Then also it will throw error.
// x is local variable inside this braces

// executes
case 3:
x = 5;
// error
case 4:
x = 7;
// error
System.out.println(""+x);





In the above case, it will through the error. Because its scope is only in that case.
In this scenario, the scope of a variable is declared inside the braces. so outside that braces, x won't work. Because x is a local variable in that case condition.






share|improve this answer















Case 1



 int a = 2;
switch(a)

case 2:
int x = 4;
// executes
case 3:
x = 5;
// executes
case 4:
x = 7;
// executes




in this value of x will be 7 because the switch won't break if it find a matching case.



Case 2



If you want to stop execution on 2 do the following



 int a = 2;
switch(a)

case 2:
int x = 4; // variable declaration works even if case wont match. Validity inside switch for every line of code after this line
break;
// executes and break
case 3:
x = 5;
break;
// executes and break
case 4:
x = 7;
break;
// executes and break
default:
// write default case here



You need not use int in every x because of the scope of the declaration throughout the scope.



The scope of the declaration of a variable in the switch will affect the following statements or line of code in the switch. It's because, if the variable declaration it will if the scope is not defined by braces.



Case 3



 int a = 3;
switch(a)

case 2:

int x = 4;
break; // u can skip this break to. Then also it will throw error.
// x is local variable inside this braces

// executes
case 3:
x = 5;
// error
case 4:
x = 7;
// error
System.out.println(""+x);





In the above case, it will through the error. Because its scope is only in that case.
In this scenario, the scope of a variable is declared inside the braces. so outside that braces, x won't work. Because x is a local variable in that case condition.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 23 at 2:02

























answered Mar 23 at 1:38









Tomin B AzhakathuTomin B Azhakathu

2,09011022




2,09011022







  • 1





    i think you misunderstood the question.. if a == 4, then x = 7.. but there must be some error because x is only declared in case 2.. my question is why does it not throw an error..

    – glennmark
    Mar 23 at 1:41






  • 1





    The scope of decleartion of a variable in switch will affect the following statements or line of code in switch

    – Tomin B Azhakathu
    Mar 23 at 1:43






  • 1





    @glennmark this wont throw error.

    – Tomin B Azhakathu
    Mar 23 at 1:46






  • 1





    do you mean that every variable declaration inside a switch statement is invoked even though they don't fall in the right case?

    – glennmark
    Mar 23 at 1:49






  • 1





    @glennmark Can you check the explaination with comment is enough. Else we can discuss in chat

    – Tomin B Azhakathu
    Mar 23 at 2:03












  • 1





    i think you misunderstood the question.. if a == 4, then x = 7.. but there must be some error because x is only declared in case 2.. my question is why does it not throw an error..

    – glennmark
    Mar 23 at 1:41






  • 1





    The scope of decleartion of a variable in switch will affect the following statements or line of code in switch

    – Tomin B Azhakathu
    Mar 23 at 1:43






  • 1





    @glennmark this wont throw error.

    – Tomin B Azhakathu
    Mar 23 at 1:46






  • 1





    do you mean that every variable declaration inside a switch statement is invoked even though they don't fall in the right case?

    – glennmark
    Mar 23 at 1:49






  • 1





    @glennmark Can you check the explaination with comment is enough. Else we can discuss in chat

    – Tomin B Azhakathu
    Mar 23 at 2:03







1




1





i think you misunderstood the question.. if a == 4, then x = 7.. but there must be some error because x is only declared in case 2.. my question is why does it not throw an error..

– glennmark
Mar 23 at 1:41





i think you misunderstood the question.. if a == 4, then x = 7.. but there must be some error because x is only declared in case 2.. my question is why does it not throw an error..

– glennmark
Mar 23 at 1:41




1




1





The scope of decleartion of a variable in switch will affect the following statements or line of code in switch

– Tomin B Azhakathu
Mar 23 at 1:43





The scope of decleartion of a variable in switch will affect the following statements or line of code in switch

– Tomin B Azhakathu
Mar 23 at 1:43




1




1





@glennmark this wont throw error.

– Tomin B Azhakathu
Mar 23 at 1:46





@glennmark this wont throw error.

– Tomin B Azhakathu
Mar 23 at 1:46




1




1





do you mean that every variable declaration inside a switch statement is invoked even though they don't fall in the right case?

– glennmark
Mar 23 at 1:49





do you mean that every variable declaration inside a switch statement is invoked even though they don't fall in the right case?

– glennmark
Mar 23 at 1:49




1




1





@glennmark Can you check the explaination with comment is enough. Else we can discuss in chat

– Tomin B Azhakathu
Mar 23 at 2:03





@glennmark Can you check the explaination with comment is enough. Else we can discuss in chat

– Tomin B Azhakathu
Mar 23 at 2:03





Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현