Programming - Using C++ - Session - 05
Programming - Using C++ - Session - 05
Objectives
In this lesson, you will learn to:
Use operators
Use conditional constructs
Use loop constructs
Define the scope of variables
Types of Operators
Arithmetic operators
Assignment operators
Unary operators
Comparison operators
Logical operators
Conditional operators
Arithmetic Operators
Are used to perform arithmetic operations
Are used in an order according to the
precedence rules
Unary Operators
Operate on one operand
Are of two types:
Increment operator (++)
Decrement operator (--)
Comparison Operators
Are also called relational operators
Are used to compare two values
Evaluate to true or false
Evaluates whether or not the x == y Returns true if the values are equal and false
==
operands are equal. otherwise
Evaluates whether or not the x != y Returns true if the values are not equal and
!=
operands are not equal false otherwise
Logical Operators
Are used to combine two or more expressions
Are of three types:
AND (&&)
OR (||)
NOT (!)
Conditional Constructs
Control the flow of a program
Allow selective execution of statements
Use comparison operators for evaluating conditions
Are of two types:
The ifelse construct
The switchcase construct
Just a Minute
Write a construct that assigns grades to students based
on their marks. Students who have scored marks
between 75 and 100 are to be given grade A, those who
have scored between 50 and 75 are to be given grade
B, and the rest of them should be given grade C.
Loop Constructs
Cause a section of a program to be repeated a certain
number of times
Are of three types:
while loop
dowhile loop
for loop
Just a Minute...
Write a function to display the sum of all numbers
between 1 and 100.
File Scope
Is considered to be the outermost scope
Variables are accessible throughout the program file
Are called global variables
Local Scope
Is defined as being limited to the braces of a function
or a control structure like for, while, and if
Are called local variables
Class Scope
Variables are accessible only within the class
Variables have the flexibility of being accessed
outside the class by declaring them as public
Summary
In this lesson, you learned that:
Operators are used to compute and compare values
and test multiple conditions
You use arithmetic operators to perform arithmetic
operations, such as addition, subtraction,
multiplication, and division
You can also use arithmetic assignment operators to
perform arithmetic operations
The unary operators, such as the increment and
decrement operators operate on one operand
Comparison operators, which are also called
relational operators, are used to compare two values
NIIT Programming Using C++/Session 5/Slide 28 of 30
Operators and Decision-Making Constructs
Summary (Contd.)
Logical operators are used to combine two or more
expressions
Conditional constructs are used to allow the selective
execution of statements. The conditional constructs in
C++ are:
if...else
switch...case
Looping constructs are used when you want a section
of a program to be repeated a certain number of
times. C++ offers the following looping constructs:
while
do...while
for
NIIT Programming Using C++/Session 5/Slide 29 of 30
Operators and Decision-Making Constructs
Summary (Contd.)
The break and continue statements are used to
control the program flow within a loop