Week 4
Week 4
Jeanine Ingber
Relational Operators
== equality
!= non equality
< less than
> greater than
<= less than equal to
>= greater than equal to
Jeanine Ingber
Logical Operators
! not
&& and
|| or
Jeanine Ingber
Boolean Expressions
X>0
y==0
x>0 && y==0
Jeanine Ingber
Control Structures
Selection
if
if else
switch
repetition
while
do while
for
Jeanine Ingber
If statement
if(Boolean expression)
statement; //single statement
if(Boolean expression)
{ //more than one statement
statement1;
.
statement n;
}
Jeanine Ingber
If statement - examples
if (x>0)
cout << sqrt(x);
if(x>0)
{
x=sqrt(x);
cout << x;
{
Jeanine Ingber
if - else statement
if(Boolean expression)
statement;
else
statement;
if(Boolean expression)
{
statement block
}
else
{
statement block
}
Jeanine Ingber
if-else examples
1) if(x>=0)
cout << sqrt(x);
else
cout << “x is negative”;
2) if(grade >=90)
cout << “grade is A”;
else if(grade >= 80)
cout << “grade is B”;
else if(grade >= 70)
cout << “grade is C”;
else
cout << “grade is F”;
Jeanine Ingber
Self - tests
Jeanine Ingber
Switch Statement
•Multiway selection statement
•example
switch(expression)
{
case constant:
statement(s);
break;
case constant:
statement(s);
break;
default: //optional
statement(s);
}
Jeanine Ingber
Switch Statement
Jeanine Ingber
Self-test
Jeanine Ingber
repetition
while statement
do while statement
for statement
Jeanine Ingber
while statement
while(expression)
statement;
while(expression)
{ //braces needed for multiple statements
statement;
statement;
.
}
Statements are repeated while expression is true. If expression is false
at the first test, statement(s) are not executed
Jeanine Ingber
example
#include <iostream>
using namespace std;
int main()
{
int count=0, base=3;
while(base > 0 && count < 5)
{
count++;
base--;
cout << base << endl;
}
cout << “count is “ << count;
return 0;
}//end main
output?
Jeanine Ingber
Self-test while loops
Jeanine Ingber
do while
do
statement;
while(expression);
do
{
statement1;
statement2;
.
} while(expression);
note - the expression is tested after the statement(s)
are executed, so statements are executed at least once.
Jeanine Ingber
do while example
n=5;
fact =1;
do
{
fact = fact*n;
n--;
} while(n>1);
cout << fact;
output————?
Jeanine Ingber
for statement
for(initialization; test; increment/decrement)
statement;
Jeanine Ingber
for statement
initalize
test
increment
decrement
true
statement(s)
statement(s)
Jeanine Ingber
for statement - example
output—————?
Jeanine Ingber
for statement - example
//n factorial again
int fact =1;
for(int n=5;n>1;n- -)
fact = fact * n;
cout << fact << n;
output—————?
Jeanine Ingber
nesting control stuctures
break;
terminates loop
execution continues with the first statement
following the loop
cin >> x;
for(int I=0; I<100; I++)
{
x- -;
if(x<0) break;
}
cout << x << “ “ << I;
Jeanine Ingber
continue statement
continue;
forces next iteration of the loop, skipping any
remaining statements in the loop
for(int I=1; I<=100; I++)
{
if(I%2) continue;
cout << I;
}
output?
Jeanine Ingber