270 likes | 620 Views
PROGRAMMING LANGUAGE. Lecture #7 CONTROL STRUCTURE & FLOW CHARTS. By Shahid Naseem (Lecturer). LECTURE OUTLINES. LECTURE OUTLINES. IF Statement & Flow Chart IF-ELSE statement & Flow Chart The “NESTED IF” Statement & Flow Chart The “NESTED IF-ELSE” statement & Flow Chart
E N D
PROGRAMMING LANGUAGE Lecture #7 CONTROL STRUCTURE & FLOW CHARTS By ShahidNaseem (Lecturer)
LECTURE OUTLINES • IF Statement & Flow Chart • IF-ELSE statement & Flow Chart • The “NESTED IF” Statement & Flow Chart • The “NESTED IF-ELSE” statement & Flow Chart • The “SWITCH” Statement • The “BREAK” Statement • Difference between “nested if-else” & “Switch” Statements. • The “goto” Statement. Control Structure (Civil Engineering Department)
THE “IF” STATEMENT • 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; Control Structure (Civil Engineering Department)
THE “IF” STATEMENT FALSE TRUE CONDITION Set of statements Statements after if structure Control Structure (Civil Engineering Department)
THE “IF” STATEMENT • 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”; } } Control Structure (Civil Engineering Department)
ASSIGNMENT #2 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. Control Structure (Civil Engineering Department)
THE “IF-ELSE STATEMENT • 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. Control Structure (Civil Engineering Department)
THE “IF-ELSE STATEMENT • Syntax if (condition) { statement-1; statement-2; -------------- statement-n; } else { statement-1; statement-2; } Control Structure (Civil Engineering Department)
THE “IF-ELSE” STATEMENT FALSE TRUE CONDITION Block-2 Block-1 Statements after if structure Control Structure (Civil Engineering Department)
THE “IF-ELSE STATEMENT • 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”; } Control Structure (Civil Engineering Department)
ASSIGNMENT #3 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. Control Structure (Civil Engineering Department)
THE “NESTED-IF” STATEMENT • 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. • Syntax if (condition) { if (condition) { statement-1; } statement-2; } Control Structure (Civil Engineering Department)
THE “NESTED-IF” STATEMENT FALSE TRUE CONDITION-1 TRUE FALSE CONDITION-2 Block-2 Block-1 Next Statement Control Structure (Civil Engineering Department)
THE “NESTED-IF” STATEMENT • 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 () { inta,b,c; cout<<“enter first integer?”; cin>>a; Control Structure (Civil Engineering Department)
THE “NESTED-IF” STATEMENT cout<<“enter second integer?”; cin>>b; cout<<“enter third integer?”; cin>>c; if(a==b ) { if(a==c) cout<<“All values are equal”; } else cout<<“These values are different”; } Control Structure (Civil Engineering Department)
THE “NESTED-IF-ELSE” STATEMENT • 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. • Syntax if (condition-1) statement-1; else if (condition-2) statement-2; else statement-3; Control Structure (Civil Engineering Department)
THE “NESTED-IF-ELSE” STATEMENT TRUE Block-1 CONDITION-1 FALSE TRUE Block-2 CONDITION-2 FALSE Statement after if-else structure Control Structure (Civil Engineering Department)
THE “NESTED-IF-ELSE” STATEMENT • Write a program to perform simple arithmetic operation by using “nested –if-else” structure. #include<iostream.h> Void main () { inta,b; char op; cout<<“enter first integer, operator & second integer/n ”; cout<<“press enter key”; cin>>a>>op>>b; Control Structure (Civil Engineering Department)
THE “NESTED-IF-ELSE” STATEMENT 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); Else cout<<“Invalid input”;} Control Structure (Civil Engineering Department)
THE “SWITCH” STATEMENT • 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. Control Structure (Civil Engineering Department)
THE “SWITCH” STATEMENT • Syntax switch (expression) { case const-1: statements; break; case const-2: statements; break; default: statement; } Control Structure (Civil Engineering Department)
THE “SWITCH” STATEMENT • 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) Control Structure (Civil Engineering Department)
THE “SWITCH” STATEMENT { Case o: cout<<“Divisible by 2”<<endl; Break; case 1: cout<<“Not divisible by 2”<<endl; Break; } Cout<<“ok”<<endl; } Control Structure (Civil Engineering Department)
THE “BREAK” STATEMENT • 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. Control Structure (Civil Engineering Department)
ASSIGNMENT #4 Write a program to perform simple arithmetic operation by using SWITCH STATEMENT Control Structure (Civil Engineering Department)
DIFFERENCE B/W “NESTED-IF-ELSE” AND “SWITCH” STATEMENTS Control Structure (Civil Engineering Department)