Professional Documents
Culture Documents
th
Review Challenge
This program computes the average of 2 numbers and
prints the result. Find the syntax & semantic errors.
#include <iostream>
int main(void)
{
int sum, a,
b;
std::cout << Enter 2 numbers:\n;
std ::
cin >> a;
sum = sum + a;
std::c
in >> b
sum = sum + b;
sum = sum / 2;
std::cout >> Average: + sum;
;
)
Review Challenge
Given a number, n, in seconds, print out the number of
hours, minutes and seconds that n represents.
#include <iostream>
int main(void)
{
int n;
Hours: 2
Minutes: 1
Seconds: 5
;
;
;
char variables
int main(void)
{
char small_num;
small_num = -125;
// this is OK!
char variables
#include <iostream>
int main(void)
{
char grade;
cout << Enter desired grade: ;
cin >> grade;
cout << You dont deserve a <<
grade;
}
grade
P
B
a = b = c = 5;
int main(void)
{
int a, b, c;
c = 5;
b = c;
a = b;
Const Variables
Whats wrong with this program?
#include <iostream>
int main(void)
{
double rad;
std::cout << Enter the radius of your zit: ;
std::cin >> rad;
std::cout << Zit circumference: << 2 * rad * 3.141;
std::cout << Zit area: << rad*rad * 3.141;
}
Hint:
1.What do we have to do to our program
to get increased precision (3.141 ->3.14159)?
Const Variables
Use const variables when you have a value that is
fixed (like , the speed of light, etc.), and is
used multiple times in your program.
You can define your
constant variable at the
int main(void)
top of your program once
{
3.141592653; and then use it over and
const double PI = 3.14;
over.
double rad;
#include <iostream>
program.
Const Variables
Once a variable is defined as const, its value cant be changed.
Also, you must set the value of a const variable when it is defined.
#include <iostream>
int main(void)
{
const int numHumanLegs = 2;
numHumanLegs = 10;
// syntax error!
//
// OK!
syntax error!
a -88
45
1
5
15
11
int main(void)
{
int a;
After:
int main(void)
{
int a;
a = 5; a = 5 + 10;
a = a + 10;
a = a * 3; a = 15 * 3;
a = a / 4; a = 45 / 4;
a = a % 2; a = 11 % 2;
std::cout << a;
}
a = 5;
a += 10;
a *= 3;
a /= 4;
a %= 2;
std::cout << a;
}
int main(void)
{
int a = 5;
a--; // OR --a;
int b = 6;
b++; // OR ++b;
int b = 6;
b = b + 1;
}
++ and -- Specifics
You may only use the ++ and -- operators with
variables. Not with expressions or numbers.
int main(void)
{
int a = 5;
++a;
++(a+5);
// SYNTAX ERROR!
7--;
// SYNTAX ERROR!
The if Statement
The if statement is used to make decisions in your
C++ programs.
// Lets learn the IF statement
#include <iostream>
passcode
91
int main(void)
{
int passcode;
if (passcode != 9939)
std::cout << "\aInvalid password.\n";
}
Invalid password.
If this is true
if ( expression )
do this statement;
> bbb )
aaa>=
!=
==
ais
greater
than
b;
a
has
has
greater
athe
different
same
thanvalue
or
value
equal
asthan
b;
to b;
b;
cout << a
if (
The if Statement
Lets learn how to use more advanced forms of the
if statement
Form #1
if ( expression )
do this statement;
else
do this statement;
If the expression is
true, e.g. age < 34, then
C++ runs the first
statement, else C++ runs
the second statement.
#include <iostream>
using namespace std;
int main(void)
{
int age;
cout << "Enter age: ";
cin >> age;
if (age < 34)
cout << Just a kid";
else
cout << Old f@rt!;
}
The if Statement
Lets learn how to use more advanced forms of the
if statement
Form #2
if ( expression )
do this statement;
else if ( expression2 )
do this statement;
else if ( expression3 )
do this
this statement;
statement;
do
else if ( expression4 )
do this statement;
#include <iostream>
using namespace std;
int main(void)
{
int price;
cout << "Enter the price: ";
cin >> price;
if (price > 100)
cout << Expensive";
else if (price < 20)
cout << Cheap;
else
cout << Just right.;
}
The if Statement
As soon as a condition is found to be true in a set of if, else-if
statements, none of the else clauses are evaluated.
#include <iostream>
int main(void)
{
int score = 80;
80 >= 90???
score
80
The if Statement
Sometimes, you might want to run more than one
statement if your expression is true
if ( expression )
{ do this statement;
else
do this statement
statement;too;
}
{ do this statement;
#include <iostream>
using namespace std;
int main(void)
{
int hairs;
cout << "Enter hairs: ";
cin >> hairs;
also do this;
and this too;
The if Statement
You can also define new variables
within any block in your program.
if ( expression )
{
do this
int
foo,statement;
bar;
float
bletch
= 16; too;
do this
statement
When
a block
exits, all
}
int main(void)
{
int hairs;
hairs
177
125
-18
of its variables
disappear!
real_hairs
250/2
real_hairs = hairs / 2;
cout << Liar, you have <<
real_hairs;
}
cout << Have a nice day\n;
}
iq;
31 > 100???
iq -47
31
intWhats
main(void)
wrong with it?
{
int
iq;
int main()
{
int num_beers;
std::cin >> num_beers;
if (num_beers > 3)
if (num_beers < 15)
{
std::cout <<
Drunk but alive\n;
if (num_beers
< 15)
std::cout << Drunk but alive\n;
else
std::cout <<
<< Barely
Barely buzzed.\n;
buzzed.\n;
}
std::cout
} else
std::cout << Barely buzzed.\n;
}
The if Statement
Sometimes you will see C++ programmers place an arithmetic
expression in between the parentheses of an if statement.
int main()
a 5
{
b
4
int a = 5, b = 4;
(5-4)*3
3
if ((a b)*3)
std::cout<< Will it print this\n;
else
std::cout << or this?\n;
}
If the expressions value is not equal to zero, then the
program runs the next statement as if the expression were
true.
The if Statement
Sometimes you will see C++ programmers place an arithmetic
expression in between the parentheses of an if statement.
int main()
a 5
{
b
5
int a = 5, b = 5;
(5-5)*3
0
if ((a b)*3)
std::cout<< Will it print this\n;
else
std::cout << or this?\n;
}
If the expressions value is equal to zero, then the program
runs the else statement as if the expression were false.
#include <iostream>
using namespace std;
int main(void)
{
int
iq 981
50
179
iq;
Your IQ: 50
An Interesting Idea
Question: Why would I use the following syntax in my
program?
int main(void)
{
int score;
Instead of
int main(void)
{
int score;
An Interesting Idea
Answer: To prevent bugs like the following:
int main(void)
{
int score;
An Interesting Idea
Lets see how it works!
int main(void)
{
int score;
int main(void)
{
int score;
Syntax error!
C++ Strings
So far, weve learned how to define integer and floatingpoint variables.
C++ also lets us define string variables.
Instead of holding numbers like int or double, string
variables holds a string of characters.
C++ Strings
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string myname;
myname = Carey;
cout << Hello, << myname;
}
myname Carey
Hello, Carey
C++ Strings
#include <iostream>
#include <string>
myname
Alan
C++ Strings
You can use the
comparison operators
on strings as well.
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string myname;
cout << Name? ;
cin >> myname;
if (myname == Carey)
cout << You stud!\n;
else
cout << You slacker!\n;
}
myname
CAREY
Name? CAREY
You slacker!
C++ Strings
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string password;
cout << Password? ;
cin >> password;
if (password < UCLA)
cout << Too low;
else if (password > UCLA)
cout << Too high!;
else
cout << Welcome,
master.\n;
Go + Bruins
msg1 = msg1 + Bruins;
cout << msg1 << \n;
msg1 += !!!;
! ! !
cout << msg1 << \n;
}
Go
Go Bruins
Go Bruins!!!
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string pass;
cout << Enter password: ;
getline(cin, pass);
if (pass == UCLA rules)
cout << Welcome Carey;
else
cout << Youre not Carey;
}
pass
UCLA rules
UCLA rules
Welcome Carey
getline(cin, model);
getline(cin, color);
...
}
Any time you use cin before using getline, you must put
a special command between the two or it wont work!