#include <iostream>
#using namespace std;
int main(){
cout << "Hello world" << endl;
return 0;
}
#include <stdio.h>
int main(){
printf("Hello world\\n");
return 0;
}
Input in C++
#include <iostream>
using namespace std;
int main(){
int value;
cout << "Enter an integer" << endl;
cin >> value:
cout << "the integer is: " << value << end;
return 0;
}
#include <stdio.h>
int main(){
int value;
printf("Enter an integer\\n");
scanf("%d", &value);
printf("The integer is: %d\\n", value);
return 0;
}
Data types in C++
- Integers: can be
short(16 bit), int (32 bit), or long (64 bit)
- Real numbers: can be
float (7 digits), double (15 digits), or long double (19 digits)
- Characters:
char is 1-byte (8 bits)
- Logic:
bool (1 bit)
- Arrays:
int array[5] = {1, 3, 5, 7, 9};
Strings
- In C, strings were null-terminated character arrays
- In C++, we have a string class
- Creating a string:
string courseTitle = “Programming Fundamentals”;
- We can also use some parameters
+ → concatenate strings
== → equal to (used in if/else statements)
!= → not equal to
#include <iostream>
#strings <string>
using namespace std;
int main(){
string CourseDept, CourseNum, CourseCode;
cout << "Enter course department and code:" << endl;
cin >> CourseDept >> CourseName;
CourseCode = CourseDept + CourseNum;
if (CourseCode == "ECE244"){
cout << "That's us!" << endl;
}
}
Expressions and Statements
| Arithmetic |
x + y, x >= y |
| Logical |
`A |
| Control Statements |
- if and if/else conditions |
- while and do/while loops
- for loops |
Functions