0% found this document useful (0 votes)
13 views55 pages

Programming Fundamental 6 Lec

The document outlines the fundamentals of programming control structures, focusing on selection and repetition. It explains the use of logical expressions, relational operators, and how they facilitate decision-making in programming through one-way and two-way selections. Additionally, it introduces logical (Boolean) operators for combining logical expressions and discusses the importance of order of precedence in evaluating these expressions.

Uploaded by

cyberexpert997
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views55 pages

Programming Fundamental 6 Lec

The document outlines the fundamentals of programming control structures, focusing on selection and repetition. It explains the use of logical expressions, relational operators, and how they facilitate decision-making in programming through one-way and two-way selections. Additionally, it introduces logical (Boolean) operators for combining logical expressions and discusses the importance of order of precedence in evaluating these expressions.

Uploaded by

cyberexpert997
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

Programming

Fundamentals

Instructor: Noor Ullah Khan


Today’s Agenda

• Control Structures
• If and if else
• Relational Operators
• Comparing Characters
• One-Way Selection
• Two-Way Selection
Control Structures

• A computer can process a program in one of the following ways:


• in sequence;
• selectively, by making a choice, which is also called a branch;
• repetitively, by executing a statement over and over, using a structure called a loop;
• or by calling a function.
Up till now our program was executing
statements in Sequence.

With such a program, the computer starts at


Control the beginning and follows the statements in
order to the end.
Structure No choices are made;
s
There is no repetition.
Control structures provide
alternatives to sequential program
execution and are used to alter the
sequential flow of execution.
Control
Structures The two most common control
structures are selection and
repetition.
Control Structures

In selection, the program executes particular statements


depending on some condition(s).

In repetition, the program repeats particular statements a certain


number of times based on some condition(s).
We will start
with Selection
SELECTION: if
AND if . . . else
First we need to learn about logical expressions and
how to evaluate them.
An expression that evaluates to
true or false is called a logical
expression.
Logical For example, because “8 is greater
expressi than 3” is true, the expression 8 > 3
is a logical expression.
on Note that > is an operator in C++,
called the “greater than” and is an
example of a relational operator.
Logical expression
Logical expression
Each of the relational operators is
a binary operator; that is, it
requires two operands.
Logical
expression Because the result of a comparison
is true or false, expressions using
these operators always evaluate
to true or false.
Relational Operators and
Simple Data Types

You can use the relational operators with all three


simple data types.
Relational Operators and
Simple Data Types
For char values, whether an expression using
relational operators evaluates to true or false
depends on a machine’s collating sequence.

A collating sequence (also called a sort


Comparing sequence) defines how characters in a
character set relate to each other when they
Characters are compared and ordered.

The collating sequence of some of the characters


is on following slides.
Comparing Characters
Comparing Characters
Although there are only two
logical values, true and false,
they turn out to be extremely
useful.
Comparing
Characters
Because they permit programs
to incorporate decision making
that alters the processing flow.
A bank would like to send a notice to a
customer if her or his checking account
balance falls below the required minimum
balance.
That is, if the account balance is below the
One-Way required minimum balance, it should send a
Selection notice to the customer;

Otherwise, it should do nothing.


Similarly, if the policyholder of an insurance
policy is a nonsmoker, the company would
like to apply a 10% discount to the policy
premium.
Both of these examples involve one-way
One-Way selection.
Selection
In C++, one-way selections are incorporated
using the if statement.
One-Way Selection
It begins with the reserved word if, followed
by an expression contained within
parentheses, followed by a statement.

One-Way Note that the parentheses around the


Selection expression are part of the syntax.

The expression is sometimes called a


decision maker because it decides whether
to execute the statement that follows it.
The expression is usually a
logical expression. If the
value of the expression is
true, the statement executes.
One-Way
Selection If the value is false, the
statement does not execute.
One-Way Selection
One-Way Selection
One-Way Selection
One-Way Selection
Two-Way Selection
• There are many programming situations in which you must choose
between two alternatives.
• For example, if a part-time employee works overtime, the paycheck is
calculated using the overtime payment formula;
• Otherwise, the paycheck is calculated using the regular formula.
Two-Way Selection
Two-Way Selection
• It begins with the reserved word if.
• By a logical expression contained within parentheses.
• Followed by a statement,
• Followed by the reserved word else, followed by a second statement.
Two-Way Selection
Two-Way Selection
Two-Way Selection
Two-Way Selection
Two-Way Selection
Logical (Boolean) Operators and
Logical Expressions
• The logical expressions used in earlier examples involve the
evaluation of a single relational operator.
• There are situations when the logical expression is a combination of
two or more logical expressions.
• For example, suppose weight and height are double variables.
Consider the following logical expression:
• weight > 180 and height < 6.0
Logical (Boolean) Operators and
Logical Expressions
• This logical expression is a combination of the logical expressions
weight > 180 and height < 6.0, and these logical expressions are
combined using the word “and”.
• Logical (Boolean) operators enable you to combine logical
expressions
• Logical operators take only logical values as operands and output
only logical values as results.
Logical (Boolean) Operators and
Logical Expressions
Logical (Boolean) Operators and
Logical Expressions
• The operator ! is unary, so it has only one operand.
• The operators && and || are binary operators and there is no space
within these operators.
Logical (Boolean) Operators and
Logical Expressions
Logical (Boolean) Operators and
Logical Expressions
Logical (Boolean) Operators and
Logical Expressions
Logical (Boolean) Operators and
Logical Expressions
Logical (Boolean) Operators and
Logical Expressions
Logical (Boolean) Operators and
Logical Expressions
Order of Precedence
Order of Precedence

You might also like