You are on page 1of 29

# Analytical and Computational Methods in Civil Engineering I

## Lecture 3 Control Structures in C

Elvin B. Cruz
Lecturer II
Institute of Civil Engineering

Assignment Operators

## CE 26 Lecture 3 Control Structures in C

Increment/Decrement Operators

## CE 26 Lecture 3 Control Structures in C

Control Structures in C
Enable the programmer to control the flow of
the program and the sequence by which the
inputted commands are to be executed
Types:
1. Sequence Structure
Statements are executed in the order by which they are written

2. Selection Structure
if, if-else statements, switches

## 3. Repetition (Recursive) Structure

for, while, do-while
CE 26 Lecture 3 Control Structures in C

Selection Structures
If, if-else, nested if-else, switch

## CE 26 Lecture 3 Control Structures in C

Selection Structures
1. if statement
- single-selection structure

2. if-else statement
- double-selection structure

3. switch
- multiple-selection structure

## CE 26 Lecture 3 Control Structures in C

if Selection Structure
Allows a program to execute a (set of)
command(s) if the relational condition statement
is satisfied
Syntax:
if (condition statement)
{
body statement;
}
CE 26 Lecture 3 Control Structures in C

if Selection Structure

## If-else Selection Structure

Instructs the program what statements to execute if
the condition statement is satisfied and if it is not
Syntax:
if (condition statement)
{
body statement1;
}
else
{
body statements2;
}
CE 26 Lecture 3 Control Structures in C

## If, if-else, Nested if-else

NOTE:
The first statement directly following these
control structures are executed when the
conditional statement is satisfied.
To include several statements to be executed,
contain them within a pair of braces.

## Switch Selection Structure

Branches of decisions
Evaluates relational statements involving
types int and char only (case sensitive)
Efficiently substitutes for a nested if-else
statements when execution of one of many
alternatives is needed.
Commonly uses break and goto to terminate
execution of statements
CE 26 Lecture 3 Control Structures in C

## Switch Selection Structure

switch ( integer/char conditional expression )
{
case constant1:
statements;
break;
case constant2:
statements;
break;
........................................
default:
statements;
}
CE 26 Lecture 3 Control Structures in C

Using break

Using goto

Using break

Using goto

## CE 26 Lecture 3 Control Structures in C

Repetition Structures
(Loops)
for, while,do-while

## CE 26 Lecture 3 Control Structures in C

Repetition Structures
Means of Repetition:
1. Counter-controlled (definite repetition)
Counter-controlled variable counts the
number of specified repetitions
2. Sentinel-controlled (indefinite repetition)
Sentinel serves as the lookout/guard since
the no. of repetitions is unknown or is
dependent on data that will be inputted by
the user
CE 26 Lecture 3 Control Structures in C

Repetition Structures
1. for
-repeats a statement or compound statements a
specified number of times

2. while
-executes a statement or a block of statements until a
specified expression evaluates to false.

3. do-while
-similar with while except that the condition is evaluated
after each execution of the loop; executes the
statement at least once
CE 26 Lecture 3 Control Structures in C

## for Repetition Structure

Automatically handles a counter-controlled
repetition structure
Syntax:
for (initialization; test condition; increment)
{
statement(s);
}
CE 26 Lecture 3 Control Structures in C

## while Repetition Structure

The program executes the statement in the body of the while
statement until the condition statement is not satisfied

Syntax
initialization;
while(test condition)
{
compound statement;
increment;
}

## while Repetition Stucture

Sample of sentinel-controlled repetition

## do-while Repetition Structure

The condition statement is evaluated at the end of
every iteration
Syntax:
Initialization;
do
{
statements;
Decrement/increment of counter;
}
while (condition);
CE 26 Lecture 3 Control Structures in C

## CE 26 Lecture 3 Control Structures in C

References:
Lecture Notes and Presentations by Dr. Eric
Tingatinga and Rahf Alvarez
C How to Program: Introducing C++ and Java,
3rd Ed. By Deitel&Deitel.