Uploaded by atharvagujar12

Control III

advertisement
Learning Goals
By the end of this section, you should be able to:
• write the code for simple branching
• read code that includes branching
• develop a plan for testing code that includes a
branch
Department of Computer Science
APSC 160
Flow of Control III
Branching
Example: Develop a program that prompts the user for the
number of gallons of gas in a car and the number of miles they
wish to travel. If there is sufficient gas for the trip, the program
should show the number of gallons of gas remaining at the end of
the trip. In all cases, the program prints the message "Done" before
terminating. Assume that the car gets 30 miles to the gallon.
if( Boolean ) {
/* true block */
}
Flowchart – your solution
Flowchart
Branching
if( Boolean ) {
/* true block */
}
else {
/* false block */
}
Example: Develop a program that prompts the user for the
number of gallons of gas in a car and the number of miles they
wish to travel. If there is sufficient gas for the trip, the program
should print a message to say that the trip can be completed and
show the number of gallons of gas remaining at the end of the trip;
otherwise, it prints a message to say that there's not enough
gas. In all cases, the program prints the message "Done" before
terminating. Assume that the car gets 30 miles to the gallon.
Flowchart – your solution
Flowchart
Download