I know Java a bit till operators and a bit of looping ... Can I complete class 9 looping in one day
Answers
Answer:
In 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.
If statements. Most computations require different actions for different inputs.
The following code fragment uses an if statement to put the smaller of two int values in x and the larger of the two values in y, by exchanging the values in the two variables if necessary.
anatomy of an if statement
Flip.java uses Math.random() and an if-else statement to print the results of a coin flip.
The table below summarizes some typical situations where you might need to use an if or if-else statement.
examples of conditionals
While loops. Many computations are inherently repetitive. The while loop enables us to execute a group of statements many times. This enables us to express lengthy computations without writing lots of code.
The following code fragment computes the largest power of 2 that is less than or equal to a given positive integer n.
anatomy of a while loop
TenHellos.java prints "Hello World" 10 times.
PowersOfTwo.java takes an integer command-line argument n and prints all of the powers of 2 less than or equal to n.
For loops. The for loop is an alternate Java construct that allows us even more flexibility when writing loops.
For notation. Many loops follow the same basic scheme: initialize an index variable to some value and then use a while loop to test an exit condition involving the index variable, using the last statement in the while loop to modify the index variable. Java's for loop is a direct way to express such loops.