1.3 Conditionals and LoopsIn the programs that we have examined to this point, each of the statements is executed once, in the order given. Most programs are more complicated because the sequence of statements and the number of times each is executed can vary. We use the term control flow to refer to statement sequencing in a program. Show
If statements.Most computations require different actions for different inputs.
While loops.Many computations are inherently repetitive. The whileloop enables us to execute a group of statements many times. This enables us to express lengthy computations without writing lots of code.
For loops.The for loop is an alternate Java construct that allows us even more flexibility when writing loops.
Nesting.The if, while, and forstatements have the same status as assignment statements or any other statements in Java; that is, we can use them wherever a statement is called for. In particular, we can use one or more of them in the body of another statement to make compound statements. To emphasize the nesting, we use indentation in the program code.
Loop examples.Applications.The ability to program with loops and conditionals immediately opens up the world of computation to us.
Other conditional and loop constructs.To be complete, we consider four more Java constructs related to conditionals and loops. They are used much less frequently than the if, while, and forstatements that we've been working with, but it is worthwhile to be aware of them.
We don't use the following two flow control statements in this textbook, but include them here for completeness.
Exercises
Creative Exercises
Web Exercises
What type of operation is used to compare two values?The equality operator (==) is used to compare two values or expressions. It is used to compare numbers, strings, Boolean values, variables, objects, arrays, or functions.
Is a set of instructions that directs the computer to perform a variety of tasks?A set of instructions that directs a computer's hardware to perform a task is called a program, or software program. The two main types of software are system software and application software.
What is is a hardware and or software that protects a network's resources from intrusion?A firewall is hardware and/or software that is used to protect a computer or private network resources from intruders or hackers who might try to break into those systems.
What property allows you to specify the caption of the form in the title bar group answer choices?The Text property allows you to specify the caption of the window in the title bar.
|