Professional Documents
Culture Documents
Lecture 7
if ( payCode = 4 )
cout << "You get a bonus!" << endl;
– paycode is set to 4 (no matter what it was before)
– Condition is true (since 4 is non-zero)
• Bonus given in every case
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
– Compiler associates else with the immediately preceding ()ي;;سبق
if
– 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
Example 2: j = 1;
sum = 0;
for ( ; j <= 3; ){
sum = sum + j;}
Output : infinite loop
Example 3: j = 1;
sum = 0;
for ( ; ; )
{ sum = sum + j; j++;
cout << "\n" << sum;
}
Output : infinite loop
for Repetition Statement (Cont.)
• General form of the for statement
– for ( initialization; loopContinuationCondition; increment )
statement;
• Can usually be rewritten as:
– initialization;
while ( loopContinuationCondition )
{
statement;
increment;
}
• If the control variable is declared in the initialization expression
– It will be unknown outside the for statement
for Repetition Statement (Cont.)
• for statement examples
– Vary control variable from 1 to 100 in increments of 1
• for ( int i = 1; i <= 100; i++ )
– Vary control variable from 100 to 1 in increments of -1
• for ( int i = 100; i >= 1; i-- )
– Vary control variable from 7 to 77 in steps of 7
• for ( int i = 7; i <= 77; i += 7 )
– Vary control variable from 20 to 2 in steps of -2
• for ( int i = 20; i >= 2; i -= 2 )
– Vary control variable over the sequence: 2, 5, 8, 11, 14, 17, 20
• for ( int i = 2; i <= 20; i += 3 )
– Vary control variable over the sequence: 99, 88, 77, 66, 55, 44,
33, 22, 11, 0
• for ( int i = 99; i >= 0; i -= 11 )
for Repetition Statement (Cont.)
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”; break;
case 2: case 2:
cout <“Rammallah”;
cout << “Rammallah”; break;
case 3: case 3:
cout << “Tolkarm”; cout << “Tolkarm”;
case 4: break;
case 4:
cout << “Jenien”;
cout << “Jenien”;
default: break;
cout<<“ invalid choice”; 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);
Hw # 3
• Write a program that computes
the value of ex by using the formula:?