C++ Basics

50 %
50 %
Information about C++ Basics

Published on March 16, 2014

Author: inam12

Source: slideshare.net

C++ Basics Inam Ul Haq Lecturer in Computer Science University of Education Okara Campus inam@ue.edu.pk, inam.bth@gmail.com Subject: Programming Language for Mathematics Class: BS(Hons) Mathematics

Header Files // header files are preprocessor directives that are included to call built-in functions to be used in the program // e.g. for keywords: cin and cout, the header file iostream.h is used #include <iostream.h> Void main() { //variable declaration //read values input from user //computation and print output to user return 0; } Note: After you write a C++ program you compile it; that is, you run a program called compiler that checks whether the program follows the C++ syntax: • if it finds errors, it lists them • If there are no errors, it translates the C++ program into a program in machine language which you can execute UniversityofEducationOkara Campus 2

Notes • // is used for comments (this is not executed) • indentation is for the convenience of the reader; compiler ignores all spaces and new line ; the delimiter for the compiler is the semicolon • E.g. • Void main() • { • Clrscr(); • Int a,b; • If(a==b) • { • Cout<<“a is equal to b”<<endl; • all statements ended by semicolon • Lower vs. upper case matters!! • Void is different than void • Main is different that main UniversityofEducationOkara Campus 3

Hello world program When learning a new language, the first program people usually write is one that salutes the world :) Here is the Hello world program in C++. #include <iostream.h> Void main() { cout << “Hello world!”; getch(); } UniversityofEducationOkara Campus 4

Variable declaration Syntax: <variable-name> <type> e.g: • int n, average; //integer • Double m; //real number • char c, my-character//character UniversityofEducationOkara Campus 5

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

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; UniversityofEducationOkara Campus 7

If statements if (condition) { S1; } else { S2; } S3; condition S1 S2 S3 True False UniversityofEducationOkara Campus 8

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 UniversityofEducationOkara Campus 9

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)) … UniversityofEducationOkara Campus 10

If Statement #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; } UniversityofEducationOkara Campus 11

While Loop while (condition) { S1; } S2; condition S1 S2 True False UniversityofEducationOkara Campus 12

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; } UniversityofEducationOkara Campus 13

For Loop • for ( initialization; conidition; increment/decrement) • { • Statement 1’ • Statement 2; } UniversityofEducationOkara Campus 14

For Loop Example • #include <iostream> • int main () • { • for( int a = 10; a < 20; a = a + 1 ) • { • cout << "value of a: " << a << endl; • } • return 0; • } UniversityofEducationOkara Campus 15

Do While Loop • do {      block of code; statement 1; statement 2;      } while (condition); //it is called test condition UniversityofEducationOkara Campus 16

Do While Loop Example • #include <iostream>  • #include <conio.h> • int main ()  • {  • int a = 10; • do  •     {  •        cout << "value of a: " << a << endl;  •        a = a + 1;  •     } •   while( a < 20 );  • return 0;  • } UniversityofEducationOkara Campus 17

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, otherwise it prints  again the message • Are you really really sure you do not want to use this program? (y/n) • And so on,  every time adding one more “really”.  UniversityofEducationOkara Campus 18

Add a comment

Related presentations

Related pages

Damen Alle Basic Artikel günstig - c-and-a.com

Damen Basics für vielseitige Mode günstig bei C&A. Das A und O guten Kleidungsstils sind die geeigneten Damen Basics, auf denen Sie die modischen und ...
Read more

Herren Alle Produkte günstig - c-and-a.com

Jetzt günstig online bestellen: Herren Alle Produkte qualitätsgeprüft im C&A Online-Shop – schnelle Lieferung - versandkostenfrei bestellen bei ...
Read more

Basics : C'Real Basics Balance - st-hippolyt.de

C'Real Basics Balance Basics C´Real Basics besteht aus naturreinem Getreide, das zur Verbesserung der enzymatischen Kohlenhydratverdauung im Dünndarm ...
Read more

C Tutorial - Learn C - Cprogramming.com

A simple-to-understand introduction to C tutorial. Learn C today with examples, clear explanations and quizzes.
Read more

Learn How To Master The Basics Of C# ... - amazon.de

C#: C# CRASH COURSE – Beginner’s Course To Learn The Basics Of C# Programming In 24 Hours!: (c#, c programming, c, java, python, angularjs, c++ ...
Read more

Das C Tutorial (deutsch)

Das C Tutorial als Buch. Wem das Lesen am Bildschirm zu lästig wird, kann sich das C Tutorial "C-HowTo" auch in gedruckter Form als Buch bestellen.
Read more

C programming - C Tutorial

C tutorial for beginners - Learn ANSI, GNU and K/R standard of C programming language with simple and easy examples covering basic C, language basics ...
Read more

basic Bio-Genuss für alle

Wie alles begann Gründung der basic AG. Mitbegründer Georg Schweisfurth über die Gründung des Bio-Supermarktes ...
Read more

Damen | Basics | H&M DE

/ Basics; Filter Größe Wählen Sie bitte eine Kategorie, um nach Größe zu filtern ...
Read more

CXBASIS

Die CX-Basis hat es sich zur Aufgabe gemacht, all jenen mit Rat und Tat beizustehen, die deren Interesse für den Citroën CX teilen. Zum Programm gehört ...
Read more