0% found this document useful (0 votes)
0 views

Lecture 5 - Errors, Operators Conditional Statements

The document provides an overview of errors, operators, and conditional statements in C++. It covers various types of operators, their functions, and examples of using them in programming, as well as the concept of algorithms and pseudocode for problem-solving. Additionally, it discusses different types of programming errors and debugging techniques.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Lecture 5 - Errors, Operators Conditional Statements

The document provides an overview of errors, operators, and conditional statements in C++. It covers various types of operators, their functions, and examples of using them in programming, as well as the concept of algorithms and pseudocode for problem-solving. Additionally, it discusses different types of programming errors and debugging techniques.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

SCHOOL OF ELECTRICL ENGINEERING &

COMPUTER SCIENCE (SEECS)

Errors, Operators & Conditional Statements


Sara Tariq Sheikh
Recap!
Class Activity

Fundamentals of Computer Programming 2


Join Padlet

https://padlet.com/saratariq4/c-starter-
luedenkideynlm92

Fundamentals of Computer Programming


Assignment Operators

Getting Started with C++ 4


Examples

Getting Started with C++ 5


Understanding x |= 3
Assuming x = 5
Step 1:
Convert 5 and 3 to their decimal equivalents

Decimal Binary Representation

5 101

3 011

Getting Started with C++ 6


Understanding x |= 3
Step 2:
The bitwise OR (|) operator compares corresponding bits of two
numbers:
• Returns 1 if at least one bit is 1.

5 = 101 3 = 011 x=x|3

1 0 1

1 1 1

0 1 1

Getting Started with C++ 7


Understanding x |= 3
Step 3:
Convert the binary back to its decimal form.

Binary Decimal

111 7

So, x |= 3 is equal to 7.

Getting Started with C++ 8


Operator Name Function

x ^= 3 XOR assignment operator • If the bits are the same (0,0 or 1,1),
the result is 0.
• If the bits are different (0,1 or 1,0),
the result is 1.

x >>= 3 Right shift assignment operator • Moves each bit 3 positions to the
right, discarding the rightmost bits
and filling the leftmost bits with 0s
• For positive numbers, >> divides
by powers of 2.
x <<= 3 Left shift assignment operator • Moves each bit 3 positions to the
left, shifting in zeros (0) from the
right
• Left shifting (<<) multiplies the
number by powers of 2.
• Each left shift operation (x << n) is
equivalent to x * (2^n).
• Shifting too far left can cause
overflow if the number exceeds
the variable's bit capacity.

Getting Started with C++ 9


Comparison Operators

Getting Started with C++ 10


Logical Operators

Getting Started with C++ 11


Logical Operators
#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 3;
cout << (x > 3 && x < 10);
return 0;
}

Getting Started with C++


Operator Precedence

• Operator precedence specifies the order in which operations are


performed within an expression.
• For example, int x = 5 - 17 * 6;

Getting Started with C++ 13


Operator Associativity
• Operator associativity determines the order in which operands are
grouped when multiple operators have the same precedence.
• Left-to-right associativity
• In expressions like a + b - c, the addition and subtraction operators are
evaluated from left to right. So, (a + b) - c is equivalent.
• Right-to-left associativity
• Some operators, like the assignment operator =, have right-to-left
associativity. For example, a = b = 4; assigns the value of b to a.

Getting Started with C++ 14


Operator Name Associativity

() [] -> . Function call, Subscript, Member access Left

++ -- Increment/Decrement Right

!~-+ Logical/Bitwise NOT, Unary plus/minus Right

*/% Multiplication, Division, Modulus Left

+- Addition, Subtraction Left

<< >> Bitwise shift Left

< <= > >= Relational operators Left

== != Equality operators Left

& Bitwise AND Left

^ Bitwise XOR Left

| Bitwise OR Left

&& Logical AND Left

|| Logical OR Left

?: Ternary conditional Right

= += -= *= /= %= &= ^= |= <<= >>= Assignment and compound assignment Right

, Comma Left 15
Example
#include <iostream>
using namespace std;

int main() {
int x = 5;
cout << !(x > 3);
return 0;
}

Getting Started with C++ 16


Example

• int result = 10 + 3 * 2 / (6 - 4) % 2;

Getting Started with C++ 17


Example
#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 3;
cout << (!(x < 3) && (y == 3 || x < 10));
return 0;
}

Getting Started with C++ 18


Example
#include <iostream>
using namespace std;
What if x++ is
int main() { replaced with ++x?
int x = 5; How will the output
change?
int n = x++ * 3;

cout << "Value of n: " << n << endl;


cout << "Updated value of x: " << x << endl;
return 0;
}
Getting Started with C++ 19
Errors

Getting Started with C++ 20


What are errors?

• Issues or defects that arise within a computer program, resulting in


abnormal behavior

• Programming errors are called bugs

• The process of tracking them down and correcting them is called


debugging

Getting Started with C++ 21


Types of Errors

Getting Started with C++ 22


Type Purpose Example Error Message
Syntax Related to rules of the cout << "Welcome to NUST.\ error C2143: syntax error:
language n" missing ';' before 'return'
return 0;

Runtime Code is written correctly; int x = 9, y = 0; Unhandled exception at


trouble during execution int result = x / y; 0x00007FF7BE1F1C92 in
cout << "result = " << result; main.exe: 0xC0000094:
Integer division by zero.

Linker Due to wrong function int Main() { fatal error LNK1120: 1


prototyping, incorrect header //Output on the screen unresolved externals
files etc cout << "Welcome to
NUST.\n";
return 0;
}

Logical/Semantic incorrect design of a program int x = 3, y = 4; No errors here


– wrong results are produced int result = x + y / 2; In this case error is due to
cout << "result = " << result; arithmetic operators’
precedence
result = 5
Getting Started with C++ 23
Additional Resources
• Errors in C/C++ (GeeksforGeeks)
• Operators in C++ (Tutorialspoint)

• Programming and Problem Solving with C++, 7th Edition


• Chapter 3: Numeric Types, Expressions, and Output
• Section 3.4 Simple Arithmetic Expressions
• Chapter 5: Conditions, Logical Expressions, and Selection Control
Structures
• Section 5.5 Logical Operators

Errors and Operators 24


Algorithms, Pseudocode, Flow Charts

Operators, Algorithms, and Conditional Statements 25


What is an Algorithm?
An algorithm is a procedure consisting of a finite set
of instructions that provides a solution to a particular
problem

• Step-by-step procedure to solve a given problem

• Algorithm Representation
• Pseudocode
• Flowchart
Operators, Algorithms, and Conditional Statements 26
Pseudocode
• Pseudocode is a technique used to describe the steps of an
algorithm in a manner that's easy to understand for anyone with
basic programming knowledge

• Generic way of describing an algorithm without the use of any


specific programming language syntax

• English-like way to state an algorithm

Operators, Algorithms, and Conditional Statements 27


Example
• Write a pseudo code to convert the temperature entered by the user
from centigrade to Fahrenheit.

BEGIN
DISPLAY “Enter Temperature in Centigrade: ”
INPUT centigrade_value
CALCULATE Fahrenheit = (1.8 * centigrade_value) + 32
DISPLAY Fahrenheit
END

Operators, Algorithms, and Conditional Statements 28


Example
• Write a pseudocode for a program that takes three numbers as
input and determines the largest among them.
BEGIN
DISPLAY "Enter three numbers:"
INPUT a, b, c
IF a > b AND a > c THEN
DISPLAY "Largest number is: ", a
ELSE IF b > c THEN
DISPLAY "Largest number is: ", b
ELSE
DISPLAY "Largest number is: ", c
ENDIF
END

Operators, Algorithms, and Conditional Statements 29


Example
• Write a pseudocode for a program that generates the sum of the
first fifty natural numbers. S= n(n+1)/2​

BEGIN
n ← 50
sum ← (n * (n + 1)) / 2
DISPLAY "The sum of the first 50 natural numbers is:", sum
END

Operators, Algorithms, and Conditional Statements 30


Example
• Write a pseudocode for a program that generates the sum of the
first fifty natural numbers.

BEGIN
sum ← 0 // Initialize sum to 0
FOR i FROM 1 TO 50 DO
sum ← sum + i // Add current number to sum
ENDFOR
DISPLAY "The sum of the first 50 natural numbers is:", sum
END

Operators, Algorithms, and Conditional Statements 31


Flowchart
• Graphical representation of a process,
system, or computer algorithm

• It comprises special symbols or shapes,


connectors, and comments, that explain the
flow of the process

• Usually drawn from top to bottom

• Can have only one start and one stop


symbol
Operators, Algorithms, and Conditional Statements 32
Flowchart – Symbols
Terminal
• A rounded rectangle symbol indicates Start or Stop in a program’s logic
flow
• Terminal is the first and last symbol in the flowchart

Input / Output:
• A parallelogram denotes any function of input/output type
• Take input from input devices
• Display output on output devices

Processing:
• A rectangle represents processing & operations
• All arithmetic processes are indicated by the action or process symbol

Operators, Algorithms, and Conditional Statements 33


Flowchart – Symbols
Decision
• A diamond symbol represents a decision point
• Decision-based operations such as yes/no questions or true/false

Connectors:
• A circle symbol is used to connect multiple flowcharts
• Complex and large flowcharts are divided into smaller ones for easy
display.

Flow Lines:
• Arrow shows the direction of flow of instructions

Operators, Algorithms, and Conditional Statements 34


Flowchart – Example
• Task: Adding two numbers entered by a user Start

Start Declare variables num1,


num2 and sum
Declare variables num1, num2 and sum
Input num1 and
num2
Input values of num1, num2
Add num1, num2 and assign
Add num1 and num2 and assign the result to a value to sum
variable sum
Print sum
Display sum
Stop
Stop
Operators, Algorithms, and Conditional Statements 35

You might also like