CS201- Introduction to Programming- Lecture 05
-
Upload
bilal-ahmed -
Category
Education
-
view
50 -
download
2
Transcript of CS201- Introduction to Programming- Lecture 05
![Page 1: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/1.jpg)
Introduction to ProgrammingIntroduction to Programming
Lecture 5Lecture 5
![Page 2: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/2.jpg)
In the Previous In the Previous LectureLecture
Basic structure of C programBasic structure of C program Variables and Data typesVariables and Data types OperatorsOperators ‘ ‘cout’ and ‘cin’ for output and inputcout’ and ‘cin’ for output and input BracesBraces
![Page 3: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/3.jpg)
DecisionDecision
![Page 4: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/4.jpg)
If StatementIf StatementI f condition is trueIf condition is true
statementsstatements
If Ali ’s height is greater then 6 If Ali ’s height is greater then 6 feetfeet
Then Then Ali can become a member of the Ali can become a member of the Basket Ball teamBasket Ball team
![Page 5: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/5.jpg)
If Statement in If Statement in CC
If (condit ion)If (condit ion)statement ;statement ;
![Page 6: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/6.jpg)
If Statement in If Statement in CC
I f ( condit ion )If ( condit ion ){{
statement1 ;statement1 ;statement2 ;statement2 ;
::} }
![Page 7: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/7.jpg)
I f statement in CIf statement in C
i f (age1 > age2)if (age1 > age2)cout<<“Student 1 is older cout<<“Student 1 is older than student 2” ;than student 2” ;
![Page 8: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/8.jpg)
Relational Relational OperatorsOperators
< < less thanless than<= <= less than or equal toless than or equal to== equal to== equal to>= greater than or equal to>= greater than or equal to> greater than> greater than!= not equal to!= not equal to
![Page 9: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/9.jpg)
Relational Relational OperatorsOperators
a != b;a != b;
X = 0;X = 0;X == 0;X == 0;
![Page 10: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/10.jpg)
ExampleExample#include <iostream.h>#include <iostream.h>main ( ) main ( ) {{
int AmirAge, AmaraAge;int AmirAge, AmaraAge;AmirAge = 0;AmirAge = 0;AmaraAge = 0;AmaraAge = 0;
cout<<“Please enter Amir’s age”;cout<<“Please enter Amir’s age”;cin >> AmirAge;cin >> AmirAge;cout<<“Please enter Amara’s age”;cout<<“Please enter Amara’s age”;cin >> AmaraAge;cin >> AmaraAge;
if AmirAge > AmaraAge) if AmirAge > AmaraAge) {{
cout << “\n”<< “Amir’s age is greater then Amara’s age” ;cout << “\n”<< “Amir’s age is greater then Amara’s age” ;} }
}}
![Page 11: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/11.jpg)
Flow Chart SymbolsFlow Chart SymbolsStart or stop
Process
Continuation mark
Decision
Flow line
![Page 12: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/12.jpg)
Flow Chart for i f statementFlow Chart for i f statement
Condition
Process
IF
Then
Entry point for IF block
Exit point for IF block
Note indentation from left to right
![Page 13: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/13.jpg)
ExampleExampleIf the student age is greater than 18 If the student age is greater than 18
or his height is greater than five or his height is greater than five feet then put him on the foot balll feet then put him on the foot balll teamteam
ElseElsePut him on the chess teamPut him on the chess team
![Page 14: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/14.jpg)
Logical OperatorsLogical Operators
ANDAND &&&&OROR ||||
![Page 15: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/15.jpg)
Logical OperatorsLogical OperatorsIf a is greater than b If a is greater than b
AND c is greater than dAND c is greater than d
In Cif(a > b && c> d)if(age > 18 || height > 5)
![Page 16: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/16.jpg)
i f-elseif-elseif (condition)if (condition){{
statement ;statement ;----
}}elseelse{{
statement ;statement ;----
}}
![Page 17: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/17.jpg)
i f-elseif-elseCondition
Process 1
IF
Then
Entry point for IF-Else block
Exit point for IF block
Process 2
Else
Note indentation from left to right
![Page 18: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/18.jpg)
Code
if (AmirAge > AmaraAge) { cout<< “Amir is older than Amara” ;}
if (AmirAge < AmaraAge) { cout<< “Amir is younger than Amara” ;}
Example Example
![Page 19: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/19.jpg)
ExampleExampleCodeCode
if AmirAge > AmaraAge) if AmirAge > AmaraAge) {{ cout<< “Amir is older than Amara” ;cout<< “Amir is older than Amara” ;} } else else {{ cout<<“Amir is younger than Amara” ;cout<<“Amir is younger than Amara” ;}}
![Page 20: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/20.jpg)
Make a small f low chart of thisMake a small f low chart of thisprogram and see the one to program and see the one to
oneonecorrespondence of the chart correspondence of the chart and the codeand the code
![Page 21: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/21.jpg)
Example Example CodeCode
if AmirAge > AmaraAge) if AmirAge > AmaraAge) {{ cout<< “Amir is older than Amara” ;cout<< “Amir is older than Amara” ;} } else else {{ cout<<“Amir is younger than or of the same age cout<<“Amir is younger than or of the same age
as Amara” ;as Amara” ;}}
![Page 22: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/22.jpg)
ExampleExampleIf (AmirAge != AmaraAge)If (AmirAge != AmaraAge) cout << “Amir and Amara’s Ages cout << “Amir and Amara’s Ages
are not equal”;are not equal”;
![Page 23: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/23.jpg)
Unary Not operator Unary Not operator !!
!true = false!true = false !false = true!false = true
![Page 24: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/24.jpg)
If (!(AmirAge > AmaraAge))If (!(AmirAge > AmaraAge))
??
![Page 25: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/25.jpg)
ExampleExample
i f (( interMarks > 45) && (testMarks >= passMarks))i f (( interMarks > 45) && (testMarks >= passMarks)){ {
cout << “ Welcome to Virtual University”;cout << “ Welcome to Virtual University”;} }
![Page 26: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/26.jpg)
ExampleExample
If(!((interMarks > 45) && (testMarks >= passMarks)))If(!((interMarks > 45) && (testMarks >= passMarks)))
??
![Page 27: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/27.jpg)
Nested ifNested ifI f (age > 18)If (age > 18){{
If(height > 5)If(height > 5){{
::}}
}}Make a f lowchart of this nested if Make a f lowchart of this nested if
structure…structure…
![Page 28: CS201- Introduction to Programming- Lecture 05](https://reader034.fdocuments.us/reader034/viewer/2022050922/559f3c041a28ab7b748b4585/html5/thumbnails/28.jpg)
In Today’s Lecture In Today’s Lecture DecisionDecision
– IfIf– ElseElse
Flowcharts Flowcharts Nested if Nested if