





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
•Write expressions using the conditional operator •Differentiate the Increment and Decrement operator •Implement selection control using if, if…else and switch statements •Implement repetition control in a program by using while, do… while and for loop statements
Typology: Lecture notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!






Control structures
Selection
structures
If
if
if..else
Nested if
switch
Loops
Structures
while
do..while
for
System.out.println("X is equal to Y");
System.out.println("X is not equal to Y"); x = y; System.out.println("However, now it is.");
if.. Else statements
System.out.println("X is equal to Y"); else System.out.println("X is NOT equal to Y");
if (condition)
execute_statements;
else
execute_statements;
System.out.println("X is not equal to Y"); x = y; System.out.println("However, now it is.");
if.. else..if statements
System.out.println("X is equal to Y"); else if (x !=y) System.out.println("X is NOT equal to Y"); else System.err.println (“ Error Catch!! ");
if (condition)
execute_statements;
else if (condition)
execute_statements;
else
execute_statements;
Nested If statements
if (condition) { if (condition) {
}else if (condition){
}else {
}
}
if (coldOutside==true) { if (snowing==true) { wearParka(); } else { wearJacket(); } } else { wearShorts(); }
Selection Structures
What is the output after executing the following
program?
int mask = 0; int count = 0;
if ((5<7 || (++count <10)) && mask++ <1 ) mask = mask + 1; if (6<8 && false) mask = mask + 10; if( !(mask >1) && ++count>1 ) mask = mask + 100;
System.out.println( " Mask is " + mask + "and count is " + count );
Selection Structures
What is the output after executing the following
program?
int mask = 0; int count = 0;
if (5<7 || ++count <1 && mask++ <1 ) mask = mask + 1; if (6<8 && false) mask = mask + 10; if( !(mask >1) && ++count>1 ) mask = mask + 100;
System.out.println( " Mask is " + mask + " and count is " + count );
Selection Structures
What is the output after executing the following
program?
int n=95;
if (n <= 100) if (n > 90) System.out.println("A"); System.out.println("B"); if (n >80) System.out.println("C"); if (n < 50) System.out.println("D"); else System.out.println("E");
The switch Statement The case Statement
3 - 20
execution continues into the next case.
executed if no CaseExpression matches the
SwitchExpression.
3.9 The switch Statement
NoBreaks.java Practice 3
Practice 3: Solution
3.9 The switch Statement
Practice 4
Practice 4 (20 minutes):
A five digit integer is said to be friendly if
divisible by 3
( the five-digit number itself) is divisible by 5.
For example, the number 42325 is friendly because 4 is divisible by 1,
42 is divisible by 2, 423 is divisible by 3, 4232 is divisible by 4, and 42325
is divisible by 5.
Write a program that prompts for a five-digit integer and determines
whether or not the number is “friendly”. Use JOptionPane for user input
and output.
Compound Assignment Operators
Differences Between Prefix and Postfix
in a statement , there is no difference between prefix and
postfix notation.
++x
Nested if statements and Multiple Alternative Decisions
public static void main (String[] args){
int mask = 5;
if (++mask > 5) System.out.println("mask:"
public static void main (String[] args){ int mask = 2;
if (mask++ > 2) System.out.println("mask:" + mask);
public static void main (String[] args){ int mask = 2;
if (mask++ > 2) System.out.println("mask:" + mask);
public static void main (String[] args){ int mask = 5;
if (mask++ > 5) System.out.println("mask:" + mask);
Loop Structures
The while Loop
4 - 28
while(condition)
{
}
Write a program that use the while loop to print
“Hello” five times.
The while Loop for Input Validation
4 - 29
input is valid.
do…while loops
do
{
statement(s);
}while ( condition );
Practice 9 Solution
Write a program to display the following output.
1
21
321
4321
54321
Practice 9 Solution
Write a program to display the following output.
54321
4321
321
21
1
Nested loops
for(int i = 0; i < 10; i++)
for(int j = 0; j < 10; j++)
loop statement;
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
loop statements;
}
}
Nested For Loops
The continue Statement
executing iteration of a loop to terminate and the
next iteration will begin.
of the condition in while and for loops.
statement should be avoided because it makes the
code hard to read and debug
Problem
public static void main(String []
args)
{
int i=0;
while ( i < 10 )
{
i++;
if ( i == 5)
break;
System.out.println(i);
}
}
public static void main(String [] args)
{
int i=0;
while ( i < 10 )
{
i++;
if ( i == 5)
continue;
System.out.println(i);
}
}
the condition is false in the beginning.
can be evaluated.
4 - 45
4.11 The Random Class
import java.util.Random;
Random randomNumbers = new Random();
4 - 46
Method Description
nextDouble() Returns the next random number as a double. The number will be within the range of 0.0 and 1.0.
nextFloat() Returns the next random number as a float. The number will be within the range of 0.0 and 1.0.
nextInt() Returns the next random number as an int. The number will be within the range of an int, which is – 2,147,483, to +2,147,483,648.
nextInt(int n) (^) This method accepts an integer argument, n. It returns a random number as an int. The number will be within the range of 0 to n.
4.11 The Random Class
”Effort always is a best Indicator of Interest”