lecture #7 control structure & flow charts by shahid naseem (lecturer)

27
PROGRAMMING LANGUAGE Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

Upload: marlene-stone

Post on 04-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

PROGRAMMING LANGUAGE

Lecture #7

CONTROL STRUCTURE & FLOW CHARTS

ByShahid Naseem

(Lecturer)

Page 2: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

LECTURE OUTLINES

Page 3: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

3

LECTURE OUTLINESIF Statement & Flow ChartIF-ELSE statement & Flow ChartThe “NESTED IF” Statement & Flow ChartThe “NESTED IF-ELSE” statement & Flow

ChartThe “SWITCH” StatementThe “BREAK” StatementDifference between “nested if-else” &

“Switch” Statements.The “goto” Statement.

Control Structure (Civil Engineering Department)

Page 4: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

4

THE “IF” STATEMENT

Control Structure (Civil Engineering Department)

The “IF” statement is used to execute a set of statements after testing a condition.

The “IF” statement evaluates a condition , if the given condition is true, the statement following the “if statement” is executed.

If the condition is false, the statement following the “if statement” condition is ignored and the control transfers to the next statement.

Syntax if(condition)

statement-1;statement-2;

Page 5: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

5

THE “IF” STATEMENT

Control Structure (Civil Engineering Department)

Set of statements

Statements after if

structure

TRUEFALSE

CONDITION

Page 6: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

6

THE “IF” STATEMENT

Control Structure (Civil Engineering Department)

Write a program to input a number. If the number is divisible by 3 then print the message on the screen that the number is divisible by 3. Use “IF statement”.#include<iostream.h>void main(){

int n;cout<<“enter a number?”;cin>>n;if(n%3==0){ cout<<“the number”<<“is divisible

by 3”;}

}

Page 7: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

7

ASSIGNMENT #2

Control Structure (Civil Engineering Department)

Write a program to calculate the electricity bill. The rates of electricity per units are as follow.1. If the units consumed are equal or less than 300, then the cost is Rs. 3/- per unit.

2. If the units consumed are more than 300, then the cost is Rs.3.5/- per unit and a surcharge of 5% of bill is added.

Page 8: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

8

THE “IF-ELSE STATEMENT

Control Structure (Civil Engineering Department)

The “IF-ELSE statement” is used for making two-way decisions. In this statement, one condition and two blocks of statements are given. Either one of the two blocks of statements is executed after evaluating a condition.

The “IF-ELSE” statement tests the given relational condition.

If the condition is true then the first block of statements is executed else the other statements if the condition is false.

Page 9: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

9

THE “IF-ELSE STATEMENT

Control Structure (Civil Engineering Department)

Syntaxif (condition){

statement-1;statement-2;--------------statement-n; }

else { statement-1;

statement-2; }

Page 10: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

10

THE “IF-ELSE” STATEMENT

Control Structure (Civil Engineering Department)

Block-1

Statements after if

structure

TRUEFALSE

CONDITION

Block-2

Page 11: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

11

THE “IF-ELSE STATEMENT

Control Structure (Civil Engineering Department)

Write a program to input a number from the keyboard. Use IF-ELSE statement to find out whether the number is less than or greater than 100.

#include<iostream.h>void main (){

int n;cout<<“enter an integer value?”;cin>>n;

if (n>100)cout<<“number is greater than

100”;else

cout<<“number is less than 100”;}

Page 12: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

12

ASSIGNMENT #3

Control Structure (Civil Engineering Department)

Write a program to calculate the “NET PAY” of an employee. Input the basic pay and calculate the “NET PAY” and calculate the NET PAY as follows.

House Rent is 45% of the basic pay.Medical allowance is 2% of basic if basic

is greater than Rs. 5000. It is 5% of basic pay if the pay is less than Rs.5000.

Conveyance allowance is Rs.96/- if basic pay is less than Rs. 5000. It is Rs.193/- if the basic pay is more than Rs.5000/-

Net Pay is calculated by adding basic pay, medical allowance, conveyance allowance and house rent.

Page 13: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

13

THE “NESTED-IF” STATEMENT

Control Structure (Civil Engineering Department)

When an “IF statement” is used within another “IF statement”, it is called the “nested if statement”.

The “nested if statement” is used for multi-way decision making.

Syntaxif (condition){

if (condition){

statement-1;}statement-2;

}

Page 14: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

14

THE “NESTED-IF” STATEMENT

Control Structure (Civil Engineering Department)

Block-1

Next Statement

TRUEFALSE

CONDITION-1

Block-2

FALSETRUE

CONDITION-2

Page 15: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

15

THE “NESTED-IF” STATEMENT

Control Structure (Civil Engineering Department)

Write a program to input three integer values. Compare the three values to find out if they are equal. Use “Nested if statement” and print the message “ all values are equal” if they are equal. Otherwise print the message “These values are Different”.

#include<iostream.h>Void main (){

int a,b,c;cout<<“enter first integer?”;cin>>a;

Page 16: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

16

THE “NESTED-IF” STATEMENT

Control Structure (Civil Engineering Department)

cout<<“enter second integer?”;cin>>b;cout<<“enter third integer?”;cin>>c;

if(a==b ){

if(a==c)cout<<“All values are equal”;

}elsecout<<“These values are different”;

}

Page 17: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

17

THE “NESTED-IF-ELSE” STATEMENT

Control Structure (Civil Engineering Department)

When an “if-else” structure is placed in another “if-else “structure, it is called “nested-if-else” structure. It is used for multiple selection.

Syntaxif (condition-1)statement-1;else if (condition-2)statement-2;elsestatement-3;

Page 18: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

18

THE “NESTED-IF-ELSE” STATEMENT

Control Structure (Civil Engineering Department)

Block-1

Statement after if-else structure

TRUE

FALSE

CONDITION-1

Block-2

FALSE

TRUE

CONDITION-2

Page 19: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

19

THE “NESTED-IF-ELSE” STATEMENT

Control Structure (Civil Engineering Department)

Write a program to perform simple arithmetic operation by using “nested –if-else” structure.

#include<iostream.h>Void main (){

int a,b;char op;cout<<“enter first integer, operator & second integer/n ”;cout<<“press enter key”;cin>>a>>op>>b;

Page 20: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

20

THE “NESTED-IF-ELSE” STATEMENT

Control Structure (Civil Engineering Department)

If (op==‘+’)cout<<“Addition=“<<(a+b);

Elseif (op==‘-’)cout<<“Subtraction=“<<(a-b);

Elseif (op==‘*’)cout<<“Multiplication=“<<(a*b);

Else if (op=‘/’)cout<<“Division=“<<(a/b);

Else if (op=‘%’)cout<<“Remainder=“<<(a%b);

Elsecout<<“Invalid input”; }

Page 21: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

21

THE “SWITCH” STATEMENT

Control Structure (Civil Engineering Department)

The “Switch” statement, is used as a substitute of “Nested-if-else statements”.

It is used when multiple choices are given and one choice is to be selected.

The “nested-if-else” structure becomes complicated in multiple choices.

The “Switch Statement” is used in such situations.

Only one condition is given in the “switch statement” and multiple choices are given inside the main body.

Page 22: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

22

THE “SWITCH” STATEMENT

Control Structure (Civil Engineering Department)

Syntaxswitch (expression)

{case const-1:

statements;break;case const-2:

statements;break;default:

statement; }

Page 23: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

23

THE “SWITCH” STATEMENT

Control Structure (Civil Engineering Department)

Write a program to input an integer value. Test the integer value if the value is divisible by 2, then print the message “Divisible by 2” otherwise “Not divisible by 2” by using switch statement.

#include<iostream.h>Void main (){

int n;cout<<“enter any value”<<endl;cin>>n;

Switch(n%2)

Page 24: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

24

THE “SWITCH” STATEMENT

Control Structure (Civil Engineering Department)

{Case o:

cout<<“Divisible by 2”<<endl;Break;case 1:

cout<<“Not divisible by 2”<<endl;Break;}Cout<<“ok”<<endl;}

Page 25: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

25

THE “BREAK” STATEMENT

Control Structure (Civil Engineering Department)

The “BREAK” statement is used to exit from the body of the switch structure.

In the switch statement, the break statement is normally used at the end of statements in each case.

It exits the control from the body of switch structure.

If it is not used then the statements of other cases that come after the matching case will also be exectued.

Page 26: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

26

ASSIGNMENT #4

Control Structure (Civil Engineering Department)

Write a program to perform simple arithmetic operation by using SWITCH STATEMENT

Page 27: Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By Shahid Naseem (Lecturer)

27

DIFFERENCE B/W “NESTED-IF-ELSE” AND “SWITCH” STATEMENTS

Control Structure (Civil Engineering Department)

NESTED IF-ELSE STATEMENT

SWITCH STATEMENT

i. It becomes complicated for multiple selections.

It is easy to understand for multiple selections.

ii. It uses an independent expression for each case.

It uses a single expression for all cases, but each case must have a constant value of integer type or character type.

iii. The test condition can be given in a special range of value. If the given condition matches then the statements under it will be executed.

Only a single expression is given in the switch statement which returns a single value. The test condition cannot be given in a specified range. It is drawback.