0% found this document useful (0 votes)
58 views

C++ Basics: March 10th

The document provides an overview of basic C++ concepts including: 1) The structure of a simple C++ program with main function, variable declarations, input/output statements, and conditionals. 2) How to declare variables of basic data types like int, double, char and use input/output statements to read from and write to the user. 3) How to use conditional statements like if-else and loops like while to control program flow.

Uploaded by

sbalajisathya
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

C++ Basics: March 10th

The document provides an overview of basic C++ concepts including: 1) The structure of a simple C++ program with main function, variable declarations, input/output statements, and conditionals. 2) How to declare variables of basic data types like int, double, char and use input/output statements to read from and write to the user. 3) How to use conditional statements like if-else and loops like while to control program flow.

Uploaded by

sbalajisathya
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

C++ Basics

March 10th
A C++ program
//if necessary include headers
//#include <foo.h>

void main() {

//variable declaration
//read values input from user
//computation
//print output to user
}

Notes:
• what follows after // on the same line is considered comment
• indentation is for the reader; compiler ignores all spaces and new line ; the
delimiter for the compiler is the semicolon
• all statements ended by ;
• Lower vs. upper case matters!!
– Void is different than void
– Main is different that main
Variable declaration
type variable-name;
Meaning: variable <variable-name> will be a variable of type <type>

Where type can be:


– int //integer
– double //real number
– char //character

Example:
int a, b, c;
double x;
int sum;
char my-character;
Input statements
cin >> variable-name;
Meaning: read the value of variable <variable-name> from
the user

Example:
cin >> a;
cin >> b >> c;
cin >> x;
cin >> my-character;
Output statements
cout << variable-name;
Meaning: print the value of variable <variable-name> to the user
cout << “any message “;
Meaning: print the message within quotes to the user
cout << endl;
Meaning: print a new line

Example:
cout << a;
cout << b << c;
cout << “This is my character: “ << my-character << “ he he he”
<< endl;
If statements
True False
condition
if (condition) {
S1;
}
S1 S2
else {
S2;
}
S3;
S3
Boolean conditions
..are built using
• Comparison operators
== equal
!= not equal
< less than
> greater than
<= less than or equal
>= greater than or equal

• Boolean operators
&& and
|| or
! not
Examples
Assume we declared the following variables:
int a = 2, b=5, c=10;

Here are some examples of boolean conditions we can use:


• if (a == b) …
• if (a != b) …
• if (a <= b+c) …
• if(a <= b) && (b <= c) …
• if !((a < b) && (b<c)) …
If example
#include <iostream.h>

void main() {
int a,b,c;
cin >> a >> b >> c;

if (a <=b) {
cout << “min is “ << a << endl;
}
else {
cout << “ min is “ << b << endl;
}
cout << “happy now?” << endl;
}
While statements
True False
while (condition) { condition

S1;
} S1

S2;
S2
While example
//read 100 numbers from the user and output their sum
#include <iostream.h>

void main() {
int i, sum, x;
sum=0;
i=1;
while (i <= 100) {
cin >> x;
sum = sum + x;
i = i+1;
}
cout << “sum is “ << sum << endl;
}
Arrays
Used to store a collection of elements (variables)

type array-name[size];

Meaning:
This declares a variable called <array-name> which contains <size>
elements of type <type>
The elements of an array can be accessed as: array-name[0],…array-
name[size-1]

Example:
int a[100]; //a is a list of 100 integers, a[0], a[1], …a[99]
double b[50];
char c[10];
Array example
//Read 100 numbers from the user
#include <iostream.h>

void main() {

int i, a[100], n;

i=0; n=100;
while (i<n) {
cout << “Input element “ << i << “: ”;
cin >> a[i];
i = i+1;
}
//do somehing with it ..
}
Problems
Write a C++ program to read a sequence of (non-negative) integers from
the user ending with a negative integer and write out

• the average of the numbers


• the smallest number
• the largest number
• the range of the numbers (largest - smallest)

• Example:
– The user enters: 3, 1, 55, 89, 23, 45, -1
– Your program should compute X{3, 1, 55, 89, 23, 45}

You might also like