C++ Basics and Review - Primitive Types, Operators, and Comments

c basics mostly review n.w
1 / 9
Embed
Share

Explore C++ basics with a focus on primitive types, operators, and comments. Learn about integers, floating-point numbers, Boolean values, characters, and common operators. Understand the importance of including libraries, specifying variable types, and adding comments in your code.

  • C++
  • Programming Basics
  • Operators
  • Primitive Types
  • Comments

Uploaded on | 0 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. C++ BASICS (MOSTLY REVIEW)

  2. C++: #include <iostream> #include <iostream> #include < #include <stdlib.h //for this program, but for most //for this program, but for most //programs you will need the //programs you will need the //standard library so it s good to get //standard library so it s good to get //into the habit of including it. //into the habit of including it. using namespace std; using namespace std; C: #include < #include <stdio.h int main() { int main() { stdio.h> > stdlib.h> //technically you don t need this library > //technically you don t need this library // // printf printf() displays the string inside quotation () displays the string inside quotation printf printf("Hello, World!"); ("Hello, World!"); return 0; return 0; } } int main() { int main() { } } cout cout <<"Hello world!" << <<"Hello world!" << endl return (0); return (0); endl; ;

  3. C++ Primitive Types: C++ Primitive Types: Numbers: Numbers: Integers: Integers: int (see note, below) int (see note, below) Floating point numbers: Floating point numbers: float, double float, double C Primitive Types: C Primitive Types: Numbers: Numbers: Integers: Integers: Short, int, long Short, int, long Floating point numbers: Floating point numbers: float, double float, double Boolean: Boolean: Bool Bool Characters: Characters: Char Char Boolean: Boolean: Bool Bool Characters: Characters: char char C++: you specify that the int is either short or long. Short =16 bits, default int = 32 bits, long int = 64 bits. You can make chars and ints signed (the default) or unsigned. Unsigned: you get an extra bit (and thus larger numbers are possible) but you can t have a negative number.

  4. OPERATORS: Same in C and C++: postfix postfix x++,x x++,x-- -- Multiplication: Multiplication: * * Division Division / / Modulus: Modulus: % % Addition: Addition: + + Subtraction Subtraction - - Relational: Relational: >,<,>=,<= >,<,>=,<= Equality: Equality: ==,!= ==,!= Logical and: Logical and: && && Logical Or: Logical Or: || || Assignments: Assignments: =,+=, =,+=,- -=,*=,/=, =,*=,/=,

  5. Comments: Same in C and C++ /* comments gohere (for block comments) block comments are opened with the slash-star, and then closed with star-slash */ Or //comments go here (for in-line comments, for these //everything following the double slash until the end // of the line is commented out. No need to close.

  6. Branching (if conditions): Same in C and C++ C++: int char grade; char grade; C: int char grade; char grade; int testscore testscore = 76; = 76; int testscore testscore = 76; = 76; if ( grade = 'A'; grade = 'A'; } else if ( } else if (testscore grade = 'B'; grade = 'B'; } else if ( } else if (testscore grade = 'C'; grade = 'C'; } else if ( } else if (testscore grade = 'D'; grade = 'D'; } else { } else { grade = 'F'; grade = 'F'; } } cout cout << "Grade = " << grade << << "Grade = " << grade << endl if (testscore testscore >= 90) { >= 90) { if ( grade = 'A'; grade = 'A'; } else if ( } else if (testscore grade = 'B'; grade = 'B'; } else if ( } else if (testscore grade = 'C'; grade = 'C'; } else if ( } else if (testscore grade = 'D'; grade = 'D'; } else { } else { grade = 'F'; grade = 'F'; } } printf printf("Grade = %c ("Grade = %c\ \n", grade); if (testscore testscore >= 90) { >= 90) { testscore >= 80) { >= 80) { testscore >= 80) { >= 80) { testscore >= 70) { >= 70) { testscore >= 70) { >= 70) { testscore >= 60) { >= 60) { testscore >= 60) { >= 60) { endl; ; n", grade);

  7. Loops: Same in C and C++ For Loop: For Loop: int x=0; int x=0; for(int for(int i i=1; x += x += i i; ; } } x /=10; x /=10; //NOTE: you can skip any of the 3 conditions if //NOTE: you can skip any of the 3 conditions if defined elsewhere defined elsewhere WhileLoop WhileLoop: : int count = 1; int count = 1; int tot = 0; int tot = 0; while (count < 11) { while (count < 11) { tot += count; tot += count; count++; count++; } } =1; i i<11; <11; i i++) { ++) {

  8. Array Basics*: Same in C and C++ int c[5]; int c[5]; c[0]=100; c[0]=100; c[1]=200; c[1]=200; c[2]=300; c[2]=300; c[3]=400; c[3]=400; c[4]=500; c[4]=500; Multidimensional: Multidimensional: int int marr marr[10][20]; [10][20]; you can do: you can do: int marr2[4][3] = { int marr2[4][3] = { {6,3,2}, {8,1,3}, {3,5,1}, {7,8,9} }; {6,3,2}, {8,1,3}, {3,5,1}, {7,8,9} }; OR OR int a[5] = {3,2,4,1,7}; int a[5] = {3,2,4,1,7}; *Note: We will be talking about dynamic arrays more later!

  9. C++: THE BASICS COVERED!

Related


More Related Content