CSC 201 - Lecture 3
CSC 201 - Lecture 3
COMPUTER PROGRAMMING I
(C PROGRAMMING LANGUAGE)
LECTURE THREE
LECTURE RECAP
Topics covered so far:
• Lecture 1: Course Introduction &
Computer Programming Basics
Topic to cover:
Contents
• Basic Output
• printf Function
INPUT • Format Specifiers Table
AND • Common Special Characters for
Cursor Control
OUTPUT • Basic Output Examples
• Basic Input
• Basic Input Example
Basic Input
• There is a function in C which
allows the programmer to
accept input from a keyboard.
The sample program below
illustrates the use of this
function:
if (control expression)
{
\\program statement
}
If the control expression is TRUE, the body of the if is executed. If it is
FALSE, the body of the if is skipped.
N.B: There is no “then” keyword in C!
Because of the way in which floating point types are stored, it makes it
very difficult to compare such types for equality. Avoid trying to compare
real variables for equality, or you may encounter unpredictable results.
A.O. Agbeyangi - Chrisland University (CSC) 13
Exercise:
if (expression)
statement1;
else
statement2;
• If the expression is TRUE, statement1 is executed while statement2 is skipped.
• If the expression is FALSE, statement2 is executed while statement1 is skipped.
• Examples:
if (x<y)
min=x;
else
min=y;
switch(expression){
switch case constant-expression :
statement(s);
Statemen break; /* optional */
case constant-expression :
t statement(s);
break; /* optional */
/* you can have any number of case
statements */
default : /* Optional */
statement(s);
A.O. Agbeyangi - Chrisland University (CSC)
}
The following rules apply to a switch statement:
The expression used in a switch statement must have an integral or enumerated type, or
be of a class type in which the class has a single conversion function to an integral or
enumerated type.
You can have any number of case statements within a switch. Each case is followed by the
value to be compared to and a colon.
The constant-expression for a case must be the same data type as the variable in the
switch, and it must be a constant or a literal.
When the variable being switched on is equal to a case, the statements following that case
will execute until a break statement is reached.
When a break statement is reached, the switch terminates, and the flow of control jumps
to the next line following the switch statement.
Not every case needs to contain a break. If no break appears, the flow of control will fall
through to subsequent cases until a break is reached.
A switch statement can have an optional default case, which must appear at the end of
the switch. The default case can be used for performing a task when none of the cases is
true. No break is needed in the default case.
A.O. Agbeyangi - Chrisland University (CSC) 18
Exercise:
• A final note on loop nesting is that you can put any type of loop inside any
other type of loop. For example, a ‘for’ loop can be inside a ‘while’ loop or vice
versa.
A.O. Agbeyangi - Chrisland University (CSC) 29
Exercise: