Download presentation
Presentation is loading. Please wait.
1
Programming Fundamental-1
Instructor Name: Dr. Muhammad Safyan Lecture-5
2
Lecture Outline Decision Statement 2
3
Control Structures A computer can process a program in one of three ways: in sequence; selectively, by making a choice, which is also called a branch; or repetitively, by executing a statement over and over, using a structure called a loop. 3
4
In Sequence
5
Branching(Decision)
6
Decision Making Every programming language provide structure for decision making General Format is if ( condition ) Statement (or group of statements) If the condition is true statement will be executed. If the condition is false statement or group of statement will be skipped. If Ali’s height is greater then 6 feet Then Ali can become a member of the Basket Ball team In every day life, we are often making decisions. We perform different tasks while taking decisions. For example, the statement ‘if the milk shop is open, bring one liter of milk while returning home from college’, involves this phenomenon. In this statement, there is an element of decision making. We bring one litre of milk if the shop is open. And if the shop is closed, we come back to home without milk. Thus we are making a decision on the condition that the shop is open. The decision- making process is everywhere in our daily life. We see that the college gives admission to a student if he has the required percentage in his previous examination and/or in the entry test. Similarly administration of a basketball team of the college e than six f decides that the students having height mor eam t 6
7
If Statement in C If (condition) statement ; If ( condition ) {
: } In every day life, we are often making decisions. We perform different tasks while taking decisions. For example, the statement ‘if the milk shop is open, bring one liter of milk while returning home from college’, involves this phenomenon. In this statement, there is an element of decision making. We bring one litre of milk if the shop is open. And if the shop is closed, we come back to home without milk. Thus we are making a decision on the condition that the shop is open. The decision- making process is everywhere in our daily life. We see that the college gives admission to a student if he has the required percentage in his previous examination and/or in the entry test. Similarly administration of a basketball team of the college e than six f decides that the students having height mor eam t 7
8
cout<<“Student 1 is older than student 2” ;
If Statement in C if (age1 > age2) cout<<“Student 1 is older than student 2” ; In every day life, we are often making decisions. We perform different tasks while taking decisions. For example, the statement ‘if the milk shop is open, bring one liter of milk while returning home from college’, involves this phenomenon. In this statement, there is an element of decision making. We bring one litre of milk if the shop is open. And if the shop is closed, we come back to home without milk. Thus we are making a decision on the condition that the shop is open. The decision- making process is everywhere in our daily life. We see that the college gives admission to a student if he has the required percentage in his previous examination and/or in the entry test. Similarly administration of a basketball team of the college e than six f decides that the students having height mor eam t 8
9
Relational Operators Find the two number are equal or which one is greater 9
10
Relational Operators Find the two number are equal or which one is greater 10
11
Example using namespace std; #include<iostream>
#include <conio.h> main() { int AmirAge, AmaraAge; AmirAge = 0; AmaraAge = 0; cout<<"Please enter Amir’s age"; cin >> AmirAge; cout<<"Please enter Amara’s age"; cin >> AmaraAge; if (AmirAge > AmaraAge) cout << "\n"<< "Amir age is greater then Amara\'s age" ; } getch(); Find the two number are equal or which one is greater 11
12
Example For char values, whether an expression using relational operators evaluates to true or false depends on ASCII value. Find the two number are equal or which one is greater 12
13
Flow Chart Symbols Start or stop Process Flow line Continuation mark
Find the two number are equal or which one is greater Continuation mark Decision 13
14
Flow Chart for if statement
Entry point for IF block IF Condition Then Process Find the two number are equal or which one is greater Exit point for IF block 14
15
if-else if (condition) { statement ; - } else 15
Find the two number are equal or which one is greater 15
16
if-else Entry point for IF-Else block Exit point for IF block IF
Condition Then Process 1 Else Process 2 Exit point for IF block Note indentation from left to right
17
Example Code if (AmirAge > AmaraAge) {
cout<< “Amir is older than Amara” ; } if (AmirAge < AmaraAge) cout<< “Amir is younger than Amara” ;
18
Example Code if AmirAge > AmaraAge) {
cout<< “Amir is older than Amara” ; } else cout<<“Amir is younger than Amara” ;
19
Flow Chart
20
Sample Program 2 A shopkeeper announces a package for customers that he will give 10 % discount on all bills and if a bill amount is greater than 5000 then a discount of 15 %. Write a C ++ program which takes amount of the bill from user and calculates the payable amount by applying the above discount criteria and display it on the screen.
21
Sample Program 2 include <iostream.h> ain ( ) { dou a amo t
netPaya discount = 0 ; // pr cout << "Please enter the amount of the bill " ; cin >> amount ; //test the conditions and calculate net payable if ( amount > 5000 ) //calculate amount at 15 % discount discount = amount * (15.0 / 100); netPayable = amount - discount; cout << "The discount at the rate 15 % is Rupees " << discount << endl; cout << "The payable amount is Rupees " << netPayable ; } else // calculate amount at 10 % discount discount = amount * (10.0 / 100); cout << "The discount at the rate 10 % is Rupees " << discount << endl ; The complete program code is given below: /* This program calculates the discount p w # includ
22
Put him on the chess team
Example If the student age is greater than 18 and his height is greater than five feet then put him on the foot ball team Else Put him on the chess team Find the two number are equal or which one is greater 22
23
Logical Operator (Binary Operator)
Complex condition can be handled with logical operator Logical AND(&&) Logical OR(||) These are binary operators and take two operands. logical expressions as operands, which return TRUE or FALSE if ((interMarks > 45) && (testMarks >= passMarks)) { cout << “ Welcome to GC University”; }
24
Logical Operator (Unary Operator)
!true = false !false = true if ( ! (age > 18 )) cout << “ The age is less than 18”;
25
if(age > 18 || height > 5)
Logical Operators If a is greater than b AND c is greater than d In C++ if(a > b && c> d) if(age > 18 || height > 5) Find the two number are equal or which one is greater 25
26
Logical Operators with if Condition
An applicant are invited on the basis of following two conditions: 4 or more years of college, AND 2 years experience programming in Java OR a grade point average greater than 3.5. A good answer might be: if ( college >= 4 && ( experience >= 2 || gpa > 3.5 ) ) Cout<<"Interview applicant"; else Cout<<"Send resume to circular file."; include <iostream.h> ain ( ) { dou a amo t netPaya discount = 0 ; // pr cout << "Please enter the amount of the bill " ; cin >> amount ; //test the conditions and calculate net payable if ( amount > 5000 ) //calculate amount at 15 % discount discount = amount * (15.0 / 100); netPayable = amount - discount; cout << "The discount at the rate 15 % is Rupees " << discount << endl; cout << "The payable amount is Rupees " << netPayable ; } else // calculate amount at 10 % discount discount = amount * (10.0 / 100); cout << "The discount at the rate 10 % is Rupees " << discount << endl ; The complete program code is given below: /* This program calculates the discount p w # includ
27
Precedence Logical Operators with if Condition
include <iostream.h> ain ( ) { dou a amo t netPaya discount = 0 ; // pr cout << "Please enter the amount of the bill " ; cin >> amount ; //test the conditions and calculate net payable if ( amount > 5000 ) //calculate amount at 15 % discount discount = amount * (15.0 / 100); netPayable = amount - discount; cout << "The discount at the rate 15 % is Rupees " << discount << endl; cout << "The payable amount is Rupees " << netPayable ; } else // calculate amount at 10 % discount discount = amount * (10.0 / 100); cout << "The discount at the rate 10 % is Rupees " << discount << endl ; The complete program code is given below: /* This program calculates the discount p w # includ
28
Logical Operators Precedence
For example, say that A, B, C, and D stand for relational expressions (things like 23 > 90). Then, A || B && C means A || (B && C) A && B || C && D (A && B) || (C && D) A && B && C || D ((A && B) && C) || D !A && B || C ((!A) && B) || C include <iostream.h> ain ( ) { dou a amo t netPaya discount = 0 ; // pr cout << "Please enter the amount of the bill " ; cin >> amount ; //test the conditions and calculate net payable if ( amount > 5000 ) //calculate amount at 15 % discount discount = amount * (15.0 / 100); netPayable = amount - discount; cout << "The discount at the rate 15 % is Rupees " << discount << endl; cout << "The payable amount is Rupees " << netPayable ; } else // calculate amount at 10 % discount discount = amount * (10.0 / 100); cout << "The discount at the rate 10 % is Rupees " << discount << endl ; The complete program code is given below: /* This program calculates the discount p w # includ
29
Logical and Relational Operators Precedence
30
Logical Operators Precedence
Add parentheses to the following to show how operator precedence groups operands: a > b && 45 <= sum || sum < a + b && d > 90 include <iostream.h> ain ( ) { dou a amo t netPaya discount = 0 ; // pr cout << "Please enter the amount of the bill " ; cin >> amount ; //test the conditions and calculate net payable if ( amount > 5000 ) //calculate amount at 15 % discount discount = amount * (15.0 / 100); netPayable = amount - discount; cout << "The discount at the rate 15 % is Rupees " << discount << endl; cout << "The payable amount is Rupees " << netPayable ; } else // calculate amount at 10 % discount discount = amount * (10.0 / 100); cout << "The discount at the rate 10 % is Rupees " << discount << endl ; The complete program code is given below: /* This program calculates the discount p w # includ (a > b) && (45 <= sum )|| sum < (a + b) && (d > 90)
31
Else If Else if is a condition used for multiple choices
Else if chooses exactly one or none statement from the multiple choices (other wise logical error may occur). General format of else if statement If (condition) Statement of Group of statements Else if (condition) . Else include <iostream.h> ain ( ) { dou a amo t netPaya discount = 0 ; // pr cout << "Please enter the amount of the bill " ; cin >> amount ; //test the conditions and calculate net payable if ( amount > 5000 ) //calculate amount at 15 % discount discount = amount * (15.0 / 100); netPayable = amount - discount; cout << "The discount at the rate 15 % is Rupees " << discount << endl; cout << "The payable amount is Rupees " << netPayable ; } else // calculate amount at 10 % discount discount = amount * (10.0 / 100); cout << "The discount at the rate 10 % is Rupees " << discount << endl ; The complete program code is given below: /* This program calculates the discount p w # includ
32
Else If if ( studentGrade >= 90 ) // 90 and above gets "A"
cout << "A"; else if ( studentGrade >= 80 ) // gets "B“ cout << "B"; else if ( studentGrade >= 70 ) // gets "C" cout << "C"; else if ( studentGrade >= 60 ) // gets "D" cout << "D"; Else // less than 60 gets "F" cout << "F"; include <iostream.h> ain ( ) { dou a amo t netPaya discount = 0 ; // pr cout << "Please enter the amount of the bill " ; cin >> amount ; //test the conditions and calculate net payable if ( amount > 5000 ) //calculate amount at 15 % discount discount = amount * (15.0 / 100); netPayable = amount - discount; cout << "The discount at the rate 15 % is Rupees " << discount << endl; cout << "The payable amount is Rupees " << netPayable ; } else // calculate amount at 10 % discount discount = amount * (10.0 / 100); cout << "The discount at the rate 10 % is Rupees " << discount << endl ; The complete program code is given below: /* This program calculates the discount p w # includ
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.