AND & &
< boolean > & & < boolean > -- > < boolean >
returns true if both inputs are true Returns false if not.
Ex
boolean x = true;
boolean y = false;
boolean z = x & & y; false
boolean w = x & & x; true
Ex
int a = IO.read_int ();
int b = IO.read_int ();
int c = IO.read_int ();
boolean x = (a < c) & & (a < b);
Assume the input stream is:7, 4, 10
What will be the value of x? False
OR | |
< boolean > | | < boolean > -- > < boolean >
returns true if at least one input is true. Returns false otherwise
NOT!
! < boolean > -- > < boolean >
returns ``the other boolean''
Way 3:
/ /read 3 integers
int a = IO.read_int ()
int b = IO.read_int ()
int c = IO.read_int ()
/ /Perfrom comparisions and display results
if ((a < b) & & (b < C))
{
print (a, b, c);
}
else if ((a < c) & & (c < b))
{
print (a, c, b);
}
else ---------
....Four more cases!
static private print (int x, int y, in z)
{
IO.println (x + ````+ y + ````+ z);
}
Remark
These are three main mechanisms of iteration.
- ``While''
- ``For''
- ``Recursion''
Ex
Read and write four integers
int i = 1;
while (i < = 4)
{
int x = IO.read_int ();
IO.print (x);
i = i + 1;
}
input stream
5, 8, 7, 2
outpus stream
5, 8, 7, 2
Ex
Read several nonzero integers and print them.
IO. Example
in 8 - 16 - 4 3 0
out 8 - 16 - 4 3
int number = IO.read_int ();
while (! (number == 0))
{
IO.println (number);
number = IO.read_int ();
}
Remark
Counter controle loops are controled by a counter which governs the behavior of the loop.
Problem
Read three integers and display the difference between the largest and the smallest values.
The sequence of programs will be:
- Read and echo three integers.
- Same as 1 + Determine and display the maximum value
- Same as 2 + Determine and display the minimum value
- Same as 3 + Determine and display the difference of the max and min, and display it.
- Without displaying max and min values or echoin data determine the difference of the max and min values and display its result.