Professional Documents
Culture Documents
Control Statement
Objectives
To be able to construct a simple and complex logical
(boolean) expression
To evaluate a given condition
To be able to construct (identify, analyze) selection
and repetition problems
Overview
Statements fall into three general types;
Assignment, where values, usually the results of
calculations, are stored in variables.
Input / Output, data is read in or printed out.
Control, the program makes a decision about what to do
next.
Control Statements
3 types of control statements;
Sequential
Selection
Repetition/loop
In real problems, the solution derived from
combination of above types
One the efficiency aspects of program is how control
structure is developed – run time
Sequential Control Statement
One way directly from top to bottom
#include <stdio.h>
start
void main()
input
{ Basic salary
allowances
float
b_sal,allw,T_inc; Total income = basic salary
+ allowances
scanf(“%f”,&b_sal);
scanf(“%f”,&allw); Print total
income
T_inc = b_sal+allw;
end
printf(“%f”,T_inc);
}
Selection Control Statement
The basic decision statement is two-way selection
which describe either true or false.
C implement two-way selection with the if … else
statement.
Types of Control Statement:
Single Selection
Bi-Selection
Multi-Selection
Rules of if…else
The expression must be enclosed in parentheses.
No semicolon (;) after if…else statement.
Both true or false statements can be any statements or
null statement.
Single-Selection Statement
Logic flow of if … statement
true
condition
false
statement2
Example
It is possible to use the if part without the else.
if (temperature < 0)
printf("Frozen\n");
Bi-Selection Statement
Logic flow of if … else statement
false true
condition
statement1 statement2
Example
The following test decides whether a student has
passed an exam with a pass mark of 50
if (mark >= 50)
printf("Pass\n");
else
printf("Fail\n");
Multi-Selection Statement
Logic flow of Nested if-else Statement
Concept
if(condition1)
{
if(condition2)
{
if(condition3)
statement3;
else
statement2;
}
else
statement1;
}
else
statement0;
Example
a multi-way decision based on several conditions
is called nested if-else.
if (a > b)
if (a > c)
printf(“Biggest : a");
else
printf("Biggest : c ");
else
if (b > c)
printf(“Biggest : b");
else
printf("Biggest : c ");
Multi-Selection Statement
Logic flow of if-else-if Ladder Statement
Concept
if(condition1)
statement1;
else if(condition2)
statement2;
else if(condition3)
statement3;
.
.
.
else if(condition-n)
statement-n;
else
default-statement;
Example
a multi-way decision based on several conditions
is called nested if-else.
if (mark >= 80)
printf("Passed: Grade A\n");
else if (mark >= 60)
printf("Passed: Grade B\n");
else if (mark >= 50)
printf("Passed: Grade C\n");
else
printf("Failed\n");
Example of Multiple
Statements
Having more than one statement following the if or the
else, groups together between curly brackets-compound
statements.
if (mark >= 50)
{
printf("Passed\n");
printf("Congratulations\n");
}
else
{
printf("Failed\n");
printf(“Try again!!!\n");
}
Switch-case statement
Another form of the multi way decision; a well
structured, but can only be used in certain cases
where;
Only one variable is tested, all branches must depend on
the value of that variable. The variable must be an
integral type. (int or char).
Each possible value of the variable can control a single
branch. A final, catch all, default branch may optionally
be used to trap all unspecified cases.
Switch case Statement
Logic flow of switch case statement
Syntax
switch ( <switch variable> )
{ case <constant expression> : <statement>;
[break;]
.
.
.
default : <statement>;
}
Example
switch (selection) {
case 'a' : printf("\nOption a was selected.\n");
break;
int x=1;
while (x<=10)
{
printf(“ %d\n”,x);
x++;
}
Exercise
Using while control statement, write a program that
calculate the total number from 1 to 5.
do … while statement
The do-while loops is similar to while loop, but the
test occurs after the loop body is executed. This
ensures that the loop body is run at least once.
Executes the specified statement until the value of
the specified condition becomes FALSE.
Syntax
do
{
<statement>
} while ( condition );
do-while structure
Example
This will print a number from 1- 10 (10 times
repetition)
int x=1;
do
{
printf(“ %d”,x);
x++;
} while (x<=10);
Exercise
Using do-while control statement, write a program
that calculate the total number from 1 to 5.
For statement
The for loop is frequently used, usually where the loop
will be traversed a fixed number of times. It is very
flexible, and novice programmers should take care not
to abuse.
Executes the specified statement as long as the
condition is TRUE.
Syntax
for ( [initialization] ; [test-
expression] ; [incrementation] )
int x;
for (x=1;x<=10;x++)
{
printf(“%d”,x);
}
Exercise
Using for control statement, write a program that
calculate the total number from 1 to 5.
Example
The initial value (s) in the initialization part
doesn’t have to be zero and the incrementation
value doesn’t have to be 1.
int x;
for (x=2;x<=10;x=x+2)
{ printf(“%d”,x);
}
Exercise
Using a control statement, write a program that
calculate the total number from 1 to 5.
What is the difference between while, do-while and
for ?
Infinite loops
The infinite or never-ending loop can be created using
for(;;) or while(1).
When using infinite loops, be sure to provide an exit
from loop using either break or return statement.
Example
int selection;
for (;;)
{
printf("\nMenu\n");
printf("\n1-Add Record");
printf("\n2-Delete Record");
printf("\n3-Exit");
for(i=1;i<=6;i++)
{
for(j=1;j<=6-i;j++)
{ OUTPUT ???
printf(" ");}
for(j=1;j<=2*i-1;j++)
{
printf("*");
}
printf("\n");
}
}
Exercise
Using for control statement, write a program to display
the following pattern:
*
**
***
****
*****