Methods and Control Flow 6282004 Control Flow n

  • Slides: 7
Download presentation
Methods and Control Flow 6/28/2004

Methods and Control Flow 6/28/2004

Control Flow n n When a program executes, the computer goes through the statements

Control Flow n n When a program executes, the computer goes through the statements in the program, in some order The order of the execution is sometimes called “flow of control” or “control flow” Calling a method causes control to go into another object’s instructions Control returns to caller when the method completes 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 2

Visualizing Control Flow n n Think of a control flow as a little “worm”

Visualizing Control Flow n n Think of a control flow as a little “worm” that reads your code and runs it step-by-step When we call a method, we say that control “enters” that method Bank. System alice bob Bank. Acct balance 100 0 balance 200 0 public class Bank. System public class Bank. Account { … { private int balance; public static void main public Bank. Account() (String args[]) { balance = 0; { … create alice and bob here … } } // call alice and bob’s methods public int get. Balance() alice. deposit( 100 ); { return balance; bob. deposit( 200 ); } } int a = alice. get. Balance(); public void deposit( int amount ) int b = bob. get. Balance(); { balance += amount; System. out. println( a + “ “ } } + b ); } } Whew! } That was a Copyright 2004, by the authors of these slides, and Ateneo de } lot of work! 6/28/2004 3 Manila University. All rights reserved

Another Bank. System Example n n Suppose Bank. System’s main method creates a Bank.

Another Bank. System Example n n Suppose Bank. System’s main method creates a Bank. Account object and then eventually calls withdraw on that object Control transfers to the withdraw method in Bank. Account Within the withdraw method, is. Below. Min. Balance (also in Bank. Account) is called, so control transfers to that method Control moves back to the calling methods when the called methods complete 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 4

Bank. System’s main() public class Bank. System { public static void main( String args[]

Bank. System’s main() public class Bank. System { public static void main( String args[] ) { Bank. Account b = new Bank. Account(); b. deposit( 11000 ); b. withdraw( 2000 ); . . . } } 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 5

Bank. Account’s withdraw() public class Bank. Account {. . . public void withdraw( int

Bank. Account’s withdraw() public class Bank. Account {. . . public void withdraw( int amount ) { if ( amount <= balance ) { balance = balance - amount; if ( is. Below. Min. Balance() ) balance = balance – 50; // subtract penalty } else System. out. println( “Insufficient Balance” ); }. . . } 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 6

is. Below. Min. Balance() public class Bank. Account {. . . public static final

is. Below. Min. Balance() public class Bank. Account {. . . public static final int MINBAL = 10000; . . . public boolean is. Below. Min. Balance() { return (balance < MINBAL); }. . . } 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 7