SUNY Geneseo Department of Computer Science


Conditionals and Logic

{Date}

CSci 141, Spring 2004
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Misc

I'm out of town Thursday (1/29)

Questions?

Reading Summary

Sections 5.1 - 5.5

Conditionals are how to use logic

Boolean logic

Logic operators

Law of excluded middle

Manipulating expressions

Proof by case

Execution time

            for ( i = 0; i < 1000000000;   i++ ) {
                a[i] = 0;
            }

Logic Examples

Determine whether a point (x,y,z) lies inside a cylinder (from computer graphics)

[A Cylinder of Height h and Radius r]

    if (    y >= 0
         && y <= h
         && x*x + z*z <= r*r )

Determine if point is outside the cylinder

    if (    y < 0
         || y > h
         || x*x + y*y > r*r	)

Make a robot move forward until it either comes to a wall or a red tile

    // not (robot at wall or a red tile)
    while (    robot.okToMove()
            && robot.colorOfTile() != Color.red ) {
        robot.move();
    }

Move at least 3 tiles, then continue until a wall or red tile

    int tiles = 0;
    while (    tiles < 3
            || (    robot.okToMove() 
                 && robot.colorOfTile() != Color.red )	) {
        robot.move();
        tiles = tiles + 1;
    }
    do {
        move
        move
        move
    } while( not red and not wall)

Digression: Why Robot.NORTH instead of NORTH or Color.red instead of red?

    Class . Name

Database application

Proof by Case

Where have you seen it recently?

Northeast corner algorithm

Next

Introduction to Recursion

Read Section 6.1


Next Lecture