You are on page 1of 8

PROGRAMMING FUNDAMENTALS

LEC 05

~ Logic Structure (Flow):

~ Sequential 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.
~ Decision Making in C++:
1. 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.

· C++ handles decision making by supporting the following statements:

- if statement

- Variations of if Statement

- switch statement

- Conditional Operator (?) Statement

- goto Statement

2. 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.

3. 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

· 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.
4. if Statement Structure :

If (condition)

statement;

If ( condition )

statement1;

statement2;

5. 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.

6. Use of Relational Operators:


· A relational Operator always evaluates for true or false only.
7. Example:

using namespace std;

#include<iostream>

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" ;


}

~ Flow Charts:
1. 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.
2. 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.

· 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.


~ Logical Operators:
1. Use of Logical Operators :
Problem Statement:

If a is greater than b AND c is greater than d.

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)

2. 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.

You might also like