Easier View

Very sorry, The posts are all mixed-up . View blog archieve to start learning =D

About Me

My name is Luke Chin. Here to teach you guys how to code C++

Thursday, April 12, 2007

Summary

Ok, You have learnt many things so far. If , for , while . different types of statements and conditions.

The For Statement

//The for statement is much similar to the while statement loop wise

#include
using std::cout;
using std::cin;
using std::endl; // if you havent seen this before it justs ends the line

int main()
{

for (int A = 1 ; A<= 30 ; A++)
{
cout << A << endl;

}

system("pause");
return 0;
}

-the for statement is made up of 3 parts

for ("initalization" ; "condition" ; "action")

The while Statement

// The while statement is used to repeat a sequence of statements as long as they are true


#include
using std::cout;
using std::cin;
using std::endl; // if you havent seen this before it justs ends the line

int main()
{
int A;
A=1 ; //just making the variable A which is a number to 1

while (A<=30) //while A lesser or equal to 30 (start)
{
cout << A << endl; // print A
A++; // A + 1
} // (end) the loop just prints A and adds 1 to it prints A again until 30

system("pause");
return 0;
}

The if statement ( A step ahead pay attention)

// the if statement is used to choose alternative courses of actions

#include
using std::cout; //this states that your using it so theres nomore (std::cout) just (cout)
using std::cin; // same as above but for cin

int main()
{
int grade;
cout << " Enter a grade ";
cin >> grade;

if (grade>=90)
cout << "The grade is a A";

if(grade>=80)
cout << "The grade is a B";

if(grade>=70)
cout << "The grade is a C";

system("pause");

return 0;
}


// This is a simple example of if (we will be getting more into the IF later)

-here are some things you need to know

sign meaning
> greater than
< lesser than
>= greater than or equal to
< = lesser than or equal to
== equal to
!= not equal

Doing mathematics in C++

#include

int main()
{
int num1,product,num2;


std::cout << "enter integer 1 :"; std::cin >> num1;
std::cout << "enter integer 2 :"; std::cin >> num2;
product = num1 * num2;
std::cout << product;
system("pause");
return 0;
}


-do you understand what this code does? it asks the user for 2 numbers and multiplies them
and prints the product to the screen.

sign meaning
+ addition
- subtraction
++ increment +1
-- decrement -1
/ divide
* multiplication
% mudulus // returns the remainder

Wednesday, April 11, 2007

Using input in programs (Strings)

#include // input output header file
#include // string header file

int main() // you MAIN function

{
std::string name; // string varaible name

std::cout << "enter your name"; // prompting for the users name std::cin >> name; // accepting name
std::cout << "Hi , " << name; // printing hi , name

system("pause"); // this is used to pause the application


return 0; // ending your program
}

Your First C++ Program =D

#include // input output header file

int main() // you MAIN function
{
std::cout << "Helloworld"; // printing helloworld to the screen

system("pause"); // this is used to pause the application

return 0; // ending your program
}