You are on page 1of 27

Introduction to C Programming

C Control Structures
CT018-3-1 Introduction To C Programming Control Structure
Topic & Structure of the lesson
In this chapter you will learn about:

Types of Control Structures
Selection Structure
if-else statement
Condition Operator(?:)
switch statement

CT018-3-1 Introduction To C Programming Control Structure
Learning Outcomes
If you have mastered this topic, you should be
able to use the following terms correctly in your
assignments:

switch
If-else
case
default
break
nested
CT018-3-1 Introduction To C Programming Control Structure
Types of Control Structures
A control structure controls the flow of
execution.

A combination of individual instructions into a
with a single logical unit with one entry point
and one exit point.

Three types: sequential, selection and
repetition.
CT018-3-1 Introduction To C Programming Control Structure
Types of Control Structures
Sequential flow is specified by a compound
statement,consisting of group of statements in
a block.(block is delimited by braces{}).
{
statement
1
;
statement
2
;
. . .
statement
;
}
CT018-3-1 Introduction To C Programming Control Structure
Flow chart -Sequence Structure

Statement -1
Statement -2
Statement -n
CT018-3-1 Introduction To C Programming Control Structure
Types of Control Structures

Selection structure allows choice for
among alternative statements.

Repetition structure allows for an action
to be repeated while some condition
remains true.
CT018-3-1 Introduction To C Programming Control Structure
Selection Structure
The selection structure allows a choice in
what statement will be executed next.

C provides three types of selection structures.
if statement
if-else statement
switch statement
CT018-3-1 Introduction To C Programming Control Structure
if statement
The if selection structure either performs
an action if a condition is true or skips the
action if the condition is false.
The general form of the if statement is,
if (expression)
{ statements }
For Example:-
if (grade >= 60)
printf(Passed);
CT018-3-1 Introduction To C Programming Control Structure
if - else statement
The if-else selection structure either
performs an action if a condition is true and
performs a different action if the condition is
false.
The general form of the if-else statement is,
if (expression) {
then-statements
}else {
else-statements
}
CT018-3-1 Introduction To C Programming Control Structure
if - else statement
For Example :-
if (grade >= 60)
printf (Passed);
else
printf(Failed);
CT018-3-1 Introduction To C Programming Control Structure
if - else statement
The Flow Chart representation is,

Grade>=60
Print Failed
Print Passed
true
false
CT018-3-1 Introduction To C Programming Control Structure
if - else statement
if statements with compound statements
if (ctri <= MAX_SAFE_CTRI) {
printf(Car %d:safe\n, auto_id);
safe++;
}
else {
printf(Car %d:unsafe,auto_id);
unsafe++;
}
CT018-3-1 Introduction To C Programming Control Structure
if - else statement
Style:Where to put the braces?

if (condition) {
statements
}
else {
statements
}
if (condition)
{
statements
}
else
{
statements
}
or
CT018-3-1 Introduction To C Programming Control Structure
if - else statement
Removing common statements

if (a < 0) {
neg++;
count++;
scanf (%d, &c);
}
else {
pos++;
count++;
scanf (%d, &c);
}
if (a < 0)
neg++;
else
pos++;

count++;
scanf(%d,&c);
CT018-3-1 Introduction To C Programming Control Structure
Nested if - else statement


if (x > 0)
pos++;
else
if (x < 0)
neg++;
else
zero++;
if (x > 0)
pos++;
else if (x < 0)
neg++;
else
zero++;
CT018-3-1 Introduction To C Programming Control Structure
Switch statement


The switch selection structure performs one of many
different actions depending on the value of an
expression.
Other wise called as multiway selection statement.
The general form of the switch statement is,
switch (expression)
{ case val_1: case_1_statements;break;
CT018-3-1 Introduction To C Programming Control Structure
Switch statement


case val_2:
case_2_statements;break;
.
case val_n:
case_n_statements;break;
default: default_statements; break;
}
CT018-3-1 Introduction To C Programming Control Structure
Switch statement


May only be used to test a constant integral
expression. i.e. the value of the expression type may
be of int or char only.

The breaks statement is used to alter the flow of a
control. In the switch structure, it is used to terminate
a branch.

Default case is optional and is used to handles any
unexpected cases.

CT018-3-1 Introduction To C Programming Control Structure
Flow Chart

Case a Case a action
break
Case b
Case b action
break
Case z
Case z action
break
Default action
T
F
T
F
T
F
CT018-3-1 Introduction To C Programming Control Structure
Example - 1


/* To find odd or even*/
main()
{ int n=5;
switch(n%2){
case 0 :
printf(Even Number);break;
case 1:
printf(Odd Number);break;
}}
CT018-3-1 Introduction To C Programming Control Structure
Example-2


main()
{
char class;
scanf(%c,&class);
switch (class) {
case B:
case b: printf (Battleship\n);
break;
case C:
case c: printf (Cruiser\n);
break;
CT018-3-1 Introduction To C Programming Control Structure
Example-2


case D:
case d: printf (Destroyer\n);
break;
case F:
case f: printf (Frigate\n);
break;
default : printf (Unknown ship class
%c\n, class);
}}
CT018-3-1 Introduction To C Programming Control Structure
SUMMARY
Specifying the order in which statements are
to be executed in a computer programs is
called program control.
A selection structure is used to choose
among alternative courses of action.
The if selection structure executes an
indicated action only when the condition is
true.
CT018-3-1 Introduction To C Programming Control Structure
SUMMARY
The if/else selection structure specifies
separate actions to be executed when the
condition is true and when the condition is
false.
The switch handles a series of decisions in
which a particular variable/expression is
tested.
The break statement causes immediate exit
from the structure.

CT018-3-1 Introduction To C Programming Control Structure
Follow Up Assignment
Read Chapter 4 from the text book.

C: How to program Dietel/Dietel

Answer the questions that follow the chapters.
CT018-3-1 Introduction To C Programming Control Structure
Q & A
Question and Answer Session

You might also like