



|
|
CS1 Course Site
|
Class Notes
Monday October 16 , 2000
|
|
Imperative Programming
Definition
IMPERATIVE PROGRAMMING is a style of programming which features commands.
Characteristics of the imperative style are...
- binding commands (assignment statements)
- conditional commands (selections)
- repetition commands
- IO commands
Program to read two integers and display the larger of the two...
int a = IO.read_int ();
int b = IO.read_int ();
if (a > b)
{
IO.println (a);
}
else
{
IO.println (b);
}
|
Program to read two integers and display the work ``OK'' if they are not equal.
int a = IO.read_int ();
int b = IO.read_int ();
if (a! = b)
{
IO.print (``OK'');
IO.println ();
}
|
MULTIWAY IF
Read one integer and display ``zero, '' ``negative, '' or ``positive'' depending on the integer.
int a = IO.read_int ();
if (a > 0)
{
IO.println (``Positive'');
}
else if
{
IO.println (``Negative'');
}
else
{
IO.println (``Zero'');
}
|
NESTED IFS
Read three integers and display them in order, low to high.
/ /Read the integers.
int a = IO.read_int ();
int b = IO.read_int ();
int c = IO.read_int ();
if (a < b)
{
if (a < c)
{
IO.println (a);
if (b < c)
{
IO.println (b);
IO.println (c);
}
else
{
IO.println (c);
IO.println (b);
}
|
MULTIWAY IF WITH ABSTRACTION
if (less (a, b, c))
{
IO.println (a + ````+ b + ````+ c);
}
else if (less (a, c, b))
{
IO.println (a + ````+ c + ````+ b);
}
else if (less (b, a, c))
{
IO.println (b + ````+ a + ````+ c);
}
else if (less (c, b, a))
{
IO.println (c + ````+ b + ````+ a);
}
else if (less (b, c, a))
{
IO.println (b + ````+ c + ````+ a);
}
else if (less (c, b, a))
{
IO.println (c + ````+ b + ````+ a);
}
|
Boolean
The boolean data type
name: boolean
values: true, false
operators: & & (and), | | (or)
EXAMPLE:
static private boolean less
(int x, int y, int z)
{
if (x < y)
{
if (y < z)
{
return true;
}
}
|
|
|