C++ Programming Essentials: Variables, Data Types, and Output

undefined
 
Variables, Assignment, and
Data Types
 
C++
 
 
cout << “
Whatever you are, be a good one.
” << endl;
 
1.
cout is called a “stream”
2.
It’s in something called “std”, (using namespace std)
3.
It represents the console/screen data stream
4.
The << are things that we’re pushing into the stream
 
Printing strings in C++ (review)
 
 
CLASS CountDown
BEGIN
  METHOD Main()
  BEGIN
    s1 = "Three... "
    s2 = "Two... "
    s3 = "One... "
    s4 = "Zero... "
    PRINT(s1 + s2 + s3 + s4 + "Liftoff!")
    PRINTLINE()
 
    PRINT("Houston, we have a problem.")
  END Main
END CountDown
 
Output:
 
Three... Two... One... Zero... Liftoff!
Houston, we have a problem.
 
Pseudocode
 
 
#include
 
<
iostream
>
 
#include
 
<
string
>
 
using
 
namespace
 std;
 
int
 main() {
 
   string s1 = 
"Three..."
;
 
   string s2 = 
"Two..."
;
 
   string s3 = 
"One..."
;
 
   string s4 = 
"Zero..."
;
 
   cout << s1 << s2 << s3 << s4 << 
" Liftoff!"
 << endl;
 
   cout << 
"Houston, we have a problem."
 << endl;
 
}
 
Output:
 
Three... Two... One... Zero... Liftoff!
Houston, we have a problem.
 
In C++
 
 
Printing and 
escape sequence
 prints a special
character in an output string.
 
Common escape sequences:
\b
 
backspace. (e.g “B\bsecz\bret” prints what?)
\t
 
tab
\n
 
newline
\r
 
carriage return
\”
 
double quote
\’
 
single quote
\\
 
backslash
\a  
 
alert (which only sometimes works 
 try it!)
 
Escape Sequences
 
 
#include
 
<
iostream
>
 
using
 
namespace
 std;
int
 main() {
 
  cout << 
"Roses are red,\n\tViolets are blue,\n"
 
  << 
"Sugar is sweet,\n\tBut I have \"commitment issues\",\n\t"
 
  << 
"So I'd rather just be friends\n\tAt this point in our "
 
  << 
"relationship."
 << endl;
 
}
 
 
Output:
 
Roses are red,
   Violets are blue,
Sugar is sweet,
   But I have "commitment issues",
   So I'd rather just be friends
   At this point in our relationship.
 
In C++
 
// Prints the number of keys on a piano.
CLASS PianoKeys
BEGIN
  METHOD Main()
  BEGIN
   keys = 88
    PRINT("A piano has " + keys + " keys.")
 END Main
END PianoKeys
 
Output:
 A piano has 88 keys.
 
 Pseudocode
 
 
#include
 
<
iostream
>
 
using
 
namespace
 std;
int
 main() {
 
  int
 keys = 
88
;
 
  cout << 
"A piano has "
 << keys << 
" keys."
 << endl;
 
}
 
Output:
 A piano has 88 keys.
 
In C++
 
// Print the number of sides of several geometric shapes.
CLASS Geometry
BEGIN
  METHOD Main()
  BEGIN
 
    sides = 7
    PRINT("A heptagon has " + sides + " sides.")
    sides = 10
     PRINT("A decagon has " + sides + " sides.")
     sides =  12
     PRINT("A dodecagon has " + sides + " sides.")
  END Main
END Geometry
 
Output:
A heptagon has 7 sides.
A decagon has 10 sides.
A dodecagon has 
12 
sides.
 
  
Pseudocode
 
// Print the number of sides of several geometric shapes.
 
#include
 
<
iostream
>
 
using
 
namespace
 std;
int
 main() {
 
  int
 sides = 
7
; 
// declare and initialize
 
  cout << 
"A heptagon has "
 << sides << 
" sides."
 << endl;
 
  sides = 
10
; 
// assignment statement
 
  cout << 
"A decagon has "
 << sides << 
" sides."
 << endl;
 
  sides = 
12
; 
// assignment statement
 
  cout << 
"A dodecagon has "
 << sides << 
" sides."
 << endl;
}
 
In C++
 
Constants
 
 
Use all CAPITAL LETTERS for constants and separate words with
an underscore:
 
 
  
Pseudocode: CREATE TAX_RATE = .05;
    
 
C++:  
const double
 TAX_RATE = .05;
  
C++: 
#define
 TAX_RATE .05
 
 
 
Declare constants at the top of the program
 
 
 
E
x
a
m
p
l
e
 
i
n
 
C
+
+
 
#include
 
<
iostream
>
#include
 
<
string
>
using
 
namespace
 std;
 
int
 main() {
  string myStr = 
"Whats up, Doc?"
;
  cout << myStr.length() << endl;         
// 14
  
// Note: if strings are equal, this would return 0
  cout << myStr.compare(
"Hello"
) << endl; 
// 1
  cout << myStr.find(
's'
) << endl;        
// 4
  cout << myStr.substr(
2
, 
7
) << endl;     
// ats up,
 
}
Slide Note
Embed
Share

Understanding variables, data types, and printing output in C++ is crucial for beginners. This tutorial covers essential concepts such as assigning values, handling strings, using escape sequences, and practical examples like counting down and displaying text creatively. Dive into the world of C++ with this informative guide!

  • C++
  • Programming
  • Variables
  • Data Types
  • Output
  • Basics

Uploaded on Sep 28, 2024 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Variables, Assignment, and Data Types C++

  2. Printing strings in C++ (review) cout << Whatever you are, be a good one. << endl; 1. cout is called a stream 2. It s in something called std , (using namespace std) 3. It represents the console/screen data stream 4. The << are things that we re pushing into the stream

  3. Pseudocode CLASS CountDown BEGIN METHOD Main() BEGIN s1 = "Three... " s2 = "Two... " s3 = "One... " s4 = "Zero... " PRINT(s1 + s2 + s3 + s4 + "Liftoff!") PRINTLINE() PRINT("Houston, we have a problem.") END Main END CountDown Output: Three... Two... One... Zero... Liftoff! Houston, we have a problem. Ps

  4. In C++ #include <iostream> #include <string> using namespace std; int main() { string s1 = "Three..."; string s2 = "Two..."; string s3 = "One..."; string s4 = "Zero..."; cout << s1 << s2 << s3 << s4 << " Liftoff!" << endl; cout << "Houston, we have a problem." << endl; } Output: Three... Two... One... Zero... Liftoff! Houston, we have a problem.

  5. Escape Sequences Printing and escape sequence prints a special character in an output string. Common escape sequences: \b backspace. (e.g B\bsecz\bret prints what?) \t tab \n newline \r carriage return \ double quote \ single quote \\ backslash \a alert (which only sometimes works try it!)

  6. In C++ #include <iostream> using namespace std; int main() { cout << "Roses are red,\n\tViolets are blue,\n" << "Sugar is sweet,\n\tBut I have \"commitment issues\",\n\t" << "So I'd rather just be friends\n\tAt this point in our " << "relationship." << endl; } Output: Roses are red, Violets are blue, Sugar is sweet, But I have "commitment issues", So I'd rather just be friends At this point in our relationship.

  7. Pseudocode // Prints the number of keys on a piano. CLASS PianoKeys BEGIN METHOD Main() BEGIN keys = 88 PRINT("A piano has " + keys + " keys.") END Main END PianoKeys Ps Output: A piano has 88 keys.

  8. In C++ #include <iostream> using namespace std; int main() { int keys = 88; cout << "A piano has " << keys << " keys." << endl; } Output: A piano has 88 keys.

  9. Pseudocode // Print the number of sides of several geometric shapes. CLASS Geometry BEGIN METHOD Main() BEGIN sides = 7 PRINT("A heptagon has " + sides + " sides.") sides = 10 PRINT("A decagon has " + sides + " sides.") sides = 12 PRINT("A dodecagon has " + sides + " sides.") END Main END Geometry Output: A heptagon has 7 sides. A decagon has 10 sides. A dodecagon has 12 sides. Ps

  10. In C++ // Print the number of sides of several geometric shapes. #include <iostream> using namespace std; int main() { int sides = 7; // declare and initialize cout << "A heptagon has " << sides << " sides." << endl; sides = 10; // assignment statement cout << "A decagon has " << sides << " sides." << endl; sides = 12; // assignment statement cout << "A dodecagon has " << sides << " sides." << endl; }

  11. Constants Use all CAPITAL LETTERS for constants and separate words with an underscore: Pseudocode: CREATE TAX_RATE = .05; C++: const double TAX_RATE = .05; C++: #define TAX_RATE .05 Declare constants at the top of the program

  12. Example in C++ Example in C++ #include <iostream> #include <string> using namespace std; int main() { string myStr = "Whats up, Doc?"; cout << myStr.length() << endl; // 14 // Note: if strings are equal, this would return 0 cout << myStr.compare("Hello") << endl; // 1 cout << myStr.find('s') << endl; // 4 cout << myStr.substr(2, 7) << endl; // ats up, }

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#