Summary

  • Branching causes your program to depart from a top-down statement-by-statement execution.

  • A method call is the most common form of unconditional branching. When the method completes, execution returns to the point where it left off.

  • Conditional branching enables your program to branch based on runtime conditions, typically based on the value or relative value of one or more objects or variables.

  • The if construct executes a statement if a condition is true and skips it otherwise.

  • When the condition in an if statement is actually two conditions joined by an or operator, if the first condition evaluates to true, the second condition will not be evaluated at all. This is called short-circuiting.

  • The if…else construct lets you take one set of actions if the condition tested evaluates true, and a different set of actions if the condition tested evaluates false.

  • if statements can be nested to evaluate more complex conditions.

  • The switch statement lets you compare the value of an expression with several constant values (integers, enumerated constants, or strings), and take action depending on which value matches.

  • It is good programming practice for switch statements to include a default statement that executes if no other matches are found.

  • Iteration, or looping, allows you to take the same action several times consecutively. Iterations are typically controlled by a conditional expression.

  • The goto statement is used to redirect execution to another point in the program, but its use is typically discouraged.

  • The while loop executes a block of code while the tested condition evaluates true. The condition is tested before each iteration.

  • The do…while loop is similar to the while loop, but the condition is evaluated at the end of the iteration so that the iterated statement is guaranteed to execute at least once.

  • The for loop is used to execute a statement a specific number of times. The header of the for loop can be used to initialize one or more variables, test a logical condition, and modify the variables. The typical use of a for loop is to initialize a counter once, test that a condition is using that counter before each iteration, and modify the counter after each iteration.

Now you’ve seen how you can control the flow of your programs, and combined with user input, we hope you can see how your programs are now a lot more powerful. The ability to react to changes in data is what most applications are all about. That completes our tour of the fundamentals of programming, and now you’re ready to take the next step. Everything you’ve done so far is what’s called procedural programming, but in Chapter 6, that’s going to change. There’s nothing wrong with procedural programming; in fact, most modern languages have their roots in procedural programming, and you’ll probably find that you now can understand the fundamentals of other languages with procedural roots, such as Visual Basic and JavaScript.

The basic data types you have to work with right now, though, form a somewhat short list—you can do plenty of math, determine true or false, and work with text in the form of strings. That’s a good start, but what if you want to model a somewhat more complex object with your code—a dog, for example, or an employee, or a book? You need a data type that’s much more advanced, one that you define yourself. That’s object-oriented programming, and that’s the subject of the next chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset