0% found this document useful (0 votes)
37 views28 pages

Lecture 3-SelectionStatements (Part 1)

The document discusses selection statements in structured programming using C language. It describes if and if-else statements. An if statement executes code based on a condition being true, while if-else executes one block of code if true and another if false. Examples show calculating grades, discounts, and electricity bills using if and if-else. The ternary operator provides an alternate way to write conditional logic. Well-indented code and flowcharts help illustrate control flow.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views28 pages

Lecture 3-SelectionStatements (Part 1)

The document discusses selection statements in structured programming using C language. It describes if and if-else statements. An if statement executes code based on a condition being true, while if-else executes one block of code if true and another if false. Examples show calculating grades, discounts, and electricity bills using if and if-else. The ternary operator provides an alternate way to write conditional logic. Well-indented code and flowcharts help illustrate control flow.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

COMSATS University

Islamabad
(Lahore Campus)

Lecture 3: Structured
Program Development
(Selection Statements)
Outline
 Control Structure
 If statements
 If else statement
 Conditional Operator

COMSATS University Islamabad, Lahore Campus 2


Control Structures
• Sequential execution
• Statements executed one after the other in the order written
• Transfer of control
• When the next statement executed is not the next one in sequence
• Overuse of goto statements led to many problems
• Structured programming – ‘goto elimination’
• Bohm and Jacopini
• All programs written in terms of 3 control structures (7 Control
Statements)
• Sequence structures: Built into C. Programs executed sequentially by
default
• Selection structures: C has three types: if, if/else, and switch
• Repetition structures: C has three types: while, do/while and for

COMSATS University Islamabad, Lahore Campus Slide 3


If Statements
The ‘if’ Selection Statement
• Selection structure:
• Used to choose among alternative courses of action
• Pseudocode:
If student’s grade is greater than or equal to 60
Print “Passed”
• If condition true
• Print statement executed and program goes on to next statement
• If false, print statement is ignored and the program goes onto the next
statement

COMSATS University Islamabad, Lahore Campus Slide 5


The ‘if’ Selection Statement
• Pseudocode statement in C:
if ( grade >= 60 )
printf ( "Passed\n" );
• C code corresponds closely to the pseudocode
• Indenting makes programs easier to read
• C ignores whitespace characters

• Diamond symbol (decision symbol)


• Indicates decision is to be made
• Contains an expression that can be true or false
• Test the condition, follow appropriate path

COMSATS University Islamabad, Lahore Campus Slide 6


The ‘if’ Selection Statement
• if structure is a single-entry/single-exit structure

A decision can be made


on any expression.
zero - false
true nonzero - true
grade >= 60 print “Passed” Example:
3 - 4 is true

false

COMSATS University Islamabad, Lahore Campus Slide 7


The Selection Statement
• Block:
• A set of statements contained within a pair of braces is called compound
statements or a block

• Syntax errors
• Caught by compiler

• Logic errors:
• Have their effect at execution time
• Non-fatal: program runs, but has incorrect output
• Fatal: program exits prematurely

COMSATS University Islamabad, Lahore Campus Slide 8


Simple if statement
• Syntax
if (condition)
Statement;

Condition: specifies a condition or relational expression


• Syntax of if condition executing a set of statements
If (condition)
{
statement-1;
statement-2;
……..
}
Note: Any non-zero value is TRUE

COMSATS University Islamabad, Lahore Campus 9


False True
Condition

Set of Statements

Statements after
if Structure

Flowchart: The “if statement


COMSATS University Islamabad, Lahore Campus 10
Example
/* Program executes a single statement if the given statement is TRUE */
#include<stdio.h>
int main()
{
int a = 100, b = 50;

if(a>b)
printf(“Lahore\n”);

printf(“OK\n”);
return 0;
}

COMSATS University Islamabad, Lahore Campus 11


Examples Cont.

/* Program takes input a number from user. If number is divisible by 3 then print the message on the screen
that ‘the number is divisible by three*/
#include<stdio.h>
int main()
Output
{
int n = 0;
Enter a number: 9
printf(“Enter a Number : ”); Number 9 is divisible by 3
scanf(“%d”,&n); OK
if(n%3==0)
{
printf(“Number %d”, n);
printf(“ is divisible by 3\n”);
}
printf(“OK\n”);
return 0;
}

COMSATS University Islamabad, Lahore Campus 12


Example

• While purchasing certain items, a discount of 10% is offered if the


quantity purchased is more than 1000. If quantity and price per item
are input through the keyboard, write a program to calculate the
total expenses.

COMSATS University Islamabad, Lahore Campus 13


Flow Chart

COMSATS University Islamabad, Lahore Campus 14


1. /* Calculation of total expenses */ Output
2. #include <stdio.h> Here is some sample interaction with
3. int main( ) the program.
4. { Enter quantity and rate 1200 15.50
5. int qty, dis = 0 ; Total expenses = Rs. 16740.000000
6. float rate, tot ; Enter quantity and rate 200 15.50
7. printf ( "Enter quantity and rate " ) ; Total expenses = Rs. 3100.000000
8. scanf ( "%d %f", &qty, &rate) ;
9. if ( qty > 1000 )
1. dis = 10 ;
10. tot = ( qty * rate ) - ( qty * rate *
dis / 100 ) ;
11. printf ( "Total expenses = Rs. %f",
tot ) ;
12. return 0;
13. }

COMSATS University Islamabad, Lahore Campus 15


if ( expression )
statement ;
Here the expression can be any valid expression including a relational
expression. We can even use arithmetic expressions in the if statement.
For example all the following if statements are valid
if ( 3 + 2 % 5 )
printf ( "This works" ) ;
if ( a = 10 )
printf ( "Even this works" ) ;
if ( -5 )
printf ( "Surprisingly even this works" ) ;
Note that in C a non-zero value is considered to be true, whereas a 0 is
considered to be false. In the first if, the expression evaluates to 5 and
since 5 is non-zero it is considered to be true. Hence the printf( ) gets
executed.

COMSATS University Islamabad, Lahore Campus 16


if-else statement
 Syntax
if (condition)
Statement-1;
else
Statement-2;

Condition: specifies a condition or relational expression


 Syntax of if-else condition executing a set of
statements
If (condition)
{
statement-1;
statement-2;
……..
}
else
{
statement-1;
statement-2;
……..
}
Note: Any non-zero value is TRUE
COMSATS University Islamabad, Lahore Campus 17
False True
Condition

Block-2 Block-1

Statements after
if Structure

Flowchart: The “if-else statement

COMSATS University Islamabad, Lahore Campus 18


The if…else selection statement
if
Only performs an action if the condition is true
if…else
Specifies an action to be performed both when the condition is true and when it
is false
Psuedocode:
If student’s grade is greater than or equal to 60
Print “Passed”
else
Print “Failed”
Note spacing/indentation conventions

COMSATS University Islamabad, Lahore Campus 19


The if…else selection statement
C code:
if ( grade >= 60 )
printf( "Passed\n");
else
printf( "Failed\n");
Ternary conditional operator (?:)
Takes three arguments (condition, value if true, value if false)
Our pseudocode could be written:
printf( "%s\n", grade >= 60 ? "Passed" : "Failed" );
Or it could have been written:
grade >= 60 ? printf( “Passed\n” ) : printf( “Failed\n” );

COMSATS University Islamabad, Lahore Campus 20


Ternary conditional operator (?:)
We can use it to assign values on a condition base
• Our code could be written:
no = x<10 ? 20 : 10;

COMSATS University Islamabad, Lahore Campus 21


Example

• Write a program to calculate the electricity bill. The rates of


electricity per unit are as follow:
• If the units consumed are equal or less than 300,then the cost is Rs. 3/- per
unit
• If units consumed are more than 300,then the cost is Rs. 3.5/- per unit and
surcharge of 5% of bill is added

COMSATS University Islamabad, Lahore Campus 22


Solution
#include<stdio.h>
#include<conio.h>
int main
{
float units=0, bill=0,rate,surcharge;
printf(“\n Enter units : ”);
scanf(“%f”, &units);
if(units<300){
rate = 3.0;
surcharge = 0.0;
}
else{
rate = 3.5;
surcharge = 0.05;
}
bill = units* rate + (units*rate*surcharge);
printf(“\nElectricity bill = %f”,bill);
return 0;
COMSATS University Islamabad, Lahore Campus 23
}
Example : In a company an employee is paid as under:
If his basic salary is less than Rs. 1500, then HRA (House Rent
Allowance) = 10% of basic salary and DA (Daily Allowance) = 90% of
basic salary. If his salary is either equal to or above Rs. 1500, then HRA
= Rs. 500 and DA = 98% of basic salary. If the employee's salary is input
through the keyboard write a program to find his gross salary.

COMSATS University Islamabad, Lahore Campus 24


No Yes

COMSATS University Islamabad, Lahore Campus 25


/* Calculation of gross salary */
#include <stdio.h>

int main( )
{
float bs, gs, da, hra ;
printf ( "Enter basic salary " ) ;
scanf ( "%f", &bs ) ;

if ( bs < 1500 )
{
hra = bs * 10 / 100 ;
da = bs * 90 / 100 ;
}
else
{
hra = 500 ;
da = bs * 98 / 100 ;
}
gs = bs + hra + da ;
printf ( "gross salary = Rs. %f", gs ) ;

return 0;
}

COMSATS University Islamabad, Lahore Campus 26


Example

• Program takes a number from user and prints greater if the number is > 100 else prints
less or equal to
#include<stdio.h>
int main()
{
int n;
printf(“Enter an integer : ”);
scanf(“%d”,&n);

If(n>100)
printf(“Number is greater than 100”);
else
printf(“Number is less than or equal to100”);
return 0;
}

COMSATS University Islamabad, Lahore Campus 27


Thanks

COMSATS University Islamabad, Lahore Campus 28

You might also like