0% found this document useful (0 votes)
59 views27 pages

Introduction To Programming: 3 Statements

This document provides an introduction to programming concepts like expressions, statements, operators, input/output and the if statement. It explains that a program executes statements from top to bottom, but conditional statements like if can change the execution order. Expressions return values and can be used on both sides of assignments. Operators perform math operations on values. Input is read with cin and output written with cout. The if statement executes code only if a condition is true, and else can specify an alternative if the condition is false.

Uploaded by

Orxan Qayibov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views27 pages

Introduction To Programming: 3 Statements

This document provides an introduction to programming concepts like expressions, statements, operators, input/output and the if statement. It explains that a program executes statements from top to bottom, but conditional statements like if can change the execution order. Expressions return values and can be used on both sides of assignments. Operators perform math operations on values. Input is read with cin and output written with cout. The if statement executes code only if a condition is true, and else can specify an alternative if the condition is false.

Uploaded by

Orxan Qayibov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Introduction to Programming

Summer Term

3 Statements

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 48
3 Statements ...

Contents
➥ Expressions
➥ Operators
➥ Input and output
➥ if statement

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 49
3 Statements ...

Statements
➥ Variables are the program’s data
➥ Statements specify what to do with the data
➥ Different kinds of statements:
➥ each expression is a statement
➥ always terminated with a ;
➥ e.g. assignment: x = a + b;
➥ compound statement: { list-of-statements }
➥ empty statement: (white space and/or ; )

➥ A program executes main()’s compound statement, from the


begining to the end

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 50
3.1 Expressions

Expressions
➥ Anything that returns a value is an expression in C++
➥ An operator is a symbol that causes the program to perform an
operation (e.g., + for the summation)

➥ The assignment operator = changes the value of the left


operand to the value of the expression on the right side
➥ Example: x = a + b;

➥ Note: in C++, an assignment again is an expression


➥ Example: x = (y = a) + b; (bad programming style!)

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 51
3.1 Expressions ...

l-values and r-values


➥ l-value: anything that can be used on the left side of an
assignment
➥ i.e. everything that represents a storage that can be altered
➥ e.g. a variable

➥ r-value: anything that can be used on the right side of an


assignment
➥ i.e. everything that has / returns a value
➥ e.g. an expression

➥ (all l-values are also r-values)

➥ Example: 35 = x is not correct, 35 is not an l-value

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 52
3.1 Expressions ...

Mathematical operators

➥ On floats: + - * /

➥ On integers: + - * / % (plus some more ...)


➥ / is the integer division: 21 / 4 → 5

➥ % is the modulo operation: 21 % 4 → 1

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 53
3.1 Expressions ...

Combined assignment and arithmetic


➥ Common case of an assignment: myAge = myAge + 2;
➥ Short form: myAge += 2;

➥ Also defined: -= *= /= %=

➥ Beware: if the meaning of the combined operators is not obvious,


you better write it explicitly!

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 54
3.1 Expressions ...

Increment and decrement


➥ c = c + 1 is the same as c++ ⇒ language name!
➥ c = c - 1 is the same as c--
➥ ++ and -- are operators with a side effect

➥ Beware: prefix and postfix operators:


➥ Prefix: ++c returns the incremented value
➥ Postfix: c++ returns the old (not yet incremented) value

➥ Examples:
➥ x = 5; a = x++; afterwards, a is 5, and x is 6
➥ x = 5; a = ++x; afterwards, a is 6, and x is 6

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 55
3.1 Expressions ...

Precedence
➥ Defines the order in which operators are evaluated

➥ * / % have higher precedence (“priority”) than + -


➥ arithmetic operators with the same precedence go from left to
right
➥ assignment operators go from right to left!

➥ parentheses ( ) override precedence

Examples:
➥ x = 5 + 3 - 8 * 9; ≡ x = (5 + 3) - (8 * 9);
➥ x = y = z = 0; ≡ x = (y = (z = 0));
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 56
3.1 Expressions ...

Precedence ...

1. Use precendence only for * / % + - (common sense)


2. Forget about the rest of precendence rules and use parentheses!

☞ http://en.cppreference.com/w/cpp/language/operator precedence

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 57
3.1 Expressions ...

Bit operations
➥ Reminder: in a computer, all data (like, e.g., numbers) is stored in
a binary form
➥ e.g., 291 = 0 ... 0 0 0 0 1 0 0 1 0 0 0 1 1

➥ C++ defines some operators in integers, which directly work on


the bit representation
➥ ~ & | ^ : bitwise NOT, AND, OR, and XOR
➥ << >> : left shift, right shift
➥ Examples:
➥ 1 | 2 equals 3, 11 & ~1 equals 10
➥ 1 << 4 equals 16, 31 >> 1 equals 15

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 58
3.1 Expressions ...

Use of bit operations


➥ Setting / resetting a bit (e.g., in a control register of a hardware
device)
➥ Examples:
➥ x |= 128; sets bit 7 in x
➥ x &= ~16; resets bit 4 in x
➥ Extracting bit-fields
➥ e.g.: assume that bits 4 .. 7 of a value x contain an (unsigned)
4-bit number
y = (x >> 4) & 15; stores the value of this field into y
➥ Fast division and multiplication
➥ x << 2 is the same as x * 4
➥ x >> 1 is the same as x / 2, if x is positive
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 59
3.1 Expressions ...

Relational operators

name op example result example result


equals == 100 == 50 false 100 == 100 true
not equals != 100 != 50 true 100 != 100 false
greater than > 100 > 50 true 100 > 100 false
greater or equal >= 100 >= 50 true 100 >= 100 true
less than < 100 < 50 false 100 < 100 false
less or equal <= 100 <= 50 false 100 <= 100 true

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 60
3.2 Input and Output

Writing to the terminal window


➥ You can print the value of one or more expressions:
cout << expression ;
cout << expression1 << expression2 ;
...
➥ Printing a line feed: cout << endl;
➥ Example:
cout << 5 << 7 << "Hello" << endl;
cout << "17 + 4 " << ’=’ << " " << 17 + 4;
cout << " and PI = " << 3.1415926 << endl;
This code will print:
57Hello
17 + 4 = 21 and PI = 3.14159
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 61
3.2 Input and Output ...

Reading from the terminal window


➥ You can read a value from the terminal into a variable:
cin >> variable ;
➥ The statement reads the maximum number of characters
appropriate for the varible’s type
➥ usually, input is delimited by the return-key (new-line)
➥ if no legal characters could be read, the variable is not
assigned (☞ 10)
➥ Example:
int i, j; char c; double d;
cin >> i; cin >> j; cin >> c; cin >> d;
When the user types 30-421.5.2, the result will be
i = 30; j = -421; c = ’.’; d = 5.2;
Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 62
3.2 Input and Output ...

Using input and output in a program


➥ You have to include the file with the declarations of the variables
cin and cout:
#include <iostream>
➥ The names cin and cout live in a namespace called std, thus,
you should declare
using std::cin;
using std::cout;

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 63
3.3 The if Statement

The if statement
➥ Normally, statements are executed in the order they appear in the
program (top to bottom).
➥ Sometimes, statements shall only be executed if some condition
holds:
if ( expression ) // If condition is true,
statement; // execute the statement
➥ Example:
if ( new_value < minimum )
minimum = new_value;
➥ Note that statement; can be replaced by a compound statement

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 64
3.3 The if Statement ...

The else clause


➥ Often, some statement shall be excuted if a condition holds, and
some other code, if not
➥ Example:
if ( a < b )
minimum = a;
if ( b < a )
minimum = b; // What is wrong here?

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 65
3.3 The if Statement ...

The else clause ...


➥ Often, some statement shall be excuted if a condition holds, and
some other code, if not:
if ( expression ) // If the condition is true,
statement1 ; // execute statement1 ,
else // if not,
statement2 ; // execute statement2 .
➥ Example:
if ( a < b )
minimum = a;
else
minimum = b; // also if a == b!

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 66
3.3 The if Statement ...

Expressions used for if


➥ Every numerical value is “allowed”:
➥ 0 means false
➥ everything else (usually 1) means true
(That is a “heritage” from the C language . . . )
➥ However: only use meaningful (i.e., Boolean) expressions!
➥ For example, use if ( minimum != 0 ) ...
rather than if ( minimum ) ...

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 67
3.3 The if Statement ...

Nested if statements
➥ What does this program print to cout?
int main() {
bool a = true, b = false;
if ( a )
if ( b )
cout << "a and b\n";
else
cout << "not a\n";
return 0;
}
➥ Always use { and } for if and else clauses!

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 68
3.3 The if Statement ...

Nested if statements ...


➥ The corrected program:
int main() {
bool a = true, b = false;
if ( a ) {
if ( b ) {
cout << "a and b\n";
}
}
else {
cout << "not a\n";
}
return 0;
}

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 69
3.3 The if Statement ...

More complex conditions


➥ Logical Operators:
Operator Symbol Usage
and && expression1 && expression2
or || expression1 || expression2
not ! !expression
➥ Examples:
➥ if ( (x == 5) && (y == 5) ) ...
➥ if ( (x == 5) || (y == 5) ) ...
➥ if ( !(x == 5) ) ...
➥ same as if ( x != 5 ) ...

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 70
Summary

Statements
➥ Variables are the program’s data
➥ Statements specify what to do with the data
➥ Different kinds of statements:
➥ each expression is a statement
➥ always terminated with a ;
➥ e.g. assignment: x = a + b;
➥ compound statement: { list-of-statements }
➥ empty statement: {} or just a ;

➥ A program executes main()’s compound statement, from the


begining to the end

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 71
Summary ...

Expressions
➥ Anything that returns a value is an expression in C++
➥ An operator is a symbol that causes the program to perform an
operation (e.g., + for the summation)

➥ The assignment operator = changes the value of the left


operand to the value of the expression on the right side
➥ Examples:
3
x = a + b * (c - 34) % 2
myAge += 2
c++ + ++d
x | (1 << 7)

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 72
Summary ...

Reminder: if Statement
int main() {
bool a = true, b = false;
if ( a ) {
if ( b ) {
cout << "a and b\n";
}
}
else {
cout << "not a\n";
}
return 0;
}

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 73
Summary ...

What we have so far


➥ Variables: data types and names
➥ Statements: operations on data (e.g., if)
➥ cout and cin
➥ The main function

Now: functions in detail

Ubiquitous
Computing
Prof. Dr. Kristof Van Laerhoven
Ubiquitous Computing Laboratory Introduction to Programming (chapter 4/13) 74

You might also like