+ All Categories
Home > Documents > Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables...

Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables...

Date post: 01-Jan-2016
Category:
Upload: loren-patrick
View: 216 times
Download: 0 times
Share this document with a friend
27
Programming Fundamentals Lecture 5
Transcript
Page 1: 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 5

Page 2: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

In the Previous Lecture

• Basic structure of C program• Variables and Data types• Operators• ‘cout’ and ‘cin’ for output and input• Braces

Page 3: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Decision

Page 4: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

If Statement

If condition is truestatements

If Ali’s height is greater then 6 feetThen

Ali can become a member of the Basket Ball team

Page 5: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

If Statement in C

If (condition)statement ;

Page 6: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

If Statement in C

If ( condition ){

statement1 ;statement2 ;

:}

Page 7: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

If statement in C

Page 8: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Relational Operators

Page 9: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Relational Operators

a != b;

X = 0;X == 0;

Page 10: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Example#include <iostream.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’s age is greater then Amara’s age” ;}

}

Page 11: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Flow Charting

• There are different techniques that are used to analyse and design a program. We will use the flow chart technique.

• A flow chart is a pictorial representation of a program.

• There are labelled geometrical symbols, together with the arrows connecting one symbol with other.

Page 12: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Flow Chart Symbols

Start or stop

Process

Flow line

Continuation mark

Decision

Page 13: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Page 14: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Page 15: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Example

If the student age is greater than 18 or his height is greater than five feet

then put him on the foot balll teamElse

Put him on the chess team

Page 16: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Logical Operators

AND &&OR ||

Page 17: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Logical OperatorsIf a is greater than b

AND c is greater than d

In Cif(a > b && c> d)if(age > 18 || height > 5)

Page 18: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

if-elseif (condition){

statement ;--

}else{

statement ;--

}

Page 19: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Example • Code

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

• if (AmirAge < AmaraAge) • {• cout<< “Amir is younger than Amara” ;• }

Page 20: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

ExampleCode

if AmirAge > AmaraAge) { cout<< “Amir is older than Amara” ;} else { cout<<“Amir is younger than or of the same age as Amara” ;}

Page 21: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Page 22: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Page 23: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Page 24: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Example

If (AmirAge != AmaraAge) cout << “Amir and Amara’s Ages are not equal”;

Page 25: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Unary Not operator !

!true = false !false = true

Page 26: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Exampleif ((interMarks > 45) && (testMarks >= passMarks)){

cout << “ Welcome to Lahore University”;}

Page 27: Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Nested ifIf (age > 18){

If(height > 5){

:}

}Make a flowchart of this nested if structure…


Recommended