Title: EE 222 Course 4 part 1
1EE 222 Course 4 - part 1
- Announcements
- Homeworks 1 and 2 were due Tuesday MIDNIGHT
- Next week classes (T and TH) Pr. P Mlesna
- You will work on a 1-week project
- get/print all course material from Vista
- IF it does not work I will send an email to every
one and let you know where to get the material
from - homework word (.doc) or pdf file
- Thanks!
2EE 222 Course 4 - part 1
- Ever notice that 'what the hell' is always the
right decision? - Marilyn Monroe
3EE 222 Course 4 - part 1
- Todays Menu
- Making decision flow control
- Conditional statements
- if, if-else statements
- switch
- Loops
- while, do-while, for loops
- Controlling loops with continue and break
4EE 222 Course 4
- Objectives
- Write proper flow charts
- is it a while or a do-while?
- Handle properly, master the use of
- repetition structures
- Selection structures
5EE 222 Course 4
- NOTE!!!!!!
- In the following
- boolean expression test condition whose result
is - true or false 0 or 1 (_____ 1 and
_____ 0) - Statement
- a single instruction ENDED by or
- a set of instructions all ENDED by
6Conditional statement if
- Syntax
- if (boolean expression)
- statement
-
- What it does
- Executes a statement/ block of code if and only
if some boolean expression is true. - Note indent the statement in the
- Flow chart look
7Conditional statement if -flow chart
8Conditional statement if -example
include using namespace std int
main() // Let's define the number that we
want the // player to guess as a constant
const int magicNumber 8 // Ask the player
for his guess cout between 1 and 10 " about..."
playerGuess // Give some feedback if the
guess is right if( playerGuess magicNumber
) cout right!" too smart for me!" done! return 0
9Conditional statement if-else
- Syntax
- if (boolean expression)
- statement
-
- else
- statement
-
- What it does
- Executes a statement/ block of code if and only
if some boolean expression is true AND . - Gives a 2nd alternative
- It is more user-friendly you know whats going
on - Flow chart look
10Conditional statement if-else flow chart
11Conditional statement if-else -example
if( playerGuess magicNumber ) cout
cout
12Conditional statements combination
- Chained if-else statements
- Nested conditional statements
if(boolean condition) statement else
if(boolean condition) statement else
if(boolean condition) statement
13Conditional statements (contd)Nested
conditional statements
- if( boolean condition )
-
- statement
- if(boolean condition)
- statement
-
- else if(boolean condition)
- statement
-
-
- else
-
- statement
-
14Conditional statement switch statement
- Syntax
- switch (expression)
- case expression_value1 statement
- case expression_value2 statement
- .
- case expression_valuen statement
- default statement
-
-
- NOTE expression can _____ be bool, int, char
- What it does
- Executes a statement/ block of code if and only
if one expression value is true - Its equivalent to several _____ statements but
its more neat
15Conditional statement switch statement
16Conditional statement switch statement example
- include
- using namespace std
- int main()
-
- // Let's read the grade
- cout
- char theGrade
- cin theGrade
- // Give an appropriate assessment
- switch( theGrade )
-
- case 'A'
- cout
- case 'B'
- case 'C'
- case 'D'
17Conditional statement switch statement example
(contd)
- Caution
- If you forget a _____ in a switch statement, the
computer may execute the _____ case statement.
18Loop the while loop
- Syntax
- while(boolean expression/ test condition)
- statement
-
- What it does
- Executes a statement/ block of code as long as
the test condition holds true. - Flow chart look
19Loop the while loop -flow chart
20Loop the while loop example
- include
- using namespace std
- int main()
-
- // Let's define the number that we want the
- // player to guess as a constant
- const int magicNumber 8
- // Let's initialize the player's guess with a
- // bogus value that is not the right choice
- int playerGuess -1
- // Then, ask the player to guess until he
- // gets it right
- cout
-
21Loop the while loop contd
- Notes
- If the test condition
- Is _____ right from the start the block will not
be executed even _____. - Is always true then its an _____ loop the
computer has no way to stop repeating the block
22Loop the do-while loop
- Syntax
- do
- statement
-
- while(boolean expression/ test condition)
- What it does
- Executes a statement/ block of code at least
_____as long as the test condition holds true. - Flow chart look
23Loop the do-while loop - flow chart
24Loop the do-while loop - example
- do
-
- cin playerGuess
- if( magicNumber playerGuess )
- cout
- else
- cout
-
- while( magicNumber ! playerGuess )
25Loop the for loop
- Syntax
- for (initialisation of control variable
test condition control variable update
) - statement
-
- What it does
- Combines __________, _____ and _____
- Executes a statement/ block of code as long as
the test condition holds true. - Flow chart look
26Loop the for loop - flow chart
27Loop the for loop -example
//single initialization for(index1
index playerGuess
if( magicNumber playerGuess ) cout "You're right! Congratulations!\n" else
cout initialization for(index1,ca indexindex,c) cin playerGuess if(
magicNumber playerGuess ) cout "You're right! Congratulations!\n" else
cout
You can rewrite a _____ loop as a _____ loop 28Combination loop, conditional statement
do // Ask the player for his guess
cin playerGuess // Give feedback
if( magicNumber playerGuess ) cout "You're right! Congratulations!\n" else
cout magicNumber ! playerGuess )
29Questions
- It helps me when
- It would help me if
- The question I have is
30Course Lab part 2
- Understand the problem
- organize /summarize the results of a problem
analysis IPO chart - Output ?
- Input ?
- Algorithm input processing?
- Flow chart
- And THEN .Programming
31Your Turn
- It helps me when
- It would help me if
- The question I have is