Lecture 5
Lecture 5
Sequence Structure
If/else structure
Programs are recipes which have a particular start spots and special rules on
how the computer follows them.
In general, a program will start at some "main" routine and continue
"downward" one line at a time until the end of the function is reached.
Any time another function is encountered, all lines of code inside that function
will be completed, before continuing in the current function.
Any time an "if" statement is reached, a decision will be made on whether to
execute one set of statements or another.
Any time a loop is reached, a set of statements may be repeated zero or more
time.
3
Sequential Structure
Sequence Structure
In a sequence structure, an action, or event,
leads to the next ordered action in a
predetermined order.
The sequence can contain any number of
actions, but no actions can be skipped in the
sequence.
The program, when run, must perform each
action in order with no possibility of skipping
an action or branching off to another action.
4
Decision Making in C++
5
Decision Making in C++
Purchase of grocery
Traveling somewhere
6
Decision Making in C++
7
Decision Making in C++
if Statement in C++
The statement used for decisions in 'C++' language is known as the 'if
statement'.
The if statement has a simple structure. That is
if ( condition )
Statement (or group of statements)
The above statements mean, If condition is true, then execute the statement
or a group of statements.
For example:
If (you are hungry)
Eat Something
8
Decision Making in C++
if Statement in C++
The simple English language statement implements in terms of variables,
operators and C++ statements
Only the statement following the if condition is part of ‘if’ and rest of all
are not part of if statement
If you want to make more than one statement part of ‘if’ statement then
you have to create block of statement by using braces’{}’
Put ‘{‘ before the first statement and ‘}’ after the last statement to make it a
single block
Either the whole block will execute or none of the statement (in block) will
execute
9
Decision Making in C++
if Statement Structure
If (condition)
statement ;
If ( condition )
{
statement1 ;
statement2 ;
:
statement(n);
} 10
Decision Making in C++
if Statement Example
Problem Statement:
If students gets 50 or more marks, display congratulation message on screen.
Solution:
If (marks>=50)
cout<<“Congratulation! You are Pass”;
Dissecting the solution:
In the if condition the value of marks is compared with value 50 using
relational operator ‘>’. if value in marks is 50 or more then the cout statement
will execute. Otherwise nothing will appear on screen.
11
Decision Making in C++
12
Decision Making in C++
Example
using namespace std;
#include<iostream>
#include <conio.h>
main(){
int 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();
}
13
Flow Charts
14
Flow Charts
15
Flow Charts
16
Flow Charts
17
Logical Operators
in C++
in C++
If(age>18) || (height>5) 18
Logical Operators
19
20