0% found this document useful (0 votes)
2 views

CSC 201 - Lecture 3

Lecture Three of the Computer Programming I course covers input and output, decision making, and program looping in C programming. Key topics include the use of printf and scanf functions for output and input, decision-making structures like if, if-else, and switch statements, as well as looping constructs such as while, for, and do-while loops. Exercises are provided to reinforce learning, including writing programs for basic input/output and decision-making tasks.

Uploaded by

scottsamantha101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CSC 201 - Lecture 3

Lecture Three of the Computer Programming I course covers input and output, decision making, and program looping in C programming. Key topics include the use of printf and scanf functions for output and input, decision-making structures like if, if-else, and switch statements, as well as looping constructs such as while, for, and do-while loops. Exercises are provided to reinforce learning, including writing programs for basic input/output and decision-making tasks.

Uploaded by

scottsamantha101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

CSC 201

COMPUTER PROGRAMMING I
(C PROGRAMMING LANGUAGE)

LECTURE THREE
LECTURE RECAP
Topics covered so far:
• Lecture 1: Course Introduction &
Computer Programming Basics

• Lecture 2: C Program Structure,


Variables, Expressions & Operators

A.O. Agbeyangi - Chrisland University (CSC) 2


3

 Topic to cover:

 Input and Output


LECTURE
 Decision Making
3
 Program Loop

A.O. Agbeyangi - Chrisland University (CSC)


4

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

A.O. Agbeyangi - Chrisland University (CSC)


Basic Output
• Now, let us look at an example of a printf() statement;
printf("value of sum is %d\n", sum);
• which produced for example an output;
value of sum is 33
• The first argument of the printf function is called the control string. When the printf is
executed, it starts printing the text in the control string until it encounters the %
character. The % sign is a special character in C and marks the beginning of a format
specifier. A format specifier controls how the value of a variable will be displayed on
the screen. When a format specifier is found, printf looks up the next argument (in
this case sum), displays its value and continues on. The d character that follows the %
indicates that a (d)ecimal integer will be displayed. At the end of the control
statement, printf reads the special character \n which indicates print the new line
character.
A.O. Agbeyangi - Chrisland University (CSC) 5
Format
Specifi
ers
Table

A.O. Agbeyangi - Chrisland University (CSC) 6


Common
Special
Characters
for Cursor
Control

A.O. Agbeyangi - Chrisland University (CSC) 7


Basic
Output
Example
s

A.O. Agbeyangi - Chrisland University (CSC) 8


9

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:

A.O. Agbeyangi - Chrisland University (CSC)


 What happens in this first program example?
 An integer called pin is defined. A prompt to enter in a number is then
printed with the first printf statement. The scanf routine, which accepts
the response, has a control string and an address list.
 In the control string, the format specifier %d shows what data type is
expected. The &pin argument specifies the memory location of the
variable the input will be placed in. After the scanf routine completes,
the variable pin will be initialized with the input integer. This is confirmed
with the second printf statement. The ‘&’ character has a very special
meaning in C.
 It is the address operator. (We will discuss more with ‘&’ character when
we get to pointers…)

A.O. Agbeyangi - Chrisland University (CSC) 10


Exercise :
Write a program to add two integer numbers to be entered by user.

A.O. Agbeyangi - Chrisland University (CSC) 11


DECISION MAKING
• Decision-making structures require that the programmer specifies one or more conditions
to be evaluated or tested by the program, along with a statement or statements to be
executed if the condition is determined to be true, and optionally, other statements to be
executed if the condition is determined to be false.
• In a sense, it makes a program “smarter” by allowing different choices to be made.
• In C, there are three decision making statements:
• if execute a statement or not
• if-else choose to execute one of two statements
• switch choose to execute one of a number of statements
• Nested if use of one or more if or if else statement inside another if or
if else statement(s)
• Nested switch use of one or more switch statement inside another switch
statement(s).

A.O. Agbeyangi - Chrisland University (CSC) 12


if Statement
The if statement allows branching (decision making) depending upon a
condition.
The basic syntax is:

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:

A.O. Agbeyangi - Chrisland University (CSC) 14


If-else
STATEMENT
• Used to decide between two courses of action. The syntax of the if-else
statement is

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;

A.O. Agbeyangi - Chrisland University (CSC) 15


Exercise:

A.O. Agbeyangi - Chrisland University (CSC) 16


• A switch statement allows a variable to be tested for
equality against a list of values. Each value is called a case,
17 and the variable being switched on is checked for each
switch case.
• The syntax for a switch statement in C programming
language is as follows:

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.O. Agbeyangi - Chrisland University (CSC) 19


Exercise: Nested - if

A.O. Agbeyangi - Chrisland University (CSC) 20


Program
Looping
• Program looping is often desirable in coding in any
language to have the ability to repeat a block of
statements a number of times.
• In C, there are statements that allow iteration of this
type i.e a loop statement.

• A loop statement allows us to execute a statement or


group of statements multiple times.
• Types: while loop, for loop, do-while loop and nested
loop

A.O. Agbeyangi - Chrisland University (CSC) 21


• A while loop in C programming repeatedly
22 executes a target statement as long as a given
condition is true.
• The syntax of a while loop in C programming
language is:
while(condition)
{
while statement(s);
}
Loop • The statement(s) may be a single statement
or a block of statements. The condition may
be any expression. The loop iterates while the
condition is true. When the condition
becomes false, the program control passes to
the line immediately following the loop.
A.O. Agbeyangi - Chrisland University (CSC)
Exercise:

A.O. Agbeyangi - Chrisland University (CSC) 23


24

• A for loop is a repetition control structure that


allows you to efficiently write a loop that
needs to execute a specific number of times.

• The syntax of a for loop in C programming


language is:
for Loop for ( init; condition; increment )
{
statement(s);
}

A.O. Agbeyangi - Chrisland University (CSC)


Here is the flow of control in a ‘for’ loop:
 The init step is executed first, and only once. This step allows you to declare and
initialize any loop control variables.
 Next, the condition is evaluated. If it is true, the body of the loop is executed. If it
is false, the body of the loop does not execute and the flow of control jumps to
the next statement just after the ‘for’ loop.
 After the body of the ‘for’ loop executes, the flow of control jumps back up to
the increment statement. This statement allows you to update any loop control
variables.
 The condition is now evaluated again. If it is true, the loop executes and the
process repeats itself (body of loop, then increment step, and then again
condition).
 After the condition becomes false, the ‘for’ loop terminates.

A.O. Agbeyangi - Chrisland University (CSC) 25


Exercise:

A.O. Agbeyangi - Chrisland University (CSC) 26


do-while Loop
• Unlike for and while loops, which test the loop condition at the top of the
loop, the do-while loop in C programming checks its condition at the
bottom of the loop.
• A do-while loop is similar to a while loop, except the fact that it is
guaranteed to execute at least one time.
• The syntax of a do-while loop in C programming language is:
do
{
statement(s);
} while( condition );
• Note that the conditional expression appears at the end of the loop, so
the statement(s) in the loop executes once before the condition is tested.
A.O. Agbeyangi - Chrisland University (CSC) 27
Exercise:

A.O. Agbeyangi - Chrisland University (CSC) 28


Nested Loop
• C programming allows to use one loop inside another loop.
• The syntax for a nested for loop statement in C is as follows:
Nested for loop Nested while loop Nested do-while loop

for ( init; condition; increment ) while(condition) do


{ { {
for ( init; condition; increment ) while(condition) statement(s);
{ { do
statement(s); statement(s); {
} } statement(s);
statement(s); statement(s); }while( condition );
} } }while( condition );

• 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:

A.O. Agbeyangi - Chrisland University (CSC) 30


Loop Control Statements
• Loop control statements change execution from its normal sequence.
• C supports the following control statements:

A.O. Agbeyangi - Chrisland University (CSC) 31


Infinite Loop
• A loop becomes an infinite loop if a condition never becomes false.
• The for loop is traditionally used for this purpose. Since none of the three
expressions that form the ‘for’ loop are required, you can make an endless loop
by leaving the conditional expression empty.

A.O. Agbeyangi - Chrisland University (CSC) 32


Assignment
1. Write a program to accept input through the keyboard and print out the Name,
Sex, Age and Address.
2. Write a program to compute your first semester GPA. The program will accept
the following inputs:
• Your Name.
• Individual course with title and code.
The program must print out the total number of courses taken, Total number of
Units and GPA.
3. Write a program to compare the value of a number and print out the minimum.
4. Write a program to check the value of a character entered if equal “W” and print
the character.
5. Write a program to print out the odd numbers between 1 and 100.

A.O. Agbeyangi - Chrisland University (CSC) 33

You might also like