The University of Iowa
The College of Liberal Arts and Sciences
Department of Computer Science

Programming Languages and Tools:

CS:3210:0001

Lecture/Lab #7

Programming with C++

Compound assignment, increment/decrement, control flow

Warm-up

  • What's the difference between x && y and x & y?

  • What will be the output of the following program?

int main() {
    std::cout 
        << std::boolalpha 
        << !( 2 > 4 ) 
        << "\n";
}
  • What does the x << y expression do?

Compound assignment

x = x + n;
x = x - n;
x = x * n;
x = x / n;
x = x % n;
x = x & n;
x = x | n;
x = x ^ n;
x = x << n;
x = x >> n;
x += n;
x -= n;
x *= n;
x /= n;
x %= n;
x &= n;
x |= n;
x ^= n;
x <<= n;
x >>= n;

Is there a better way to express these?

Yes!

Increment/decrement

x += 1;
x -= 1;

Is there a better way to express these?

Yes!

++x;
--x;

OR...

x++;
x--;
  • Pre-increment and pre-decrement operators increment or decrement the value of the object and return a reference to the result

  • Post-increment and post-decrement create a copy of the object, increment or decrement the value of the object and return the copy from before the increment or decrement

Control flow overview

if ( <condition> ) <statement> [else <statement>]

do <statement> while ( <condition> ) 

while ( <condition> ) <statement>

for ( <init-statement>; <condition>; <expression> ) <statement>

switch ( <condition> ) {

    ( ( case <constant> | default ): <statement> )+ 

}

If statement

if ( <condition> ) <statement> [else <statement>]

// the product of an integer and 
// all the integers before it

int factorial( int n )
{
    if ( n == 0 )
        return 1;
    else
        return n * factorial( n - 1 );
}

Conditionally executes the first or the second statement.

Exercise 1

Write a program that asks user for a number and prints "Yay" or "Nay" depending on the condition

  • Open the exercise template

  • Write your code, press Run to test

  • When you're done, grab your Repl's link and send it as a direct message to me (agurtovoy)

  • Click on the corresponding option in the "Lab7 exercises" poll in #general

while statement

while ( <condition> ) <statement>

// the product of an integer and 
// all the integers before it

int factorial( int n )
{
    int result = 1;
    while ( n > 0 )
    {
        result = result * n;
        n = n - 1;
    }

    return result;
}

Executes statement repeatedly, until condition becomes false. The condition is tested before each iteration.

do-while loop

int prompt_for_positive()
{
    int n = -1;
    do
    {
        std::cout << "Enter a positive number: ";
        std::cin >> n;
    }
    while ( n <= 0 );

    return n;
}

do <statement> while ( <condition> ) 

Executes statement repeatedly, until condition becomes false. The condition is tested after each iteration.

Exercise 2

Write a program that asks user for a positive integer and prints out all the integers between zero and the input

( [0, input) )

  • Open the exercise template

  • Write your code, press Run to test

  • When you're done, grab your Repl's link and send it as a direct message to me (agurtovoy)

  • Click on the corresponding option in the "Lab7 exercises" poll in #general

for loop

#include <iostream>

int main()
{
    for ( int i = 0; i < 10; ++i )
        std::cout << i << " ";
}

Executes init-statement once, then executes statement and expression repeatedly, until the condition becomes false. The condition is tested before each iteration.

for ( <init-statement>; <condition>; <expression> ) <statement>

Exercise 3

  • Open the exercise template

  • Write your code, press Run to test

  • When you're done, grab your Repl's link and send it as a direct message to me (agurtovoy)

  • Click on the corresponding option in the "Lab7 exercises" poll in #general

Write a program that asks user for a positive integer and uses `for` loop to print out all the even numbers between zero and the input

switch statement

char c = 0;
std::cin >> c;
switch ( c )
{
    case 'y':
    case 'Y':
        return true;

    case 'n':
    case 'N':
        return false;

    default:
        return default_;
}

switch ( <condition> ) {

    ( ( case <constant> | default ): <statement> )+ 

}

Transfers control to one of the several statements, depending on the value of condition.

Exercise 4

Write a program that asks user to make a selection from three items and uses `switch` statement to print out the selected item

  • Open the exercise template

  • Write your code, press Run to test

  • When you're done, grab your Repl's link and send it as a direct message to me (agurtovoy)

  • Click on the corresponding option in the "Lab7 exercises" poll in #general

Programming with C++, Spring 2020, Lecture #7

By Aleksey Gurtovoy

Programming with C++, Spring 2020, Lecture #7

Compound assignment, increment/decrement, control flow

  • 622