Sei sulla pagina 1di 13

PEMROGRAMAN BAHASA C++ TIARA DWI ANUGRAH D411 09 337

LESSON 1 : The Basics of C++


Intro to the C++ Language
#include <iostream> using namespace std; int main() { cout<<"HEY, you, I'm alive! Oh, and Hello World!\n"; cin.get(); }

#include <iostream> using namespace std; int main() { cout<<"HEY, you, I'm alive! Oh, and Hello World!\n"; cin.get(); return 1; }

Using Variables
#include <iostream> using namespace std; int main() { int thisisanumber; cout<<"Please enter a number: "; cin>> thisisanumber; cin.ignore(); cout<<"You entered: "<< thisisanumber <<"\n"; cin.get(); }

#include <iostream> using namespace std; int main() { int a,b,c,d; cout<< "please input the 'a' value : "; cin>> a; cin.ignore(); cout<< "then input the 'b' value : "; cin>> b; cin.ignore(); c=a+b; d=a*b; cout<< "the summation value is : " << c << endl; cout<< "the multiplication value is : " << d << endl; cin.get(); }

LESSON 2 : If Statements
#include <iostream> using namespace std; int main() { int age; cout<<"Please input your age: "; cin>> age; cin.ignore(); if ( age < 100 ) { cout<<"You are pretty young!\n"; } else if ( age == 100 ) { cout<<"You are old\n"; } else { cout<<"You are really old\n"; } cin.get(); } // Most important part of the program! // Need a variable... // // // // // Asks for age The input is put in age Throw away enter If the age is less than 100 Just to show you it works...

// I use else just to show an example // Just to show you it works... // Executed if no other statement is

#include <iostream> using namespace std; int main() { cout<< !( 1 || 0 ); //ANSWER: 0 cin.get(); }

#include <iostream> using namespace std; int main() { cout<< !( 1 || 1 && 0 ); //ANSWER: 0 (AND is evaluated before OR) cin.get(); }

#include <iostream> using namespace std; int main() { cout<< !( ( 1 || 0 ) && 0 ); //ANSWER: 1 (Parenthesis are useful) cin.get(); }

LESSON 3 : Loops
For
#include <iostream> using namespace std; // So the program can see cout and endl int main() { // The loop goes while x < 10, and x increases by one every loop for ( int x = 0; x < 10; x++ ) { // Keep in mind that the loop condition checks // the conditional statement before it loops again. // consequently, when x equals 10 the loop breaks. // x is updated before the condition is checked. cout<< x <<endl; } cin.get(); }

While
#include <iostream> using namespace std; // So we can see cout and endl int main() { int x = 0;

// Don't forget to declare variables

while ( x < 10 ) { // While x is less than 10 cout<< x <<endl; x++; // Update x so the condition can be met eventually } cin.get(); }

Do..While
#include <iostream> using namespace std; int main() { int x; x = 0; do { // "Hello, world!" is printed at least one time // even though the condition is false cout<<"Hello, world!\n"; } while ( x != 0 ); cin.get(); }

LESSON 4 : Functions

#include <iostream> using namespace std; int mult ( int x, int y ); int main() { int x; int y; cout<<"Please input two numbers to be multiplied: "; cin>> x >> y; cin.ignore(); cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n"; cin.get(); } int mult ( int x, int y ) { return x * y; }

Tugas halaman 22 C++ For Dummies


// // Template - provides a template to be used as the starting // point // // the following include files define the majority of // functions that any given program will need #include <cstdio> #include <cstdlib> #include <iostream> using namespace std; int main(int nNumberofArgs, char* pszArgs[]) { // your C++ code starts here // wait until user is ready before terminating program // to allow the user to see the program results system("PAUSE"); return 0; }

Semua sintax program-program tersebut telah dicompile dan dapat berjalan pada aplikasi Code Block.

(TIARA DWI ANUGRAH)

Potrebbero piacerti anche