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 , 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 is a is a collection of one or more statements contained within a pair of curly brace called?A compound statement is a sequence of zero or more statements enclosed within curly braces. Compound statements are frequently used in selection and loop statements.
When you create a block you must place multiple statements within it?When you create a block, you must place multiple statements within it. When you use nested if statements, you must pay careful attention to placement of any else clauses. In the switch structure, break is followed by one of the possible values for the test expression and a colon.
Which of the following is the simplest statement you can use to make a decision?if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.
Which of the following is the name of an unknown data item and can be assigned a value?A random variable is a variable whose value is unknown or a function that assigns values to each of an experiment's outcomes.
Is a term to indicate when multiple methods are written with the same name but with different parameter lists?Method overloading means two or more methods have the same name but have different parameter lists: either a different number of parameters or different types of parameters.
|