Week 4 Computer Laboratory II

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Logical Operations

There is a whole other class of operators known as the logical operators. C++ programs have
to make decisions. A program that can’t make decisions is of limited use. The temperature
conversion program is about as complex you can get without some type of decision-making.
Invariably a computer program gets to the point where it has to figure out situations such as
“Do this if the a variable is less than some value, do that other thing if it’s not.” That’s what
makes a computer appear to be intelligent — that it can make decisions.
Simple Logical Operator

The simple logical operators are show in the table below.


The first six entries in the figure above are comparison operators. The equality operator is
used
to compare two numbers. For example, the following is true if the value of n is 0, and is false
otherwise:
n == 0;
Looks can be deceiving. Don’t confuse the equality operator (==) with the assignment
operator (=). Not only is this a common mistake, but it’s a mistake that the C++ compiler
generally cannot catch — that makes it more than twice as bad.
n = 0; // programmer meant to say n == 0
The greater-than (>) and less-than (<) operators are similarly common in everyday life
Bitwise Logical Operations

All C++ numbers can be


expressed in binary form.
Binary numbers use only the
digits 1 and 0 to represent a
value. The following table
defines the set of operations
that work on numbers one
bit at a time, hence the term
bitwise operators.
CONTROL STRUCTURE

Computer programs are all about making decisions. If the user presses a key, the computer
responds to the command. This section focuses on controlling program flow. Flow-control
commands allow the program to decide what action to take based on the results of the C++
logical operations performed. There are basically three types of flow-control statements: the
branch, the loop, and the switch.
Branch Statement

The simplest form of flow


control is the branch
statement. This instruction
allows the program to
decide which of two paths
to take through C++
instructions, based on the
results of a logical
expression.
First, the logical expression m > n is evaluated. If the result of the expression is true, control
passes down the path marked Path 1 in the previous snippet. If the expression is false, control
passes to Path 2. The else clause is optional. If it is not present, C++ acts as if it is present but
empty. Actually, the braces are optional (sort of) if there’s only one statement to execute as
part of the if. If you lose the braces, however, it’s embarrassingly easy to make a mistake that
the C++ compiler can’t catch.
Loops

Branch statements allow you to control the flow of a program’s execution from one path of a
program or another. This is a big improvement, but still not enough to write full-strength
programs. Executing the same command multiple times requires some type of looping
statements.
While Loop: The simplest form of looping statement is the while loop. Here’s what the while
loop looks like:
while(condition)
{
// ... repeatedly executed as long as condition is true
}
The condition is tested. This condition could be if var > 10 or if var1 == var2 or anything else
you might think of. If it is true, the statements within the braces are executed. Upon
encountering the closed brace, C++ returns control to the beginning, and the process starts
over. The effect is that the C++ code within the braces (i.e. code block) is executed repeatedly
as long as the condition is true. If the condition were true the first time, what would make it
be false in the future?
for loop

The most common form of loop is the for loop. The for loop is preferred over the more basic while loop because it’s
generally easier to read. The for loop has the following format:
for (initialization; conditional; increment)
{
// ...body of the loop
}
Execution of the for loop begins with the initialization clause, which got its name because it’s normally where
counting variables are initialized. The initialization clause is only executed once when the for loop is first
encountered. Execution continues with the conditional clause. This clause works a lot like the while loop: as long as
the conditional clause is true, the for loop continues to execute.
Assignment

 Write on Branch Statements with examples


 With a code snippet explain the kind of loops.

You might also like