0% found this document useful (0 votes)
763 views29 pages

Lecture 3 - Selection and Repetition Structures

This document summarizes a lecture on control structures in C programming. It discusses sequence, selection, and repetition structures. Selection structures covered include if, if-else, and switch statements. Repetition structures covered include for, while, and do-while loops. The purpose of control structures is to control the flow and order of program execution. Examples are provided for each control structure.

Uploaded by

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

Lecture 3 - Selection and Repetition Structures

This document summarizes a lecture on control structures in C programming. It discusses sequence, selection, and repetition structures. Selection structures covered include if, if-else, and switch statements. Repetition structures covered include for, while, and do-while loops. The purpose of control structures is to control the flow and order of program execution. Examples are provided for each control structure.

Uploaded by

aef
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Analytical and Computational Methods in Civil Engineering I

2nd Semester A.Y. 2014-2015

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

CE 26 Lecture 3 Control Structures in C

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-else Selection Structure

CE 26 Lecture 3 Control Structures in C

Nested if-else Selection Structure

CE 26 Lecture 3 Control Structures in C

Nested if-else Selection Structure

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.

CE 26 Lecture 3 Control Structures in C

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

Switch Selection Structure


Using break

CE 26 Lecture 3 Control Structures in C

Switch Selection Structure


Using goto

CE 26 Lecture 3 Control Structures in C

Switch Selection Structure


Using break

Switch Selection Structure


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

for Repetition Structure

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;
}

CE 26 Lecture 3 Control Structures in C

while Repetition Stucture

CE 26 Lecture 3 Control Structures in C

while Repetition Stucture


Sample of sentinel-controlled repetition

CE 26 Lecture 3 Control Structures in C

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

do-while Repetition Structure

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.

CE 26 Lecture 3 Control Structures in C

You might also like