Introduction to Programming - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Introduction to Programming

Description:

Introduction to Programming ... Example Logical Operators Logical Operators if-else if-else Example Example Slide 20 Example Example Unary Not operator ... – PowerPoint PPT presentation

Number of Views:107
Avg rating:3.0/5.0
Slides: 29
Provided by: Saba61
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Programming


1
Introduction to Programming
  • Lecture 5

2
In the Previous Lecture
  • Basic structure of C program
  • Variables and Data types
  • Operators
  • cout and cin for output and input
  • Braces

3
  • Decision

4
If Statement
  • If condition is true
  • statements
  • If Alis height is greater then 6 feet
  • Then
  • Ali can become a member of the Basket Ball team

5
If Statement in C
  • If (condition)
  • statement

6
If Statement in C
  • If ( condition )
  • statement1
  • statement2

7
If statement in C
  • if (age1 gt age2)
  • coutltltStudent 1 is older than student 2

8
Relational Operators
  • lt less than
  • lt less than or equal to
  • equal to
  • gt greater than or equal to
  • gt greater than
  • ! not equal to

9
Relational Operators
  • a ! b
  • X 0
  • X 0

10
Example
  • include ltiostream.hgt
  • main ( )
  • int AmirAge, AmaraAge
  • AmirAge 0
  • AmaraAge 0
  • coutltltPlease enter Amirs age
  • cin gtgt AmirAge
  • coutltltPlease enter Amaras age
  • cin gtgt AmaraAge
  • if AmirAge gt AmaraAge)
  • cout ltlt \nltlt Amirs age is greater then
    Amaras age

11
Flow Chart Symbols
Start or stop
Process
Flow line
Continuation mark
Decision
12
Flow Chart for if statement
Entry point for IF block
IF
Condition
Then
Process
Exit point for IF block
Note indentation from left to right
13
Example
  • If the student age is greater than 18 or his
    height is greater than five feet then put him on
    the foot balll team
  • Else
  • Put him on the chess team

14
Logical Operators
  • AND
  • OR

15
Logical Operators
  • If a is greater than b
  • AND c is greater than d

In C if(a gt b cgt d) if(age gt 18 height gt 5)
16
if-else
  • if (condition)
  • statement
  • -
  • -
  • else
  • statement
  • -
  • -

17
if-else
Entry point for IF-Else block
IF
Condition
Then
Process 1
Else
Process 2
Exit point for IF block
Note indentation from left to right
18
Example
Code if (AmirAge gt AmaraAge) coutltlt
Amir is older than Amara if (AmirAge lt
AmaraAge) coutltlt Amir is younger than
Amara
19
Example
  • Code
  • if AmirAge gt AmaraAge)
  • coutltlt Amir is older than Amara
  • else
  • coutltltAmir is younger than Amara

20
  • Make a small flow chart of this
  • program and see the one to one
  • correspondence of the chart
  • and the code

21
Example
  • Code
  • if AmirAge gt AmaraAge)
  • coutltlt Amir is older than Amara
  • else
  • coutltltAmir is younger than or of the same
    age as Amara

22
Example
  • If (AmirAge ! AmaraAge)
  • cout ltlt Amir and Amaras Ages are not equal

23
Unary Not operator !
  • !true false
  • !false true

24
  • If (!(AmirAge gt AmaraAge))
  • ?

25
Example
  • if ((interMarks gt 45) (testMarks gt
    passMarks))
  • cout ltlt Welcome to Virtual University

26
Example
  • If(!((interMarks gt 45) (testMarks gt
    passMarks)))
  • ?

27
Nested if
  • If (age gt 18)
  • If(height gt 5)
  • Make a flowchart of this nested if structure

28
In Todays Lecture
  • Decision
  • If
  • Else
  • Flowcharts
  • Nested if
Write a Comment
User Comments (0)
About PowerShow.com