Computer Science 1Week 10
This Week ...This Week ...
• QBasic If StatementsQBasic If Statements Comparison operatorsComparison operators Logical operatorsLogical operators
• Computer ConceptsComputer Concepts GraphicsGraphics SoundSound VideoVideo
Comparison Comparison OperatorsOperators
Comparing Data to DataComparing Data to Data
Review:Review:Numeric OperatorsNumeric Operators
OperatorOperator NameName
^̂ ExponentExponent
** MultiplicationMultiplication
// DivisionDivision
++ AdditionAddition
-- Subtraction & Unary MinusSubtraction & Unary Minus
Comparison Operator Comparison Operator OverviewOverview
• Used to Used to comparecompare two data items two data items Equal, UnequalEqual, Unequal Greater than, Less thanGreater than, Less than
• Can be used with Can be used with bothboth numbers numbers and stringsand strings
• Essential for writing complex Essential for writing complex expressions in programsexpressions in programs
Comparison Comparison OperatorsOperators
OperatorOperator NameName
= Equal To
<> Not Equal To
> Greater Than
>= Greater Than or Equal
< Less Than
<= Less Than or Equal
Number ExamplesNumber Examples
1 = 4
24 <> 31
11 < 10
100 >= 100
False
True
False
True
ASCII ChartASCII ChartReviewReview
NULNUL SOLSOL STXSTX ETXETX EOTEOT ENQENQ ACKACK BELBEL BSBS HTHT LFLF VTVT FFFF CRCR SOSO SISI
DLEDLE CD1CD1 DC2DC2 DC3DC3 DC4DC4 NAKNAK SYNSYN ETBETB CANCAN EMEM SUBSUB ESCESC FSFS GSGS RSRS USUS
spsp !! "" ## $$ %% && '' (( )) ** ++ ,, -- .. //
00 11 22 33 44 55 66 77 88 99 :: ;; << == >> ??
@@ AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO
PP QQ RR SS TT UU VV WW XX YY ZZ [[ \\ ]] ^̂ __
`̀ aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo
pp qq rr ss tt uu vv ww xx yy zz {{ || }} ~~ DELDEL
Comparing LettersComparing Letters
AA 0100000101000001 6565
BB 0100001001000010 6666
CC 0100001101000011 6767
DD 0100010001000100 6868
EE 0100010101000101 6969
FF 0100011001000110 7070
aa 0110000101100001 9797
bb 0110001001100010 9898
cc 0110001101100011 9999
dd 0110010001100100 100100
ee 0110010101100101 101101
ff 0110011001100110 102102
"A" < "a"
ASCII Value
Letter ExamplesLetter Examples
"a" = "A"
"a" > "A"
"abC" < "abc"
"dog" <> "cat"
False
True
True
True
LogicalLogicalOperatorsOperators
Creating Expressive LogicCreating Expressive Logic
Logical OperatorsLogical Operators
OperatorOperator NameName
AND Logical And
OR Logical Or
XOR Logical Exclusive Or
NOT Unary Logical Negation
Logical OperatorsLogical Operators
• OR OperatorOR Operator true if true if eithereither operand is operand is truetrue
• AND OperatorAND Operator true only if true only if bothboth operands are operands are truetrue
• NOT OperatorNOT Operator true if the operand is true if the operand is falsefalse
AND ExampleAND Example
1 < 2 AND 10 < 40
Result: True
True True
AND Example 2AND Example 2
1 < 2 AND 12 < 10
Result: False
True False
OR ExampleOR Example
1 < 2 OR 10 < 40
Result: True
True True
OR Example 2OR Example 2
1 < 2 OR 12 < 10
True False
Result: True
OR Example 3OR Example 3
10 < 2 OR 12 < 10
False False
Result: False
NOT ExampleNOT Example
NOT 1 < 2
Result: False
True
NOT ExampleNOT Example
NOT 1 = 2
Result: True
False
ExamplesExamples
1 < 3 AND 10 < 40
1 = 3 AND 10 < 40
NOT 1 = 2
1 > 3 OR 30 < 20
True
False
True
False
QBasic Precedence QBasic Precedence LevelsLevels
8 ^7 -6 * /5 + -4 = <> > >= < <=3 NOT2 AND1 OR XOR
Highest Level
Lowest Level
Calculate The ResultCalculate The Result
2 = 4 OR 1 > 2 AND 5 > 4 OR 1 < 3
False OR False AND True OR True
False OR False OR True
False OR True
True
Calculate The Result 2Calculate The Result 2
NOT 1 < 4 AND 5 > 4 OR NOT 1 > 4
NOT True AND True OR NOT False
False AND True OR True
False OR True
True
QBasicQBasicIf StatementsIf Statements
Allowing QBasic to Make DecisionsAllowing QBasic to Make Decisions
If StatementIf Statement
• This statement ...This statement ... executes statements if the executes statements if the
expression is expression is truetrue can also contain a false branchcan also contain a false branch
• Uses Uses blocksblocks of statements of statements• Found in virtually every Found in virtually every
programming languageprogramming language
IF Condition THEN
Statements
END IF
Basic SyntaxBasic Syntax
Either True or False
Multiple statements
LET Age = 22
IF Age >= 21 THEN
PRINT “Older than 21"
END IF
ExampleExample
Older than 21!
ExampleExampleOutputOutput
LET Age = 18
IF Age >= 21 THEN
PRINT "Older than 21!"
END IF
Example 2Example 2
Example 2Example 2OutputOutput
Nothing. The Print Statement is
skipped
LET Age = 18
IF Age >= 21 THEN
PRINT "Older than 21!"
END IF
Example 2: Example 2: Please NotePlease Note
Only executed if true
INPUT "Enter 1 to 10", Num
IF Num < 1 OR Num > 10 THEN PRINT "Sorry!"
PRINT Num; " is invalid!"END IF
Example 3Example 3
Enter 1 to 10: 13
Sorry!
13 is invalid!
Example 3Example 3OutputOutput
DIM Answer AS STRING
PRINT "Do you love programming?"
INPUT "y or n: ", Answer
IF Answer = "y" OR Answer = "Y" THEN
PRINT "Most excellent!"END IF
Example 4Example 4
Do you love programming?
y or n: Y
Most excellent!
Example 4Example 4OutputOutput
The Else ClauseThe Else Clause
• Sometimes you need an alternative Sometimes you need an alternative branch in your programbranch in your program
• Used to process statements:Used to process statements: only if the condition is only if the condition is FalseFalse the "else" keyword separates the two blocksthe "else" keyword separates the two blocks
IF Condition THEN
Statements-1
ELSE
Statements-2
END IF
IFIF Statement Syntax Statement Syntax
Processed if the Condition is false
INPUT "Your Age: ", Age
IF Age >= 21 THEN
PRINT "Kegger! :)"
ELSE
PRINT "Milk. :("
END IF
Else ExampleElse Example
Your Age: 22
Kegger! :)
Else ExampleElse ExampleOutputOutput
Your Age: 18
Milk. :(
Else ExampleElse ExampleOutputOutput
INPUT "Your age: ", Age
IF Age >= 1 AND Age <= 100 THEN LET Days = Age * 365
PRINT "Days lived:"; Days
ELSE
PRINT "Invalid Age!"
END IF
Else Example 2Else Example 2
Your age: 20
Days lived: 7300
Example 2Example 2OutputOutput
Your age: 300
Invalid Age!
Example 2Example 2Output 2Output 2
QBasic LabQBasic Lab
If Statements If Statements
Lab: Lab: Furry OlympicsFurry Olympics
• Overview:Overview: its the Furry Olympicsits the Furry Olympics use the If Statementuse the If Statement
• Objectives:Objectives: input scores from three Judgesinput scores from three Judges determine if the athlete qualifiesdetermine if the athlete qualifies
Remember ...Remember ...
• Turn in your program Turn in your program && your outputs your outputs to Lab8 in SacCT to Lab8 in SacCT
• You must do your own workYou must do your own work• If you do not turn in your program, If you do not turn in your program,
you will you will notnot get credit! get credit!
BitmapBitmapGraphicsGraphics
The basics of picturesThe basics of pictures
Bitmap BasicsBitmap Basics
• BitmapBitmap graphic graphic also known as a “raster graphic”also known as a “raster graphic” the most common class of graphicthe most common class of graphic
• Consists of Consists of littlelittle dots called dots called pixelspixels these are arranged in a gridthese are arranged in a grid the color of each pixel is a binary numberthe color of each pixel is a binary number
A Close Look at A Close Look at PixelsPixels
How Colors Are How Colors Are RepresentedRepresented
• Colors are represented usingColors are represented using RedRed--GreenGreen--Blue Blue levelslevels Hue-Saturation-IntensityHue-Saturation-Intensity
• Stored as a binary numberStored as a binary number as the actual as the actual RRGGBB value value a color code - a color code - palette indexpalette index
Color DepthColor Depth
• Color DepthColor Depth refers to the refers to the totaltotal number of colors in an image number of colors in an image the more bits, the more colorsthe more bits, the more colors
• Bits Per PixelBits Per Pixel used to describe color depthused to describe color depth usually a power of 2 number – e.g. 2, 4, 16, 32usually a power of 2 number – e.g. 2, 4, 16, 32
ColorColorPalettesPalettes
Representing Colors By CodesRepresenting Colors By Codes
Color PalettesColor Palettes
• RRGGBB is not always used is not always used maintain a small file sizemaintain a small file size reduce memory requirementsreduce memory requirements
• Color PaletteColor Palette basically a lookup table for colorsbasically a lookup table for colors similar to "color by numbers"similar to "color by numbers" table has table has RedRed--GreenGreen--Blue Blue valuesvalues
Color PalettesColor Palettes
Actual valueActual value
Classifications of Classifications of Color ImagesColor Images
• Grayscale paletteGrayscale palette usually 256 shades of grayusually 256 shades of gray great for black-and-white picturesgreat for black-and-white pictures
• MonochromeMonochrome 2 colors – usually black and white2 colors – usually black and white 1 bit per pixel – allowing 8 pixels per byte1 bit per pixel – allowing 8 pixels per byte used by early computers – e.g. Xerox Starused by early computers – e.g. Xerox Star
Monochrome ExampleMonochrome Example
Classifications of Classifications of Color ImagesColor Images
• 16 Color16 Color 4 bits per pixel – allowing two pixels per byte4 bits per pixel – allowing two pixels per byte used by 80's computers – e.g. Commodore 64used by 80's computers – e.g. Commodore 64
• 256 Color256 Color 8 bits per pixel 8 bits per pixel usually used with a color paletteusually used with a color palette
Classifications of Classifications of Color ImagesColor Images
• High ColorHigh Color 16 bits per pixel – 65,536 colors16 bits per pixel – 65,536 colors also tends to use a palette – not alwaysalso tends to use a palette – not always
• True ColorTrue Color 3 or more bytes3 or more bytes 24 bit = 3 bytes = 16.7 million colors24 bit = 3 bytes = 16.7 million colors RedRed--GreenGreen--BlueBlue values used values used
Changing the Color Changing the Color DepthDepth
• Dithering Dithering uses patterns of two or more colorsuses patterns of two or more colors produce the illusion of additional colorsproduce the illusion of additional colors relies on the eye to blend colors and shapesrelies on the eye to blend colors and shapes
• Color MappingColor Mapping find the closest matching colorfind the closest matching color works well for chartsworks well for charts
I am intrue color!
I am intrue color!
I'm only in 16 colors using
dithering
I'm only in 16 colors using
dithering
Now I am 256 color
grayscale
Now I am 256 color
grayscale
I am monochrome
using dithering!
I am monochrome
using dithering!
Color SummaryColor Summary
NameName Bits Per PixelBits Per Pixel Number of ColorsNumber of Colors
MonochromeMonochrome 11 22
16 Color16 Color 44 1616
GrayscaleGrayscale 88 256256
256 Color256 Color 88 256256
Hi ColorHi Color 1616 65,53665,536
True ColorTrue Color 2424 16,777,21616,777,216
BitmapBitmapResolutionResolution
How Big are Pictures?How Big are Pictures?
Image ResolutionImage Resolution
• ResolutionResolution dimensions of the grid that forms a bitmap graphicdimensions of the grid that forms a bitmap graphic the higher the resolution, the better the qualitythe higher the resolution, the better the quality
• Measured by the number of pixelsMeasured by the number of pixels 150 x 100 = 150 pixels across & 100 pixels high150 x 100 = 150 pixels across & 100 pixels high 640 x 480 = 480,000 pixels = 1 half megapixel640 x 480 = 480,000 pixels = 1 half megapixel
Some Example Some Example ResolutionsResolutions
ResolutionResolution NotesNotes
32 x 3232 x 32 Classic Icon Size – pre Windows XPClassic Icon Size – pre Windows XP
800 x 600800 x 600 Late 1990's Desktop ResolutionLate 1990's Desktop Resolution
1024 x 7681024 x 768 Modern Desktop ResolutionModern Desktop Resolution
1152 x 8641152 x 864 1 Megapixel Image1 Megapixel Image
1600 x 12001600 x 1200 2 Megapixel Image2 Megapixel Image
Calculate Size:Calculate Size:MonochromeMonochrome
640 × 480
= 307,200 pixels
× 1 bit per pixel
= 307,200 bits
= 38,400 Bytes
640
480
Calculate Size:Calculate Size:True ColorTrue Color
800 × 600
= 480,000 pixels
× 24 bits per pixel
= 11,520,000 bits
= 1,440,000 Bytes!
800
600
We Need We Need Compression!Compression!
• LossyLossy compression compression some data is lostsome data is lost excellent compressionexcellent compression used in many graphics, video and sound filesused in many graphics, video and sound files
• LosslessLossless compression compression data is preserveddata is preserved not as compressed, but still goodnot as compressed, but still good
VectorVectorGraphicsGraphics
Graphics using InstructionsGraphics using Instructions
Vector Graphic BasicsVector Graphic Basics
• Consists of set of instructionsConsists of set of instructions used to "draw" a pictureused to "draw" a picture essentially a essentially a graphic program!graphic program!
• Typical attributesTypical attributes shape – square, circle, line, etc...shape – square, circle, line, etc... sizesize color – may include shadingcolor – may include shading
Advantages and Advantages and DisadvantagesDisadvantages
• AdvantagesAdvantages vectors can be resized without losing data!vectors can be resized without losing data! take little storagetake little storage easy to edit – the instructions can be changedeasy to edit – the instructions can be changed
• DisadvantagesDisadvantages usually not as realistic as bitmap imagesusually not as realistic as bitmap images cannot be used for storing photoscannot be used for storing photos
Bitmap vs. VectorBitmap vs. Vector
Stored as raw data (pixels) Stored as instructions
Bitmap Vector
Files can be large Files tend to be small
Data lost when resized No data lost - scalable
Photos and clip art Clip art only
Common File Common File TypesTypes
The Ones You Know and LoveThe Ones You Know and Love
Windows BitmapWindows Bitmap
• Native Native bitmap bitmap format of Microsoft Windows format of Microsoft Windows • Can be used to store any bitmapCan be used to store any bitmap• Uses lossless compressionUses lossless compression
although, the compression is minimalalthough, the compression is minimal files are quite largefiles are quite large
• Extension: Extension: bmpbmp
Bitmap: Portable Bitmap: Portable Network GraphicNetwork Graphic
• FeaturesFeatures 48-bit : 48-bit : 281,474,976,710,656281,474,976,710,656 colors! colors! lossless compressionlossless compression supports transparencysupports transparency public domain format – anyone can create editorspublic domain format – anyone can create editors
• Can be used in webpages Can be used in webpages • Extension: Extension: pngpng
Bitmap: JPEG Bitmap: JPEG
• JJoint oint PPhotographic hotographic EExperts xperts GGrouproup• Designed for PhotographsDesigned for Photographs• Lossy CompressionLossy Compression
compression is designed on contrast changescompression is designed on contrast changes sharp contrast changes cause problemssharp contrast changes cause problems however, sharp changes are rare in photoshowever, sharp changes are rare in photos
• Extension: Extension: jpgjpg or or jpegjpeg
Example JPEGsExample JPEGs
Bitmap: GIF Bitmap: GIF
• GGraphical raphical IInterchange nterchange FFormatormat• AttributesAttributes
Up to 256 colors – paletteUp to 256 colors – palette Lossless compressionLossless compression supports transparencysupports transparency can be used for simple animationscan be used for simple animations
• Can be used in webpagesCan be used in webpages• Extension: Extension: gifgif
Example GIFsExample GIFs
Lossless vs. LossyLossless vs. LossyGIF vs. JPEGGIF vs. JPEG
• This image is high This image is high contrastcontrast
• It will be saved in both It will be saved in both GIF and JPEG formatGIF and JPEG format
• Lossy compression Lossy compression will be visiblewill be visible
Lossless vs. Lossy Lossless vs. Lossy GIF vs. JPEGGIF vs. JPEG
GIFGIF JPEGJPEG
NoiseNoise
Common Vector File Common Vector File TypesTypes
• WWindows indows MMeta eta FFileile used for clipartused for clipart extremely popular extremely popular extension: extension: wmfwmf
• EEnhanced Windows nhanced Windows MMeta eta FFileile contains advanced featurescontains advanced features extension: extension: emfemf
Common Vector File Common Vector File TypesTypes
• SScalable calable VVector ector GGraphicraphic can be used in web pagescan be used in web pages automatically resized when automatically resized when
displayed on different screensdisplayed on different screens extension: extension: svgsvg
Example Vector Example Vector GraphicsGraphics