0% found this document useful (0 votes)
18 views11 pages

5 Lecture-If Statment

Uploaded by

alswyah17
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)
18 views11 pages

5 Lecture-If Statment

Uploaded by

alswyah17
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
You are on page 1/ 11

Structured program development in C

Before writing a program to solve a particular problem, it is essential to have


athorough understanding of the problem and a carefully planned approach to
solve it.

Algorithms

A procedure for solving a problem in terms of


The actions to be executed, and
The order in which these actions are to be executed

is called an algorithm.

Specifying the order in which statements are to be executed in a computer program is called
program control.
Pseudocode & Flowcharts

Pseudocode is an artificial & informal language that helps


to develop algorithms.

A flowchart is a graphical representation of an algorithm


or a portion of an algorithm.

Control structures

Sequential execution: statements in a program are


executed one after the other in order in which they are
written.

Transfer of control: next statement to be executed may be


other than the next one in sequence.
All programs could be written in terms of only three control
structures:-

sequence structure

selection structure

repetition structure

C provides three selection structures in the form of statements

Single-selection statement (if selection statement) either performs


(selects) an action if a condition is true or skips the action if the
condition is false.

Double-selection statement (if…else selection statement) performs an


action if a condition is true and performs different action if the
condition is false.

Multiple-selection statement (switch selection statement) performsone


of many different actions depending on the value of an expression.
C provides three types of repetition structures in the form of
statements

while
do…while
for

That is all there is!

C has only seven control statements:

Sequence
Three types of selection
Three types of repetition.

Each C program is formed by combining as many of each type


of control statement as is appropriate for the algorithm the
program implements.
The if selection statement
The pseudocode statement:

If student’s grade is greater than or equal to 60


Print “Passed”

The preceding pseudocode If statement may be written in C as

if ( grade >= 60)


cout<<passed;

Flowcharting the single-selection if statement:

true
grade >= 60 print “passed”

false
The if…else selection statement
The pseudocode statement:
If student’s grade is greater than or equal to 60
Print “Passed”
else
Print “Failed”
The preceding pseudocode If…else statement may be written in C as
if ( grade >= 60)
cout<<passed;
else
cout<<Failed;
Flowcharting the double-selection if…else statement:

Printf “failed” false grade >= 60 true Printf “passed”


C provides the conditional operator (?:) which is closely related to
if…else statement

The conditional operator is C’s only ternary operator – it takes three


operands

The operands together with the conditional operator form a


conditional expression

* The first operand is a condition.

* The second operand is the value for the entire conditional


expression if the condition is true.

* The third operand is the value for the entire conditional expression
if the condition is false.

cout<< grade >= 60 ? “Passed” : “Failed” ;

grade >= 60 ? Cout<<Passed; cout<<Failed;


‫ــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ‬
Note: %s conversion specification for printing a character string.
Nested if…else statements

It tests for multiple cases by placing if…else statements inside if…else


statements

The pseudocode statement:

If student’s grade is greater than or equal to 90


cout<<A;
else
If student’s grade is greater than or equal to 80
cout<< B;
else
If student’s grade is greater than or equal to 70
Cout<<C;
else
If student’s grade is greater than or equal to 60
cout<< D;
else
 cout<<F;
This pseudocode may be written in C as

if ( grade >= 90)


cout<<A; if ( grade >= 90)
Else cout<<A;
if ( grade >= 80)
cout<< B; else if ( grade >= 80)
cout<<B;
Else
else if ( grade >= 70)
if ( grade >= 70)
cout<< C; cout<<C;

Else else if ( grade >= 60)

if ( grade >= 60)


cout<<D;
cout<<F;
else
Else cout<<D;
cout<<F;

This is preferred as it avoids deep indentation


A compound statement can be placed anywhere in a program that a single
statement can be placed

Example of a compound statement in the else part of an if…else statement

if ( grade >= 60)


cout<< passed;
else {
cout<< Failed.;
cout<<( “You must take this course again” );
}

*Notice the braces surrounding the two statements in the else statement.
*These braces are important.
*Without the braces, the statement

cout<<( “You must take this course again” );

would be outside the body of the else part of the if, and would execute
regardless of whether the grade is less than 60.

You might also like