Reputation: 71
The code is here:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button push_me,secondButton;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
push_me = findViewById(R.id.push_me);
secondButton = findViewById(R.id.secondButton);
push_me.setOnClickListener(this);
secondButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.push_me:
textView.setText(R.string.Someone_pushed);
break;
case R.id.secondButton:
textView.setText(R.string.hello_android);
break;
}
}
if I take out the break (the two breaks down in the program) the program isn't working properly but I can not understand why.
Upvotes: 0
Views: 90
Reputation:
From the "case" label that applies onwards, your code is executed until the switch block is ended (with a }
), or until it is left with break
(or, depending on context, return
, throw
, continue
, possibly some other things I'm forgetting right now). That's just how it works in Java (and many other C-influenced programming languages).
This behavior is somewhat similar to that of labels and goto instructions, which predate "modern" structured programming. As those have fallen completely out of use unless you have to write Assembly by hand, this behavior is not intuitive for most programmers today, but it lives on as one of those weird traditions, similar to the 80-character line limit which goes all the way back to punchcards.
Upvotes: 1