Professional Documents
Culture Documents
If, If... Else, While
If, If... Else, While
Control Statements
(if, if…else, while)
Example:
Algorithm to add two numbers entered by user
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
A simple flowchart representing a process for
dealing with a non-functioning lamp.
Pseudocode
• Pseudocode (or “fake” code) is an artificial and informal language
that helps you develop algorithms.
• Similar to everyday English; convenient and user friendly.
• Helps you “think out” a program before attempting to write it.
• Carefully prepared pseudocode can easily be converted to a
corresponding C++ program.
• Normally describes only executable statements – no declarations
You could omit the braces { }, if there is only one statement inside the block
Conditional flow control
Syntax Example Flowchart
if and if…else statements
• if selection statement
• single-selection statement
• either performs (selects) an action if a condition (predicate) is
true or skips the action if the condition is false.
• if…else selection statement
• double-selection statement
• performs an action if a condition is true or performs a different
action if the condition is false.
Nested if…else statement
• test for multiple cases by placing if…else selection statements inside other if…
else selection statements.
• In a nested if... else statement, test the conditions that are more likely to be true
at the beginning of the nested if...else statement. This will enable the nested
if...else statement to run faster and exit earlier than testing infrequently occurring
cases first.
Example:
If student’s grade is greater than or equal to 90
Print “A”
Else
If student’s grade is greater than or equal to 80
Print “B”
Else
If student’s grade is greater than or equal to 70
Print “C”
Else
If student’s grade is greater than or equal to 60
Print “D”
Else
Print “F”
Relational & logical operators
• Very often, you need to compare two values before deciding on the
action to be taken.
== Equal to expr1 == expr2
Example:
!= Not Equal to expr1 != expr2 if (x >= 2 && x < 10)
> Greater than expr1 > expr2 cout << x;
Relational operators
&& Logical AND expr1 && expr2 AND (&&) OR (||) XOR (^) NOT (!)
F && T → F F || T → T F^T→T !F → T
! Logical NOT !expr
T && F → F T || F → T T^F→T
^ Logical expr1 ^ expr2
XOR F && F → F F || F → F F^F→F
Conditioner operator (? :)
• is closely related to the if…else statement
• The operands, together with the conditional operator, form a
conditional expression.
• take three operands:
• first operand is a condition,
• second operand is the value for the entire conditional expression
if the condition is true
• third operand is the value for the entire conditional expression if
the condition is false.
conditional expression
Dangling-else problem
if ( x > 5 )
which if if ( y > 5 )
does this cout << "x and y are > 5";
else
else
belongs to?
cout << "x is <= 5";
int main() {
int grade;
cout << “Please input a student’s grade: ”;
cin >> grade;
if (grade > 50)
cout << “Passed” << endl;
else
cout << “Failed” << endl;
}
int main() {
int grade;
int total = 0; // initialize sum of grades entered by the user
int gradeCounter = 1; //initialize the counter variable to 1
// counter-controlled iteration
while (gradeCounter <= 10) { // loop 10 times
cout << "Enter grade: "; // prompt for input
cin >> grade; // input next grade
total = total + grade; // add grade to total
gradeCounter = gradeCounter + 1; // increment counter by 1
}
}
while statement UML diagram
Determine
whether looping
should continue
Sentinel-controlled repetition
• Also known as indefinite repetition – the number of repetitions is not
known before the loop begins executing.
• Use a sentinel value (also called a signal/dummy/flag value) to
indicate “end of data entry”
• The sentinel value must be chosen so that it’s not confused with an
acceptable input value. For example, grades are normally
nonnegative integers, so –1 is an acceptable sentinel value.
#include <iostream>
using namespace std;
int main() {
int grade;
int total = 0; // initialize sum of grades entered by the user
int gradeCounter = 1; //initialize the counter variable to 1
int main()
{
// initializing variables in declarations
int passes = 0; // number of passes
int failures = 0; // number of failures
int studentCounter = 1; // student counter
int result; // one exam result (1 = pass, 2 = fail)