2. Assignment Statements
• Standard Assignment Statement
– In an assignment statement, first the expression on
the right-hand side of the equal sign is evaluated and
then the variable on the left-hand side of the equal
sign is set equal to this value.
– Syntax:
Variable = Expression;
– Examples:
distance = rate * time;
count = count + 2;
5. Constants
• When you initialize a variable inside a
declaration, you can mark the variable so that
the program is not allowed to change its
value. To do this, place the word const in
front of the declaration.
• Syntax:
const Type_Name Variable_Name = Constant;
• Examples:
const int MAX_TRIES = 3;
const double PI = 3.14159;
7. Decision making
• Critical to computer programming
• Many situations where a choice between
two or more options is required
8. Condition
• The basis for writing our programmed
decision control
• Must always equate to TRUE or FALSE
– Boolean type ( bool )
9. Simple IF Statement
• If the condition is true, a statement is
executed; and if the condition is false,
nothing happens.
• Syntax:
if ( <condition> )
<statement> ;
11. Simple IF Statement
• Syntax:
if ( <condition> )
<statement> ;
• Example:
cin >> grade;
if ( grade >= 75 )
cout << “Congrats! You Passed.”;
12. Simple IF Statement
• In the case where we need to execute more
than one statements, we block the
statements.
• Syntax:
if ( <condition> )
{
<statement> ;
<statement> ;
}
13. Simple IF Statement
• Syntax:
if ( <condition> )
{
<statement> ;
<statement> ;
}
• Example
cin >> grade;
if ( grade >= 75 )
{
cout<<“Congrats.n”;
cout<<“You passed.”;
}
14. Let’s try…
• Write a program that will determine if a
person is legally an adult or not by reading
in his/her age.
20. Simple IF-ELSE Statement
• If the condition is true, a statement is
executed; and if the condition is false, a
different statement is executed.
• Syntax:
if ( <condition> )
<statement> ;
else
<statement> ;
23. Simple IF-ELSE Statement
• In the case where we
need to execute more
than one statements, we
block the statements.
• Syntax:
if ( <condition> )
{
<statement> ;
<statement> ;
}
else
{
<statement> ;
<statement> ;
}
26. Switch Statement
• Alternative to the long string of ifs
• Not as flexible as the if statement
• Useful when a single variable must be
compared to multiple values.
27. Nested ifs that display department name based on
department number
28.
29. Conditional Operator
• Yet another alternative to the if statement
• Here, the if operator (conditional operator)
is represented by a question mark (?)
32. Logical AND operator
• Use a logical AND operator to create a
compound Boolean expression in which
two conditions must be true for the entire
expression to evaluate as TRUE.
• Create by typing two ampersands (&&)
between two Boolean expressions.
35. Logical OR operator
• Used to create a compound Boolean
expression in which at least one of two
conditions must be true for the entire
expression to evaluate as TRUE.
• Created by typing two pipes (||) between
Boolean expressions.