# CS1061 C Programming Lecture 5: Building Blocks of Simple Programs - PowerPoint PPT Presentation

PPT – CS1061 C Programming Lecture 5: Building Blocks of Simple Programs PowerPoint presentation | free to download - id: 1504c3-ODY2N

The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
Title:

## CS1061 C Programming Lecture 5: Building Blocks of Simple Programs

Description:

### Integer division truncates remainder. 7 / 5 evaluates to 1 ... Some arithmetic operators act before others (i.e., multiplication before ... – PowerPoint PPT presentation

Number of Views:465
Avg rating:3.0/5.0
Slides: 12
Category:
Tags:
Transcript and Presenter's Notes

Title: CS1061 C Programming Lecture 5: Building Blocks of Simple Programs

1
CS1061 C ProgrammingLecture 5 Building Blocks
of Simple Programs
• A. ORiordan, 2004

2
Arithmetic
• Arithmetic calculations
(multiplication) and / (division)
• Integer division truncates remainder
• 7 / 5 evaluates to 1
• Modulus operator() returns the remainder
• 7 5 evaluates to 2
• Operator precedence
• Some arithmetic operators act before others
(i.e., multiplication before addition) - use
parenthesis when needed.
• Example Find the average of three variables a, b
and c
• Do not use a b c / 3
• Use (a b c ) / 3

3
The if statement
• The Selection structure is used to choose among
alternative courses of action
• Example If students grade is greater than or
equal to 60 then Print Passed
• Algorithm If condition true Print statement
executed and program goes on to next statement.
If false, print statement is ignored and the
program goes onto the next statement.
• Code
• if (grade gt 60) printf("Passed\n")

4
The else clause
• The if selection statement may have an else
part.
• Example If students grade is greater than or
equal to 60 then Print Passed else Print
Failed.
• Here is the C code. Note use of relational
operator (gt)
• printf("Passed\n")
• else
• printf("Failed\n")
• Statement could have been written (using ?
notation)
• grade gt 60 ? printf(Passed\n)
printf(Failed\n)

5
The else clause (continued)
• You can have a set of statements within a pair of
braces, example
• printf("Passed.\n")
• else
• printf("Failed.\n")
• printf("You must take this course
again.\n")
• The second printf is part of the else clause.
Note also the use of the relational operator gt
(less than or equal to)

6
Booleans
• The if statement above contains a Boolean
expression. This is an expression with two values
that eveluates to either true or false.
• if (x lt 4) printf(x is less than 4)
• You can also declare Boolean variables and give
them an initial value.
• bool myBoolean false
• if (myBoolean)
• printf(This statement is never displayed)
• Booleans named after George Boole, First
Professor of Mathematics, U.C.C.

7
Relational Operators
8
C Characters
• Enclosing a character in single quotes, e.g. H,
represents a printable character constant.
Internally, characters are represented
numerically, usually by their ASCII codes. For
example, the ASCII code for A is 65.
• Example Here we declare a character variable
answer with the initial value y (for yes) and
change it in the couse of the program to n (for
no).
• char answer y / set as yes /
• ...
• answer n / changed to no /
• Non-printable characters are represented using
the escape sequence backslash (\). The same
convention is used to represent the characters \
and which would otherwise cause confusion, i.e.
\\ and \.

9
Special Characters
• C has special characters that need to be
escaped
• new-line \n
• horizontal tabulation \t
• vertical tabulation \v
• backspace \b
• carriage return \r
• bell \a
• backslash \\
• question mark \?
• single quote \
• double quote \
• Here is an example within a printf statement
• printf(\tThis is \layed\ out nicely\n)

10
• Before we can write useful programs we need to be
able to read in data from the keyboard.
• A scanf() performs a similar function to printf
except its for (formatted) input. Note the use of
the address-of operator () in the argument list.
• Heres an example where we read in an integer and
a character into variables (and then display
them).
•
• int anInt
• char aChar
•
• printf(Enter one character and one integer)
• scanf(c d, aChar, anInt)
• printf(The value of the integer is d and the
value of the character is c\n, anInt, aChar)

11