COMP 202 Lecture 12: COMP 202 – Lecture 12– char and String

20 views2 pages
COMP 202 – Lecture 12 – char and String
Review: while loops
oA while loop will execute forever, while a condition is
true
oRecall: an if block will execute once, if a condition is true
For Loops
oA for loop is a while loop with a ‘built-in’ counter
oThe 2 types are equivalent and you should use
whichever seems more appropriate
Primality checking
oWhat is an algorithmic way to check if a number is prime?
Check if it is divisible by 2. If it is then it isn’t prime
oOtherwise
Check it is divisible by 3; if it is then isn’t prime
oContinue until that number-1
Useful Loop commands
oThe break keyword causes an immediate exit from innermost loop
oThe continue keyword causes the program to skip to the next iteration of the
loop
Characters
oIn java we can initialize a variable of type char as follows
oChar letter = ‘A’;
oNote that we use single quotes for character literals and double quotes for string
literals
Char and ints
oThere is mapping from char to int
oEvery character has an integer representation (the reverse is not true- char are
represented using 16 bits and ints use 32 bits)
Character arithmetic
oSince every character is just an integer, we can perform arithmetic operations on
characters
o
Aside
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+
$10 USD/m
Billed $120 USD annually
Homework Help
Class Notes
Textbook Notes
40 Verified Answers
Study Guides
1 Booster Class
Class+
$8 USD/m
Billed $96 USD annually
Homework Help
Class Notes
Textbook Notes
30 Verified Answers
Study Guides
1 Booster Class