P. 1
c++basics

c++basics

|Views: 12|Likes:
Published by Shweta Mahajan

More info:

Published by: Shweta Mahajan on Feb 03, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PPT, PDF, TXT or read online from Scribd
See more
See less

02/03/2011

pdf

text

original

C++ Basics

CSci 107

A C++ program //include headers. the header is called iostream.h #include <iostream. it lists them ± If there are no errors. these are modules that include functions that you may use in your //program. we will almost always need to include the header that // defines cin and cout. that is. you run a program called compiler that checks whether the program follows the C++ syntax ± if it finds errors. it translates the C++ program into a program in machine language which you can execute . } After you write a C++ program you compile it.h> int main() { //variable declaration //read values input from user //computation and print output to user return 0.

compiler ignores all spaces and new line . the delimiter for the compiler is the semicolon all statements ended by semicolon Lower vs.Notes ‡ ‡ what follows after // on the same line is considered comment indentation is for the convenience of the reader. upper case matters!! ± Void is different than void ± Main is different that main ‡ ‡ .

} .The infamous Hello world program When learning a new language. return 0. the first program people usually write is one that salutes the world :) Here is the Hello world program in C++.h> int main() { cout << ³Hello world!´. #include <iostream.

double x. char my-character. Meaning: variable <variable-name> will be a variable of type <type> Where type can be: ± int ± double ± char //integer //real number //character Example: int a.Variable declaration type variable-name. int sum. c. . b.

Input statements cin >> variable-name. Meaning: read the value of the variable called <variablename> from the user Example: cin >> a. cin >> my-character. cin >> b >> c. cin >> x. .

Meaning: print the message within quotes to the user cout << endl. cout << b << c. Meaning: print the value of variable <variable-name> to the user cout << ³any message ³. . Meaning: print a new line Example: cout << a.Output statements cout << variable-name. cout << ³This is my character: ³ << my-character << ³ he he he´ << endl.

} else { S2. } S3.If statements True if (condition) { S1. condition False S1 S2 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 .

b=5. 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)) « .Examples Assume we declared the following variables: int a = 2. c=10.

} . if (a <=b) { cout << ³min is ³ << a << endl.b. cin >> a >> b >> c. } else { cout << ³ min is ³ << b << endl.c.If example #include <iostream.h> void main() { int a. } cout << ³happy now?´ << endl.

} S2. True condition False S1 S2 .While statements while (condition) { S1.

While example //read 100 numbers from the user and output their sum #include <iostream. } . i=1. while (i <= 100) { cin >> x. sum=0.h> void main() { int i. } cout << ³sum is ³ << sum << endl. i = i+1. sum = sum + x. x. sum.

. every time adding one more ³really´. otherwise it prints again the message ± Are you really really sure you do not want to use this program? (y/n) ± And so on.Exercise ‡ Write a program that asks the user ± Do you want to use this program? (y/n) ‡ If the user says µy¶ then the program terminates ‡ If the user says µn¶ then the program asks ± Are you really sure you do not want to use this program? (y/n) ± If the user says µn¶ it terminates.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->