Académique Documents
Professionnel Documents
Culture Documents
Java and C
Java is derived from C
Many of its syntactic characteristics are
similar to C
However, there are some huge differences
Expressions
Arithmetic operators are the same:
+, , *, /, %, ++,
Relational Operators
Relational operators work the same way but return
different results:
>, >=, <, <=, ==, !=
Statements
Statements in C/C++:
Labeled statement
Expression statement
Compound statement
Selection statement
Iteration statement
Jump statement
Statements
Statements in C/C++:
Labeled statement
Expression statement
Compound statement
Selection statement
Iteration statement
Jump statement
Statements
Statements in C/C++:
Labeled statement
Expression statement
Compound statement
Selection statement
Iteration statement
Jump statement
Any expression
followed by ';'
Much like to Java
10
Statements
Statements in C/C++:
Labeled statement
Expression statement
Compound statement
Selection statement
Iteration statement
Jump statement
Sequence of statements
enclosed in "{}"
Called a block in Java
11
Statements
Statements in C/C++:
Labeled statement
Expression statement
Compound statement
Selection statement
Iteration statement
Jump statement
switch (expr)
if (expr)statement
if (expr) statement
else statement
Same as in Java
12
Statements
Statements in C/C++:
Labeled statement
Expression statement
Compound statement
Selection statement
while (expr) statement
do statement while (expr
Iteration statement
for (exp1; exp2, exp3)
Jump statement
statement
Very similar to Java
13
Statements
Statements in C/C++:
Labeled statement
Expression statement
Compound statement
Selection statement
Iteration statement
goto
break;
Jump statement
Not present in Java
continue;
Not expr
allowed in this
return
course
Very similar
to Java
14
15
Many others!
CS-2303, C-Term 2010
16
17
%u
Treat expression as unsigned decimal number
%f
Treat expression as double precision floating point number; print
without exponent
%e or %E
Treat expression as double precision floating point number; print with
exponent (base 10) scientific notation
%c
Treat value of expression as the code for a single character
%s
Treat expression as a pointer to a string
CS-2303, C-Term 2010
Later in this
course
18
Examples
%6d print signed decimal number in 6-char field
%8.4f print floating point number with four places
after decimal point, field width of 8 characters
CS-2303, C-Term 2010
19
20
21
scanf() Details
K&R Table B-2 (p. 246), D&D Chapter 9
22
Summary
Differences and similarities between Java
and C
Expressions
Statements
23
Questions?
24