0% found this document useful (0 votes)
39 views28 pages

Chapter 2.2

The document discusses different C++ data types like integers, floating point numbers, characters, strings, and Booleans that can be used to store data in variables. It also covers variable declaration and initialization, built-in arithmetic, comparison, and logical operators, control flow structures like if/else and switch statements, and taking user input. The key data types, operators, and control structures for building basic C++ programs are presented.

Uploaded by

Ken Mutai
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)
39 views28 pages

Chapter 2.2

The document discusses different C++ data types like integers, floating point numbers, characters, strings, and Booleans that can be used to store data in variables. It also covers variable declaration and initialization, built-in arithmetic, comparison, and logical operators, control flow structures like if/else and switch statements, and taking user input. The key data types, operators, and control structures for building basic C++ programs are presented.

Uploaded by

Ken Mutai
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/ 28

Chapter 2:2

First Program
 C++ variables
(Containers for storing data)

 int - stores integers (whole numbers), without decimals, such as 123 or -123
 double - stores floating point numbers, with decimals, such as 19.99 or -19.99
 char - stores single characters, such as 'a' or 'B'. Char values are surrounded by
single quotes
 string - stores text, such as "Hello World". String values are surrounded by
double quotes
 bool - stores values with two states: true or false
Variable declaration

 Create a variable called myNum of type int and assign it the value 15

int myNum = 15;
cout << myNum;
int myNum;
myNum = 15;
cout << myNum;
Variables

 Note that if you assign a new value to an existing variable, it will overwrite
the previous value:
 int myNum = 15; // Now myNum is 15
 myNum = 10; // Now myNum is 10
 cout << myNum;
Displaying variables

 int myAge = 35;


 cout << "I am " << myAge << " years old.";
Adding variables

int x = 5;
int y = 6;
int sum = x + y;
cout << sum;
Multiple variables

 Use comma to separate List of same variables.


 int x = 5, y = 6, z = 50;
cout << x + y + z;
You can also assign the same value to multiple variables in one line:
 int x, y, z;
x = y = z = 50;
cout << x + y + z;
Constants

Is declared using keyword “const”


 #include <iostream>
 using namespace std;

 int main() {
 const int minutesPerHour = 60;
 const float PI = 3.14;
 cout << minutesPerHour << "\n";
 cout << PI;
 return 0;
}
User input
cin is a predefined variable that reads data from the keyboard with the extraction operator (>>)

#include <iostream>
using namespace std;

int main() {
int x;
cout << "Type a number: "; // Type a number and press
enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x;
return 0;
}
Creating a Simple Calculator
C++ Data Types
Scientific Numbers
A floating point number can also be a scientific number with an "e" to indicate
the power of 10:
Boolean Types
String Types

 The string type is used to store a sequence of characters (text).


 String values must be surrounded by double quotes:
 string greeting = "Hello";
cout << greeting
Arithmetic operators
Arithmetic operator example
Comparison Operators

 The return value of a comparison is either 1 or 0, which means true (1) or


false (0)
Comparison Example
Strings
 To use strings, you must include an additional header file in the source code,
the <string> library:
Conditions

 Conditions are expressions that evaluate to a boolean value.


If condition

 Use if to specify a block of code to be executed, if a specified condition is true


 #include <iostream>
 using namespace std;

 int main() {
 if (20 > 18) {
 cout << "20 is greater than 18";
 }
 return 0;
 }
If
#include <iostream>
using namespace std;

int main() {
int x = 20;
int y = 18;
if (x > y) {
cout << "x is greater than y";
}
return 0;
}
If …..else

 Use else to specify a block of code to be executed, if the same condition is false


 #include <iostream>
 using namespace std;

 int main() {
 int time = 20;
 if (time < 18) {
 cout << "Good day.";
 } else {
 cout << "Good evening.";
 }
 return 0;
 }
If …..else

 #include <iostream>
 using namespace std;

 int main() {
 int time = 20;
 if (time < 18) {
 cout << "Good day.";
 } else {
 cout << "Good evening.";
 }
 return 0;
 }
Else if

 Use the else if statement to specify a new condition if the first condition is false.
 #include <iostream>
 using namespace std;

 int main() {
 int time = 22;
 if (time < 10) {
 cout << "Good morning.";
 } else if (time < 20) {
 cout << "Good day.";
 } else {
 cout << "Good evening.";
 }
 return 0;
 }
Switch statements
 Use the switch statement to select one of many code blocks to be executed.
 The switch expression is evaluated once
 The value of the expression is compared with the values of each case
 If there is a match, the associated block of code is executed
#include <iostream> break;
using namespace std; case 'D' :
cout << "You passed";
int main () { break;
// local variable declaration: case 'F' :
char grade = 'D'; cout << "Better try again";
break;
switch(grade) { default :
case 'A' : cout << "Invalid grade";
cout << "Excellent!"; }
break; cout << "Your grade is " << grade;
case 'B' :
case 'C' : return 0;
cout << "Well done"; }
Break statement

 The break in C++ is a loop control statement that is used to terminate the
loop

You might also like