Programming Fundamental-1

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 5. In the Previous Lecture Basic structure of C program Basic structure of C program Variables and Data types Variables.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
If Statements & Relational Operators Programming.
True or false A variable of type char can hold the value 301. ( F )
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
The Ohio State University
Branching statements.
Introduction to Computer Programming
Chapter 4: Control Structures I (Selection)
Chapter 3 Control Statements
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II (Repetition)
Chapter 4: Control Structures I
Computing Fundamentals
Programming Fundamental
Basic Elements of C++.
Operator Precedence Operators Precedence Parentheses () unary
Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point.
Boolean Expressions and If
Control Structures II (Repetition)
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
C# and the .NET Framework
Control Structures – Selection
Basic Elements of C++ Chapter 2.
Conditionals & Boolean Expressions
Chapter 4: Control Structures I
Chapter 4: Control Structures I (Selection)
Compound Assignment Operators in C++
Introduction to Programming
Programming Funamental slides
Executes a block of statements only if a test is true
Conditional Construct
Programming Funamental slides
Chapter 7 Conditional Statements
Flow Control Statements
Selection Control Structure
Chapter 4 Selection.
Summary Two basic concepts: variables and assignments Basic types:
Computing Fundamentals
Chapter 4: Control Structures I (Selection)
CS150 Introduction to Computer Science 1
Control Structures Part 1
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Life is Full of Alternatives
Chapter 5: Control Structures II (Repetition)
Life is Full of Alternatives
Chapter 3: Selection Structures: Making Decisions
CS 101 First Exam Review.
HNDIT11034 More Operators.
Selection Control Structure
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Programming Fundamental
Control Structures.
Programming Fundamental
Presentation transcript:

Programming Fundamental-1 Instructor Name: Dr. Muhammad Safyan Lecture-5

Lecture Outline Decision Statement 2

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

In Sequence

Branching(Decision)

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

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

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

Relational Operators Find the two number are equal or which one is greater 9

Relational Operators Find the two number are equal or which one is greater 10

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

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

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

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

if-else if (condition) { statement ; - } else 15 Find the two number are equal or which one is greater 15

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

Example Code if (AmirAge > AmaraAge) { cout<< “Amir is older than Amara” ; } if (AmirAge < AmaraAge) cout<< “Amir is younger than Amara” ;

Example Code if AmirAge > AmaraAge) { cout<< “Amir is older than Amara” ; } else cout<<“Amir is younger than Amara” ;

Flow Chart

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.

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

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

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”; }

Logical Operator (Unary Operator) !true = false !false = true if ( ! (age > 18 )) cout << “ The age is less than 18”;

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

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

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

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

Logical and Relational Operators Precedence

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)

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

Else If if ( studentGrade >= 90 ) // 90 and above gets "A" cout << "A"; else if ( studentGrade >= 80 ) // 80-89 gets "B“ cout << "B"; else if ( studentGrade >= 70 ) // 70-79 gets "C" cout << "C"; else if ( studentGrade >= 60 ) // 60-69 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