0% found this document useful (0 votes)
54 views17 pages

5.lec5 Cin If Else

The document discusses console input (cin) in C++ and how it can be used to take user input. It provides examples of using cin to read different data types from the user and store them in variables. The document also covers conditional execution using if/else statements, relational and logical operators, and nested if/else statements.

Uploaded by

wasifnaseer255
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views17 pages

5.lec5 Cin If Else

The document discusses console input (cin) in C++ and how it can be used to take user input. It provides examples of using cin to read different data types from the user and store them in variables. The document also covers conditional execution using if/else statements, relational and logical operators, and nested if/else statements.

Uploaded by

wasifnaseer255
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Console input (cin)

and
conditional execution

1
Console Input (cin)
8 interactive program: Reads input from the console.
– While the program runs, it asks the user to type input.
– The input typed by the user is stored in variables in the code.

– Can be tricky; users are unpredictable and misbehave.


– But interactive programs have more interesting behavior.

8 cin: can read input from many sources.


– Can also read from files etc.

2
cin
– Each method waits until the user presses Enter.
– The value typed by the user is returned.

– prompt: A message telling the user what input to type.

int age;
cout << "How old are you? "; // prompt
cin >> age;
cout << “You typed " << age;

3
cin example
void main()
{
cout<<"How old are you? "; age 29
cin >>age;
years 36
int years = 65 - age;
cout << years << " years until retirement!";

}
8 Console (user input underlined):
How old are you?
36 years until retirement!

29

4
cin example 2
8 The cin can read multiple values from one
line
void main()
{
int num1, num2
cout << "Please type two numbers: ";
cin >> num1 >> num2;
int product = num1 * num2;
cout << “The product is " << product;
}

8 Output (user input underlined):


Please type two numbers: 8 6
The product is 48
5
Quick check 1 - Input tokens
8 token: A unit of user input, as read by the cin.
– Tokens are separated by whitespace (spaces, tabs,
new lines).
– How many tokens appear on the following line of input?
23 John Smith 42.0 "Hello world" $2.50 " 19"

A. 2 B. 6 C. 7

D. 8 E. 9

6
input tokens
8 When a token is the wrong type, the
program crashes. (runtime error)
int age;
cout << “What is your age ? ";
cin >> age

Output:
What is your age? Timmy
Error

7
The if/else statement

reading:

8
The if statement
Executes a block of statements only if a test is true

if (test) {
statement;
...
statement;
}
8 Example:
double gpa;
cin >> gpa;
if (gpa >= 2.0){
Scout<<"Application accepted.";
}
9
The if/else statement
Executes one block if a test is true, another if false

if (test) {
statement(s);
} else {
statement(s);
}
8 Example:
double gpa;
if (gpa >= 2.0) {
cout<<"Welcome to Mars University!";
}
else {
cout<<"Application denied.";} 10
Relational expressions
8 if statements and for loops both use logical tests.

for (int i = 1; i <= 10; i++) { ...


if (i <= 10) { ...
– These are boolean expressions, seen in Ch. 5.

8 Tests use relational operators:


Operator Meaning Example Value
== equals 1 + 1 == true
2
!= does not equal 3.2 != true
2.5
< less than 10 < 5 false
> greater than 10 > 5 true
11
<= less than or equal to 126 <= false
Logical operators
8 Tests can be combined using logical
operators:
Operator Description Example Result
&& and (2 == 3) && (-1 < false
5)
|| or (2 == 3) || (-1 < true
5)

8 "Truth! tables"not
for each, used with logical
!(2 == 3) true

values p and q:
p !p
p q p && q p || q
true false
true true true true
fals true
true fals false true e
e
fals true false true 12
e
Nested if/else
Chooses between outcomes using many tests
if (test) {
statement(s);
} else if (test) {
statement(s);
} else {
statement(s);
}
8 Example:
if (x > 0) {
cout<<"Positive";
}
else if (x < 0) {
cout<<"Negative";
}
else {
cout<<"Zero";
} 13
Exercises
8 Write a method that prints out if it is good
weather to go for a bike ride. The weather is
good if the temperature is between 40
degrees and 100 degrees inclusive unless it
is raining, in which case the temperature
must be between 70 degrees and 110
degrees inclusive
8 Write a method that returns the largest of
three numbers using if statements
8 Write a method that determines if one day is
before another day (given month and day) 14
Exercise
8 Prompt the user to enter two people's heights in
inches.
– Each person should be classified as one of the following:
• short (under 5'3")
• medium (5'3" to 5'11")
• tall (6' or over)

– The program should end by printing which person is


taller.
Height in feet and inches: 5 7
You are medium.
Height in feet and inches: 6 1
You are tall.
Person #2 is taller than person #1. 15
Exercises
8 Write a method that simulates rolling 2 six
sided dice a given number of time and
returns the number of times a given value is
the sum of the two dice when rolled.
8 Write a method that determines if a number
is a perfect number. A perfect number equals
the sum of its integer divisors, excluding itself
6 = 1 + 2 + 3, perfect
8 > 1 + 2 + 4, deficient
12 < 1 + 2 + 3 + 4 + 6, excessive
16
Exercises
8 Write a method that determines if we have
time to go out for lunch. Inputs are distance
to restaurant, average walking speed, time
required to finish meal, time available,
expected cost of meal, and money available
8 times are expressed as whole number of
minutes
8 money is expressed as a double

17

You might also like