4. possible to perform mathematical operation on character values
Sequence
Executes the statements in the same order in which they are written in the program
Entry point
Exit point
Flowchart
Copyright @ IT Series www.itseries.com.pk
5. possible to perform mathematical operation on character values
Selection
A control structure which chooses alternative to execute
Executes a statement or set of statements based on a condition
Also called decision-making structure or conditional structure.
Different types of selection structures in C language:
If
if-else
if-else-if
switch
Entry point
Exit point
Flowchart
Copyright @ IT Series www.itseries.com.pk
6. possible to perform mathematical operation on character values
Repetition
A control structure which executes a statement or set of statements repeatedly for a specific number of times
Also called iteration structure or loop
Different types of loops available in C language:
while loop
do-while loop
for loop
Entry point
Exit point
Flowchart
Copyright @ IT Series www.itseries.com.pk
7. possible to perform mathematical operation on character values
The if statement is the primary selection control structure
It is used to execute or skip a statement or set of statements by checking a condition
The condition is given as a relational expression. e. g marks >=40
Syntax / General Form
The syntax of if statement for single statement:
if (condition)
statement;
The syntax for compound statements:
if (condition)
{
statement 1;
statement 2;
statement N;
}
if is a keyword in C
language. It is always
written in lowercase.
Block of statements
inside the braces is
called the body of the if
statement. If there is
only 1 statement in the
body, the { } may be
omitted.
Do not place ; after (condition)
Copyright @ IT Series www.itseries.com.pk
8. Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
If Programs
Copyright @ IT Series www.itseries.com.pk
10. possible to perform mathematical operation on character values
The simplest selection structure but it is very limited in its use
Statement or set of statements is executed if the condition is true
But if the condition is false then nothing happens (no alternate action is performed)
A user may want to:
Execute one statement or set of statements if the condition is true
Execute other statement or set of statements if the condition is false
In this situation, simple if statement cannot be used effectively
Solution ‘if-else’ structure can be used to handle this kind of situation effectively
Example
A program should display Pass! if the student gets 40 or more marks
It should display Fail! if the student gets less than 40 marks
Simple if statement cannot be used to handle this situation
Copyright @ IT Series www.itseries.com.pk
20. Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
Nested if Programs
21. Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
22. Select one of several alternatives when selection is based on the value of a single variable or an expression
In C, the value of this expression may be of type int or char
The switch statement is a better way of writing a program when a series of if-else-if occurs
Syntax / General Form
Copyright @ IT Series www.itseries.com.pk
23. Rules of using switch case in C program
• The case label must integer or character
• Each case label must be unique
• A switch statement can have only one default label
• The default label can be used anywhere in switch statement
• The case label must end with colon. The default label is optional
Copyright @ IT Series www.itseries.com.pk
25. Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
26. possible to perform mathematical operation on character values
Copyright @ IT Series www.itseries.com.pk
27. Conditional operator is a decision-making or selection control structure
It can be used in place of simple if-else structure
It is also called ternary operator as it uses three operands
Syntax
(condition) ? true : false;
condition The condition is specified as relational or logical expression
true It is executed if expression evaluates to true
false It is executed if expression evaluates to false
Example
X = (A>50) ? 1 : 0;
The above statement can be written using if-else statement:
if (A>50)
X = 1;
else
X = 0;
Copyright @ IT Series www.itseries.com.pk