Control
Control
if ( payCode == 4 )
cout << "You get a bonus!" << endl;
if…else Double-Selection
Statement
• if
– Performs action if condition true
• if…else
– Performs one action if condition is true, a different action if it is
false
• Pseudocode
– If student’s grade is greater than or equal to 60
print “Passed”
Else
print “Failed”
• C++ code
– if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";
if…else Double-Selection
Statement (Cont.)
• Ternary conditional operator (?:)
– Three arguments (condition, value if true,
value if false)
• Code could be written:
cout <<( grade >= 60 ? “Passed” : “Failed” );
• else problem
– Example
• if ( x > 5 )
if ( y > 5 )
cout << "x and y are > 5";
else
cout << "x is <= 5";
– Compiler interprets as
• if ( x > 5 )
if ( y > 5 )
cout << "x and y are > 5";
else
cout << "x is <= 5";
if…else statements (Cont.)
• Compound statement
– Also called a block
• Set of statements within a pair of braces
• Used to include multiple statements in an if body
– Example
• if ( studentGrade >= 60 )
cout << "Passed.\n";
else
{
cout << "Failed.\n";
cout << "You must take this course again.\n";
}
– Without braces,
cout << "You must take this course again.\n";
always executes
if Statement
Example:
if (expression) if (u > v)
{ {
statements; a = 1;
if (expression) b = 2;
{ if ( u > z)
statements; {
} x =11;
} y = 12;
}
}
while Repetition Statement
• Repetition statement
– Action repeated while some condition remains true
– Pseudocode
• While there are more items on my shopping list
Purchase next item and cross it off my list
– while loop repeats until condition becomes false
– Example
• int product = 3;
Not Valid: X
• for (j = 0, j < n, j = j + 3) // semicolons needed
case valuen:
statementn;
break;
default:
statement;
}
The switch Statement
switch (grade)
{
case ‘A’:
cout << “Grade is between 90 & 100”;
break;
case ‘B’:
cout << “Grade is between 80 & 89”;
break;
case ‘C’:
cout << “Grade is between 70 & 79”;
break;
case ‘D’:
cout << “Grade is between 60 & 69”;
break;
case ‘E’:
cout << “Grade is between 0 & 59”;
break;
default:
cout << “You entered an invalid grade.”;
}
The switch Statement
**** Menu * * * *
1. Nablus
2. Rammallah
3. Tolkarm
4. Jenien
Choose either 1, 2, 3 or 4:
The switch Statement
switch (choice) switch (choice)
{ {
case 1: case 1:
cout << “Nablus”; cout << “Nablus”;
case 2: break;
cout << “Rammallah”; case 2:
case 3: cout <“Rammallah”;
cout << “Tolkarm”; break;
case 4: case 3:
cout << “Jenien”; cout << “Tolkarm”;
default: break;
cout<<“ invalid choice”; case 4:
} cout << “Jenien”;
break;
default:
cout<<“ invalid choice”;
}
The switch Statement
#include<iostream>
Void main ( )
{
int value
cout << “Enter 1- Palestine 2- Egypt 3- USA”;
cin >> value;
switch (value)
{
case 1: cout << “No of population is 5 million”; break;
case 2: cout << “No. of population is 70 million”; break;
case 3: cout << “No. of population is 180 million”; break;
default: cout<<“invalid choice”;
}
}
switch Multiple-Selection
Statement
• switch statement
– Used for multiple selections
void main ( )
{
int k;
for ( k= -5; k < 25; k= k+5)
{
cout << k;
cout << “ Good Morning” << endl;
}
}
void main ( )
{
int k;
for ( k= -5; k < 25; k= k+5)
{
cout << k;
break;
cout << “ Good Morning” << endl;
}
}
Output = -5
continue
Void main ( )
{
int k;
for ( k= -5; k < 25; k= k+5)
{
cout << k;
conitnue;
cout << “ Good Morning” << endl;
}
}
Output = - 5
0
5
10
15
20
Examples
int j =50;
while (j < 80)
{
j += 10;
if (j == 70)
break;
cout << “j = “ << j<< ‘\n’;
}
cout << “We are out of the loop.\n”;
Output
j = 60
We are out of the loop.
Example
do
{
cout << “Enter your age: “;
cin >> age;
if (age <=0)
cout << “Invalid age.\n”;
else
cout << "DO SOMETHING\n";
} while (age <=0);
Example
do {
x = x + 5;
y = x * 25;
cout << y << endl;
if ( x == 100) done = true;
} while (!done);
Tugas # 1
• Buat sebuah program yang dapat
menghitung nilai dari ex dengan
menggunakan formula: