1 / 24

Computer Programming Code: CS1112

Computer Programming Code: CS1112. Lecture No. 4 Bilal Ashfaq Ahmed. In The Previous Lectures. Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output and input Manipulators. Relational Operators. Selection/Conditional Statements.

miyoko
Download Presentation

Computer Programming Code: CS1112

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer ProgrammingCode: CS1112 Lecture No. 4 Bilal Ashfaq Ahmed

  2. In The Previous Lectures • Basic structure of C program • Variables and Data types • Operators • ‘cout’ and ‘cin’ for output and input • Manipulators

  3. Relational Operators

  4. Selection/Conditional Statements • Decision Making Statements • IF Selection Structure • IF-ELSE Selection Structure • SWITCH Selection Structure

  5. If Statement in C The if statement has a simple structure. if ( condition / expression ) Statement (or group of statements); For Example: Suppose the passing grade on an exam is 60. The pseudo code statement If student's grade is greater than or equal to 60 Print "Passed"

  6. IF Statement in C The preceding pseudo code If statement can be written in C++ as if ( grade >= 60 ) cout << "Passed";

  7. IF Statement in C Single Statement if (condition) Statement ; Multiple Statement if ( condition ) { Statement1 ; Statement2 ; : }

  8. Example • Suppose, we have ages of two students (say for the time being we have got these ages in variables). These variables are age1 and age2. Now we say that if the age1 is greater than age2, then display the statement ‘Student 1 is older than student 2’. • The coding for this program will be as below

  9. # # void main() { int age 1,age2; clrscr(); cout<<“Enter the age of first student”<<endl; cin>>age1; cout<<“Enter the age of Second student”<<endl; cin>>age2; if (age1 > age2) cout << “Student 1 is older than student 2”; getch(); }

  10. Flow Charts

  11. IF Flow Chart True False Boolean Exp Statement(s) Rest of the program

  12. Examples Using two IF Statement /* This program checks the age of Amer and Amara and displays the appropriate the message. The program is using two if statements.*/ # include <iostream.h> #include<conio.h> void main ( ) { int AmerAge, AmaraAge; clrscr(); //prompt the user to enter Amer’s age cout << “Please enter Amer’s age “ ; cin >> AmerAge; //prompt the user to enter Amara’s age cout << “Please enter Amara’s age “ ; cin >> AmaraAge;

  13. //perform the test if (AmerAge > AmaraAge ) { cout << “ Amer is older than Amara”; } if (AmerAge < AmaraAge ) { cout << “ Amer is younger than Amara”; } }

  14. Logical Operators AND && OR || NOT ! If a is greater than b AND c is greater than d IN C if(a > b && d > c) if (age>18 || height>5)

  15. IF ELSE Statement if ( condition) { statement(s); } else { statement(s); }

  16. IF ELSE Flow chart

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

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

  19. Example If The student age is greater than 19 or his height is greater than or equal to six feet then put him on the basket ball team Else Put him on the football team if (Age > 19 && Height >= 6) cout<<“Student is selected Basketball Team ”<<endl; else cout<<“Student is selected Football Team ”<<endl;

  20. TASK /* Program that accepts a number from the user and prints EVEN if the number is even (divisible by 2) else prints ODD. */ #include<iostream.h> #include<conio.h> void main() { int num; clrscr(); cout<<" Enter Number : “<<endl; cin>>num; if(num % 2 == 0) cout<<“EVEN Number”<<endl; else cout<<“ODD Number”<<endl; getch(); }

  21. Conditional Operator • Symbol ( ? : ) • Closely related to if–else statement • Only ternary operator in C++ • Syntax: (condition)? Statement A : Statement B; Example cout<<(num%2 == 0) ? “EVEN Number” : “ODD Number” ;

  22. Nested IF Structure • if structure is used within an other if structure • Syntax if ( condition-1 ) //outer if statement { if ( condition-2 ) //inner if statement { Statement(s) ; } } • Dangling else problem

  23. Nested IF_ELSE Statements • if-else structure place within another if-else structure • Also known as if-else- if structure • Used for multiple selecton if( student Grade >= 90 ) // 90 and above gets "A" cout << "A"; else if ( student Grade >= 80 ) // 80-89 gets "B“ cout << "B"; else if ( student Grade >= 70 ) // 70-79 gets "C" cout << "C"; else if ( student Grade >= 60 ) // 60-69 gets "D“ cout << "D"; else // less than 60 gets "F" cout << "F";

  24. Task: • 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.

More Related