25
Lecture 2 Control Structure

Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Embed Size (px)

Citation preview

Page 1: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Lecture 2

Control Structure

Page 2: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Relational Operators

-- From the previous lecture

Relational Operator Meaning

== is equal to

< is less than

> is greater than

<= is less than or equal to

>= is greater than or equal to

!= is not equal to

Page 3: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Logical Operators (&&, ||, !)

• Used for more complex conditions than the other operators

• Helps combine multiple conditions– Logical AND – Condition1 && Condition2– Logical OR – Condition1 || Condition2

• Logical Negation – to reverse condition– if (!(grade == “A”))

• Can be replaced with relational op (!=)– if (grade != “A”)

1

Page 4: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Control Structures

Control Structures• Selection Structures• Looping Structures

Page 5: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Control Structures

• Algorithms – Procedure for solving a problem in terms of

• The actions to be executed

• The order in which actions are to be executed

• Pseudocode– Artificial and informal language that helps

programmers develop algorithms

• Flowchart– Graphical representation of an algorithm

Page 6: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Control Structures• Sequential Execution

– Statements executed one after the other in order in which they are written

Add grade to total

Add 1 to counter

total = total + grade;counter = counter + 1;

3

Page 7: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Control Structures

• Selection Structures– Used to choose among alternative courses of action

– if selection structure• Single selection structure

– if /else selection structure• Double selection structure

– switch selection structure• Multiple selection structure

4

Page 8: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Control Structures

• Repetition Structures– Used to repeat an action while a condition

remains true

– for repetition structure– while repetition structure– do/while repetition structure

5

Page 9: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Selection Structures

• if selection structure– Performs an indicated action when the

condition is true.

“Passed”grade >= 60true

false

if (grade >= 60)cout<<“Passed”;

6

Page 10: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Statement 1

expression Statement 2

Statement 3

true

false

Page 11: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Selection Structures

• if/else selection structure– Performs an action when the condition is true and

another when the condition is false.

if (grade >= 60)printf (“Passed”);

elseprintf (“Failed”);

print “Passed”grade >= 60truefalse

print “Failed”

7

Page 12: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Form and Syntax

If (<Expression>)

<statement>

else

<statement>

Page 13: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

int main()

{

int num, neg ,pos;

cout<<“Please enter the no.”;

cin>> num;

if (num < 0)

neg= neg +1;

else

pos= pos +1;

}

Page 14: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Conditional Operator

• Closely related to if/else structure• ?: (Conditional Operator)• Only ternary operator in C

– Takes three operands

• Operands with conditional operator form conditional expression– Operand 1– condition– Operand 2 – action if condition true– Operand 3 – action if condition false

8

Page 15: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Conditional Operator

print “Passed”grade >= 60truefalse

print “Failed”

grade >= 60 ? “Passed”: “Failed”

9

Page 16: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

if (Expression1 )

Statement1

else if (Expression2 )

Statement2...

else if (ExpressionN )

StatementN

else

Statement N+1

EXACTLY 1 of these statements will be executed.

Nested if statements

Page 17: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Nested if/else//nested if/else//printing grades of students

if (grade >= 90)cout<<“A”;

elseif (grade >= 80)

cout<<“B”;else

if (grade >= 70)cout<<“C”;

elseif (grade >= 60)

cout<<“D”;else

cout<<“Failed”;

10

Page 18: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Nested if/else

//nested if/else//printing grades of

students

if (grade >= 90)cout<<“A”;

else if (grade >= 80)cout<<“B”;

else if (grade >= 70)cout<<“C”;

else if (grade >= 60)cout<<“D”;

elsecout<<“Failed”;

If grade of a student is 84, the conditionsfor “B”, “C” and “D”are all true. What will be printed?

11

Page 19: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Selection Structures

• switch selection structure– Multiple selection structure is useful when

an algorithm contains a series of decisions – Variable or expression is tested separately

for one of several possible values– Each value represents a different action

12

Page 20: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Switch Selection Structure

// counting grades assignedint aCount, bCount, cCount, dCount,

fCount;switch (grade){

case ‘A’: case ‘a’:++aCount;

case ‘B’: case ‘b’:++bCount;

case ‘C’: case ‘c’:++cCount;

case ‘D’: case ‘d’:++dCount;

default:++fCount;

}

If grade has value ‘A’, which variable is incremented?

13

Page 21: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

switch Selection Structure// counting grades assignedint aCount, bCount, cCount, dCount, fCount;switch (grade){

case ‘A’: case ‘a’:++aCount;break;

case ‘B’: case ‘b’:++bCount;break;

case ‘C’: case ‘c’:++cCount;break;

case ‘D’: case ‘d’:++dCount;break;

default:++fCount;

} 14

Page 22: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

break Statement

• break statement causes program control to move to the first statement after the selection/ repetition structure

• Switch cases would otherwise run together

15

Page 23: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

switch Selection Structure

case a actioncase atrue

false

break

case b actioncase btrue

break

default action

false

16

Page 24: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Summary of precedence order (revised)

Operator Associativity() left to right

++ -- ! right to left * / % left to right + - left to right< <= > >= left to right == != left to right && left to right || left to right ?: left to right

= += -= *= /= %= right to left, left to right

25

Page 25: Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater

Control Structures

• C++ keywords– Cannot be used as identifiers or variable namesC++ Keywords

Keywords common to the C and C++ programming languages

auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while C++ only keywords

asm bool catch class const_cast delete dynamic_cast explicit false friend inline mutable namespace new operator private protected public reinterpret_cast static_cast template this throw true try typeid typename using virtual wchar_t