Lecture-4

Transcription

Lecture-4
Programming Fundamental
Instructor Name:
Lecture-4
Today’s Lecture
 Logic Structure (Flow)
 Sequence Structure
 Decision making in C++
 Selection Statement (if structure)
 Use of relation Operator in if structure
 Flow Charts for Analysis & Design
 Use of Logical Operators – Examples
 If/else structure
 Home Activity [Problem Statement]
2
Logic Structure (Flow)
 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++
What is Decision Making?
 Decision making is about deciding the order of execution of statements
based on certain condition or repeat a group of statements until certain
specified conditions are met.
 Decision making structures require that the programmer specify one or
more conditions to be evaluated or tested by the program, along with a
statement or set of statements to be executed if the condition is determined
to be true, and optionally, other statements to be executed if the condition
is determined to be false.
5
Decision Making in C++
Decisions in Daily Life
 Every makes decision in daily life while doing different activities
 Decision of stopping or driving a bike/car when reach on signal
 Admission in College based on your result.
 Promotion to new class possible if you are pass in previous class
 Purchase of grocery
 Fueling your bike/car
 Traveling somewhere
 And many more
6
Decision Making in C++
Decision Making in C++
 C++ handles decision making by supporting the following statements
 if statement
 Variations of if Statement
 switch statement
 Conditional Operator (?) Statement
 goto Statement
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++
Use of Relational operators
 A relational Operator always evaluates for true or false only
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
What is a Flow Chart?
 A flow chart is a pictorial representation of a program.
 There are labeled geometrical symbols, together with the arrows
connecting one symbol with other.
 A flow chart helps in correctly designing the program by visually showing
the sequence of instructions to be executed.
 A programmer can trace and rectify the logical errors by first drawing a
flow chart and then simulating it.
14
Flow Charts
15
Flow Charts
Flow Chart for if Statement
 Recall the problem statement, if student gets 50 or
more marks then display congratulation message on
screen
Class Activity
 Draw a flow chart diagram. If age of the student is
18 years or more and he is Pakistani then he is
eligible for applying CNIC.
16
Flow Charts
Flow Chart [Class Activity]
 Did you find any thing new here?
 Here Pakistani is Boolean Variable that has either
true or false value
 Use of Logical AND (&&)
 Logical Operators are used to combine two
conditions
17
Logical Operators
Use of Logical Operators
Problem Statement
If a is greater than b
c is greater than d
AND
in C++
If(a>b) && (c>d)
Problem Statement
If age is greater than 18 OR
height is greater than 5
in C++
If(age>18) || (height>5)
18
Logical Operators
Use of Logical Operator
 Another logical operator NOT (!) is used for negation
 This operator enables a programmer to ‘reverse’ the meaning of a condition.
 This is a unary operator that has only a single condition as an operand.
 The operator ! is placed before a condition.
 If the original condition is false then the ! operator converts it to true and vice
versa
if ( ! (age > 18 ))
cout << “ The age is less than 18”;
 Here the cout statement will be executed if the original condition (age > 18) is
false
19
20