Skip to content
CS-504 (B) · Object Oriented Programming/Quick Revision Short Notes

Object Oriented Programming (CS-504 (B)) - Unit 2 Short Notes

How unit 2 is examined

This unit covers C++ control flow (branching, looping, jumping) and the operators; the marks come from listing branching and looping statements, increment and decrement, membership operators, logical and arithmetic operators, break and continue, and the types of loops.

Control instructions

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Low weight</span>

Definition. <mark>Control instructions decide the order in which statements execute; C++ has sequence, selection (decision), repetition (loop) and jump control instructions.</mark>

Key points.

  1. Sequence control runs statements one after another in the order written.
  2. Branching (selection) statements choose a path on a condition: if, if-else, switch and the conditional operator ?:.
  3. Looping statements repeat a block: for, while and do-while.
  4. Jump statements transfer control unconditionally: break, continue and goto.
if (a > b) cout << a;          // branching
for (i = 0; i < 3; i++) ;      // looping
switch (n) { case 1: break; }  // branching, multiway

Answer frame. Open with the definition; list branching then looping then jump statements, each with one syntax line as above; close by saying each statement is detailed in its own topic below.

Asked: [7 marks] (Dec 2020) Write down various branching and looping statements available in C++.

if statement

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>The if statement executes a block only when its condition is true (non-zero).</mark>

Key points.

  1. Syntax: if (condition) { statements; }.
  2. Any non-zero value is true and zero is false.
  3. Without braces only the single next statement belongs to the if.
  4. A common bug is writing = instead of == inside the condition.

if-else statement

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>The if-else statement runs one block if the condition is true and another block if it is false.</mark>

Key points.

  1. Syntax: if (c) { A } else { B }; exactly one of A or B runs.
  2. Chains use else if to test several conditions in order; the first true one wins.
  3. Nested if is an if inside another if; an else pairs with the nearest unmatched if.

Use of logical operators

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>Logical operators combine or negate conditions and give true (1) or false (0).</mark>

Key points.

  1. && (AND) is true only when both operands are true.
  2. || (OR) is true when at least one operand is true.
  3. ! (NOT) reverses a truth value.
  4. && and || short-circuit: the right operand is skipped when the left already decides the result.
A B A && B A || B !A
0 0 0 0 1
0 1 0 1 1
1 0 0 1 0
1 1 1 1 0

Example. With x=10, y=3 and arithmetic + - * / %: x+y=13, x-y=7, x*y=30, x/y=3, x%y=1; (x>5 && y<2) is 0, (x>5 || y<2) is 1, !(x>5) is 0.

int x = 10, y = 3;
cout << x + y << " " << x % y << "\n";   // 13 1
cout << (x > 5 && y < 2) << (x > 5 || y < 2) << !(x > 5);  // 010

Asked: [7 marks] (Jun 2020) Explain Logical operators and Arithmetic operators with example.

Hierarchy of logical operators

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>Precedence order decides which logical operator is evaluated first: ! highest, then &&, then ||.</mark>

Key points.

  1. ! binds tighter than &&, which binds tighter than ||; relational operators bind tighter than all three.
  2. So a || b && c means a || (b && c).
  3. Associativity of && and || is left to right; of ! is right to left.
  4. Parentheses override precedence and make code clearer.

Arithmetic operators

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>Arithmetic operators perform mathematical calculation: +, -, *, / and %.</mark>

Key points.

  1. / on two integers gives an integer quotient (10/3 = 3); one float operand gives a float result.
  2. % gives the remainder and works only on integers (10%3 = 1).
  3. *, /, % have higher precedence than +, -, all left to right.
  4. Worked values are given under logical operators above.

Asked: [7 marks] (Jun 2020) Explain Logical operators and Arithmetic operators with example (see the example under Use of logical operators).

Relational operators

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>Relational operators compare two values and give 1 (true) or 0 (false).</mark>

Key points.

  1. The six operators are <, <=, >, >=, == and !=.
  2. == tests equality while = assigns.
  3. Relational operators have higher precedence than ==/!= for the ordering ones, and lower than arithmetic.
  4. They are used as conditions in if and loops.

Assignment operators

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. ==The assignment operator = stores the value of the right side in the variable on the left.==

Key points.

  1. Compound forms +=, -=, *=, /=, %= combine an operation with assignment: a += 5 means a = a + 5.
  2. Assignment associates right to left, so a = b = 3 sets both.
  3. The left side must be a modifiable variable (an lvalue).
  4. Assignment has very low precedence.

Increment and decrement operators

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Low weight</span>

Definition. <mark>++ adds 1 and -- subtracts 1 from a variable; the prefix form changes the value before it is used and the postfix form after.</mark>

Key points.

  1. Pre-increment ++a increments first, then the new value is used.
  2. Post-increment a++ uses the old value first, then increments.
  3. --a and a-- behave the same way for decrement.
  4. They work on variables only, never on constants such as 5++.
int a = 5, b;
b = a++; cout << a << " " << b;  // 6 5  (use, then add)
b = ++a; cout << a << " " << b;  // 7 7  (add, then use)
b = a--; cout << a << " " << b;  // 6 7
b = --a; cout << a << " " << b;  // 5 5

Answer frame. Open with the definition; explain prefix versus postfix in two lines; give the program above with its output; close with the difference (prefix: change then use, postfix: use then change).

Asked: [7 marks] (Jun 2020) Write a C++ program using increment and decrement operators with suitable example.

Conditional operator

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>The conditional (ternary) operator ?: is the only three-operand operator: condition ? expr1 : expr2.</mark>

Key points.

  1. If the condition is true expr1 is the result, otherwise expr2.
  2. It is a compact replacement for a simple if-else.
  3. Example: max = (a > b) ? a : b;.
  4. Both results should be of compatible types.

Bitwise operators

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>Bitwise operators work on the individual bits of integer operands: &, |, ^, ~, <<, >>.</mark>

Key points.

  1. & AND, | OR and ^ XOR compare bit by bit; ~ flips every bit.
  2. << shifts left (multiplies by 2 per place) and >> shifts right (divides by 2).
  3. For 6 (110) and 3 (011): 6&3=2, 6|3=7, 6^3=5, ~6=-7, 6<<1=12, 6>>1=3.
  4. Do not confuse & with the logical &&.

Special operators

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Low weight</span>

Definition. <mark>Special operators are & (address-of), * (indirection or pointer dereference), . and -> (member access) and sizeof (size in bytes).</mark>

Key points.

  1. &x gives the address of x, and *p gives the value stored at the address in pointer p.
  2. The membership operators are . (dot, through an object), -> (arrow, through a pointer to an object) and :: (scope resolution, through a class or namespace name).
  3. p->m is the same as (*p).m, and the operators work with both structures and classes.
  4. sizeof(type or variable) returns its size in bytes at compile time, for example sizeof(int) is 4 and sizeof(double) is 8 on common systems.
struct S { int m; } s, *p = &s;
s.m = 1;  p->m = 2;          // dot on object, arrow on pointer
// void A::show() { }        // :: names the class the function belongs to
cout << sizeof(int);         // 4

Answer frame. Open with "membership operators give access to members of a class or structure"; define ., -> and :: in turn; show the code above; close by naming when each is used.

Asked: [7 marks] (Dec 2020) What is mean by membership operator in C++?

Loops: while loop

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>A loop repeats a block while a condition holds; while (cond) { body } tests the condition before every pass, so it is entry-controlled and may run zero times.</mark>

Key points.

  1. Loop kinds: for, while (entry-controlled) and do-while (exit-controlled).
  2. Entry-controlled loops test first and can execute zero times; the exit-controlled do-while tests last and runs at least once.
  3. while needs initialisation before the loop and an update inside it, or it never ends.
  4. Example: int i=1; while(i<=3){ cout<<i<<" "; i++; } prints 1 2 3.

Asked: [7 marks] (Jun 2020) Explain various types of Loops used in C++ with suitable example (see while, for and do-while sections).

for loop

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>The for loop packs initialisation, condition and update in one line: for (init; cond; update) { body }.</mark>

Key points.

  1. Initialisation runs once, then the condition is tested before each pass, and the update runs after each pass.
  2. It is entry-controlled and best when the number of repetitions is known.
  3. Example: for(int i=1;i<=3;i++) cout<<i<<" "; prints 1 2 3.
  4. for(;;) is an infinite loop.

do-while loop

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>The do-while loop executes its body first and then tests the condition: do { body } while (cond);.</mark>

Key points.

  1. It is exit-controlled, so the body runs at least once even when the condition is false.
  2. A semicolon must follow the closing while(cond).
  3. Example: int i=0; do { cout<<i; i++; } while(i<0); prints 0, while a while loop would print nothing.

Odd loop

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>An odd loop is a loop that does not follow the fixed for-count pattern: it runs an unknown number of times, or uses several variables or an unusual exit.</mark>

Key points.

  1. Typical forms are while(1) or for(;;) left only by break, or a loop that repeats until the user types a stop value.
  2. The number of passes is not known beforehand.
  3. The condition can be omitted or moved to the body, with break ending the loop.

Nested loop

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>A nested loop is a loop placed inside the body of another loop.</mark>

Key points.

  1. For each pass of the outer loop the inner loop runs completely.
  2. Total passes are the outer count times the inner count.
  3. It is used for tables, matrices and patterns.
  4. Example: outer i=1..2, inner j=1..2 gives pairs 11 12 21 22.

break

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>break immediately leaves the nearest enclosing loop or switch.</mark>

Key points.

  1. Syntax is break; and control moves to the statement after the loop or switch.
  2. In switch it stops fall-through to the next case.
  3. In nested loops it exits only the innermost loop.
for (int i = 1; i <= 5; i++) {
    if (i == 2) continue;   // skip 2
    if (i == 4) break;      // leave loop at 4
    cout << i << " ";
}                           // 1 3

Asked: [7 marks] (Jun 2020) Explain Break and Continue keyword with appropriate program (see the program above and continue below).

continue

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>continue skips the rest of the current pass and jumps to the next iteration of the loop.</mark>

Key points.

  1. Syntax is continue;; in for it goes to the update expression, in while and do-while to the condition test.
  2. Unlike break, the loop itself does not end.
  3. In the program above, continue skips 2, break stops at 4, so the output is 1 3.

Case control structure

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>switch selects one of many blocks by matching an integer or character expression with case labels.</mark>

Key points.

  1. Syntax: switch(expr) { case 1: ...; break; case 2: ...; break; default: ...; }.
  2. Case values must be integer or character constants and unique.
  3. Without break execution falls through into the following cases.
  4. default runs when no case matches.

goto

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>goto label; transfers control unconditionally to the statement marked label: in the same function.</mark>

Key points.

  1. Example: goto end; ... end: cout<<"done";.
  2. It breaks structured flow and makes programs hard to follow, so it is avoided.
  3. It cannot jump into another function.

Exit statement

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>exit(status) from <cstdlib> terminates the whole program immediately.</mark>

Key points.

  1. exit(0) means normal termination and a non-zero value signals an error.
  2. It ends the program from any function, unlike break or return.
  3. It closes open files and calls static destructors.

Last-minute revision

  • Branching: if, if-else, switch, ?:; looping: for, while, do-while; jumps: break, continue, goto.
  • ++a changes then uses; a++ uses then changes: from a=5, b=a++ gives a=6, b=5 and b=++a gives a=7, b=7.
  • && needs both true, || needs one, ! reverses; precedence ! then && then ||.
  • With 10 and 3: / gives 3 and % gives 1; % works only on integers.
  • Membership operators: . object, -> pointer, :: scope resolution.
  • while and for are entry-controlled; do-while is exit-controlled and runs at least once.
  • break leaves the loop; continue skips to the next iteration.
  • 6&3=2, 6|3=7, 6^3=5, ~6=-7, 6<<1=12, 6>>1=3.
  • switch needs break to stop fall-through; exit(0) ends the program.

Memory hooks

  • Pre = change then use; post = use then change.
  • NOT, AND, OR: highest to lowest binding.
  • Dot for the object, arrow for the pointer.
  • While looks first, do-while leaps first.
  • Break breaks out, continue carries on.

Coverage checklist

  • control instructions: Dec 2020 branching and looping statements.
  • if
  • if-else
  • use of logical operator: Jun 2020 logical and arithmetic operators.
  • hierarchy of logical operators
  • arithmetic operators: Jun 2020 logical and arithmetic operators.
  • relational operators
  • assignment operators
  • increment and decrement operators: Jun 2020 program.
  • conditional operators
  • bit wise operators
  • special operators, “&,*,.,>, “sizeof”: Dec 2020 membership operator.
  • Loops control structure: while loop: Jun 2020 types of loops.
  • for loop
  • do – while loop
  • odd loop
  • nested loop
  • break: Jun 2020 break and continue.
  • continue: Jun 2020 break and continue.
  • case control structure
  • go to
  • exit statement
Go to where you left off?

Quick Add to Notes

Save questions, your own notes and screenshots into notes filed by unit. It takes a free account.

Create free account

Have an account? Log in