Control Structures

Overview

 

Conditional structures

The conditional statement we seen:

Syntax of the 'if' Statement Example Explanation

if ( condition ) 
   statement

OR

if ( condition ) {
   statement1;
   statement2;
   ...
}  
    
int x;
int max;


...


if (x > max) {
   max = x;
}  
  

A condition is a true/false expression.

If the condition is true, then the body of the if-statement is executed. Otherwise, it is not executed.

if(condition){         
    statement(s); 
 } 
 else{        
     statement(s);  
}
int x;
int y;
int max;


...


if (x > y) {
   max = x;
}

else{
   max = y;
}

A condition is a true/false expression.

If the condition is true, then the body of the if-statement is executed. Otherwise, the statements in the body of the if-statement is executed.

 


Exercise 1 (To be submitted on BlackBoard)

No.
Statements
Description
1
 if (value < 10) {   
    value = 0;  
} 

The type of the expression in parentheses is boolean. If value is less than 10 then the condition is true and the body of the if-statement, which is one statement (an assignment statement) will be executed. If the condition is false the assignment statement won't be executed. In this case, we don't know the value of value before the if statement is executed, so we can't tell if the body of the if statement will be executed.

The assignment statement assigns the value 0 to the variable value. We can infer that value is an integer (e.g. int) or a floating point number (e.g. double). If its type were boolean, the Java compiler would complain.

2
 
if ((choice == 'Y') || (choice == 'y')) {
    gameOver = true;
}
else{
   gameOver = false;
} 
 
3
 if (cost < amountInPocket) {    
    System.out.println("Spending = $" + cost);    
    amountInPocket = amountInPocket - cost;  
} 
 

 

Loop Structures


Code
Explanation
int x = 1;
while (x <= 10) {
  System.out.println(x);
  x = x + 1; 
} 
An example of a while loop that has this pattern
for (int x = 1; x <= 10; x = x + 1){
  System.out.println(x);
}
A for loop that does the same thing

 


 

Exercise 2

The factorial n! is defined for a positive integer n as

n! = n* (n-1)..2*1

So, for example, 4! = 4 * 3 * 2 *1 = 24

Implement the factorial function in Dr Java interactions pane. Follow the steps below:

>int n = 4;
>//Declare and intialize factorial 
> 
>//Declare and intialize any other variables
>
>while(condition){

factorial = ?? 
//Any other statements

} 
>//Print the value of the factorial
>System.out.println(factorial); 

Exercise 3 (To submitted on BlackBoard)

Complete the Java program Exercise3.java to compute the factorial of first 10 numbers. Your program must compile and run/execute. Upon execution, it should print to screen the following:

1!= 1
2!= 2
3!= 6
4!= 24
5!= 120
6!= 720
7!= 5040
8!= 40320
9!= 362880
10!= 3628800


Exercise 4 (To be submitted on BlackBoard)

In this exercise we will utilize both conditionals and loops to solve problems at at hand. We are intersted in determining even numbers in the range 1 to 25. Complete the Java program Exercise4.java. Your program must compile and run/execute. Upon execution, it should print to screen the following:

Even Numbers between 1 and 25 are:
2
4
6
8
10
12
14
16
18
20
22
24