slide 1 summary two basic concepts: variables and assignments some c++ practical issues: division...

24
Slide 1 Summary asic concepts: variables and assignments C++ practical issues: division rule, operator precedence Sequential structure of a programme

Post on 19-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 1

Summary

Two basic concepts:

variables and assignments

Some C++ practical issues:

division rule, operator precedence

Sequential structure of a programme

Page 2: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 2

Expression and statement

An expression has a value which is the result of some operation(s) on the associated operands.

4, x-y, 2-a-(b*c)

A statement is a sentence that acts as a commandit does not have a valueit always ends in a ‘;’

cin >> x;x = 5;int x;

Page 3: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Boolean type and expressions

Page 4: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 4

Boolean type: bool C++ contains a type (new!!!) named bool

which can have one of two values

true (corresponding to non-zero value)

false (corresponding to zero value)

Boolean operators

Logical and: &&

Logical or: ||

Logical not: !

Examples

bool P = true;bool Q = false;bool R = true;bool S = P && Q;bool T = (!Q) || R;bool U = !(R && !Q);

Page 5: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 5

How to create a boolean expression for a test?

Using relational operators Using boolean (logical) operators

An expression (a boolean expression) for a test has one of the two values: true or false.

Page 6: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 6

Using Relational Operators

Relational operators are used to compare two values

Math C++ Plain English

= == equals [example: if(a==b) ]

[ (a=b) means put the value of b into a ]

< < less than

<= less than or equal to

> > greater than

>= greater than or equal to

!= not equal to

Page 7: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 7

Relational Expressions

Examples:

numberOfStudents < 200

10 > 20

20 * j == 10 + i

Page 8: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 8

Using Boolean (logical) operators

Logical AND operator && Logical OR operator || Logical NOT operator !

Examples: (x>5) && (x<10)

(x>10) || (x<5)

!(x>5)

Warning! & and | are also operators

Boolean operators can be used to form more complex conditional expressions

Page 9: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 9

Operator Precedence

Which comes first?

* / %

+ -

< <= >= >

== !=

=

Answer:

Page 10: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 10

Summary of Operator Precedence

• Precedence of operators (from highest to lowest) Parentheses ( … ) Unary operators ! Multiplicative operators * / % Additive operators + - Relational ordering < <= >= > Relational equality == != Logical and && Logical or || Assignment =

arithmetic

relational

logical

Page 11: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 11

5 != 6 || 7 <= 3

(5 !=6) || (7 <= 3)

5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24

Example:

Page 12: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

if Statements

Page 13: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 13

Part I: basics

Page 14: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 14

Introduction

• Sequence Statements in the given order

• Conditional statement (branching) Chooses between two (or more) sequences depending on some condition

if <condition exists> {

<do P>}

else { <do Q> }

• Iteration statement (looping) repetitively execute a given sequence

while <condition exists> {

<do P>}

Three program structures or constructs:

Page 15: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 15

Structured programming

Any program can be written as a sequence of three basic program structures!!!

1. sequences,

2. conditionals,

3. and iterations

Page 16: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 16

The fundamental if-else Statement

• Syntaxif (Expression)

Action1else Action2

• If Expression is true thenexecute Action1 otherwiseexecute Action2

• Exampleif(v == 0)

cout << "v is 0";else

cout << "v is not 0";

Expression

Action1 Action2

true false

Choose between two alternative actions

depending on a test (on the values of variables).

Page 17: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 17

if <it's sunny>{

<go to beach with sun block>

}

else{

<go to beach with umbrella>

}

It’s common in everyday life …

?

Page 18: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 18

Example: Absolute Value (1st )// program to read number & print its absolute value#include <iostream>using namespace std;int main(){

int value; int absvalue;

cout << "Enter integer: ";cin >> value;if (value < 0)

absvalue = -value; else absvalue = value;

cout << "The absolute value is " << absvalue << endl;return 0;

}

Page 19: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 19

When the action is more than one statement …

Put multiple action statements within braces

if <it's raining> {<take umbrella><wear raincoat>

}else { <take sunbathing stuff>}

Page 20: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 20

Example: Absolute Value (2nd)// program to read number & print its absolute value#include <iostream>using namespace std;int main(){

int value; int absvalue; // absolute value

cout << "Enter integer: ";cin >> value;if (value < 0) {

absvalue = -value; cout << "The input value is negative and its absolute

value is " << absvalue << endl; } else { absvalue = value;

cout << "The input value is positive and its absolute value is " << absvalue << endl;

}return 0;

}

Page 21: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 21

Boolean expressions Arithmetic expression: use arithmetic operators

+,-,*,/, to produce a number as the final result Boolean expression: use relational operators <,>, ==,

… and boolean operators AND (&&), OR (||), NOT (!) to produce one of the two values true (1) and false (0) as the final result

New type: bool, true, false

Example: bool cond;

cond = true;

cond = (x>y);

Old versions of C++, simulated boolean type by int with 0/1

Page 22: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 22

Summary of if-else Statement

A

if (cond1)

Belse CD

A A

B or C

D D

=

Page 23: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 23

Nested if-else Statements Nested means that one complete statement is inside

anotherif cond1 {

A;if cond2 {

B;

} else {

C }

else { D

}

Page 24: Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a

Slide 24

double score;cin >> score;

if(score >= 90.0)cout << "Grade = A" << endl;

else if(score >= 80.0) cout << "Grade = B" << endl; else if(score >= 70.0)

cout << "Grade = C" << endl;else if(score >= 60.0)

cout << "Grade = D" << endl; else

cout << "Grade = F" << endl;

Example