81
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical http://www.cs.odu.edu/~cs150/week _03/thursSept_10/exampleLabFinal / Template for project 1 is up Project 1 Due Date: Friday, September 25th 1

Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical cs150/week_03/t hursSept_10/exampleLabFinal/ cs150/week_03/t

Embed Size (px)

Citation preview

PowerPoint Presentation

Quiz 3 is due Friday September 18thLab 6 is going to be lab practicalhttp://www.cs.odu.edu/~cs150/week_03/thursSept_10/exampleLabFinal/

Template for project 1 is up Project 1 Due Date: Friday, September 25th

1

2Quiz 6 Attendance (Print your name on the paper given to you)3Control Structures I (Selection)4In this chapter, you will Discover how to use the selection control structures if, ifelseDiscover how to use a switch statement in a program

4Control StructuresA computer can proceed:In sequenceSelectively (branch): making a choiceRepetitively (iteratively): loopingBy calling a functionTwo most common control structures:SelectionRepetition

5A computer can proceed in one of the following ways in sequence meaning that statements are being executed sequentiallySelectively meaning by making a choice which is called a branch alsoRepetitively by executing an statement over and over again using a structure called loopOr by calling a function (next chapter)

5Control Structures (contd.)

6In sequential the computer starts at the beginning and follows the statements in order to the end (we saw that in chapter 1 and 2 examples) No choices are made, there is no repetitionControl structures are used to alter the sequential order of executionSelection and repetition are control structuresIn selection program executes particular statements depending on some conditionsIn repetition program repeats particular statements a certain number of times based on some conditions

6Selection: if and if...elseSelection or repetition requires execution of a logical expression:

Logical expression: An expression that evaluates to true or false.

8 is greater than 3 (is this logical expression?)

7Execution of selection or repetition requires execution of a logical expression:

78 > 3 (> is a relational operator in c++)

8== equality operator ( not the same as = assignment operator)== determines whether two expressions are equal= assigns the value of an expression to a variableEach of the relational operators are binary operators, it means it requires two operands. Because the result of a comparison is true, false, expressions using these operators always evaluate to true or false 8Relational Operators and Simple Data TypesRelational operators: Allow comparisonsRequire two operands (binary)Evaluate to true or false

Conditional statements: only executed if certain conditions are metCondition: represented by a logical (Boolean) expression that evaluates to a logical (Boolean) value

9Relational Operators and Simple Data Types (contd.)Relational operators can be used with all three simple data types:8 < 15 evaluates to true6 != 6 evaluates to false2.5 > 5.8 evaluates to false5.9 = 60)grade =P;

If the expression (score >= 60) evaluates to true the assignment statementgrade =P; executes

If the expression (score >= 60) evaluates to false ?

14In c++ any nonzero value is treated as true

If (39){Cout = 60grade=P;syntax error

if (score >= 60);grade=P;semantic error

statement in line 2 executes regardless of how the if statement evaluates17Because there is a semicolon at the end of the expression line 1 then if statement in line 1 terminates.So the statement in line 2 executes regardless of how the if statement evaluates.17Two-Way SelectionTwo-way selection syntax:

If expression is true, statement1 is executed; otherwise, statement2 is executedstatement1 and statement2 are any C++ statements

18Two-Way Selection (contd.)

19Whats wrong with this code?if ( score >=60 )cout= 21)(otherwise assigns false)

22New versions of c++ have a bool type 22How to combine logical expressions?weight > 180 and height < 6.023Logical (Boolean) Operators and Logical ExpressionsLogical (Boolean) operators: enable you to combine logical expressions

24There are situations where logical expression is a combination of two or more logical expressions 24The ! NOT Operator

25The && AND Operator

26The || OR Operator

27Order of Precedence

28

29

30Example Ch4_LogicalOperatorsExample4-1431Relational Operators and thestring TypeRelational operators can be applied to stringsStrings are compared character by character, starting with the first characterComparison continues until either a mismatch is found or all characters are found equalComparison is based on ASCII

32Relational Operators and thestring TypeIf two strings of different lengths are compared and the comparison is equal to the last character of the shorter stringThe shorter string is less than the larger string

33Suppose we have the following declarations:string str1 = "Hello";string str2 = "Hi";string str3 = "Air";string str4 = "Bill";string str4 = "Big";

34

35

36Compound (Block of) StatementsA compound statement functions like a single statement but contains one or more statements enclosed in curly braces

37Compound (Block of) Statements (contd.)if (age > 18){cout