Chapter 2

 

 

 

Text Box:  
cout  (Outputing Data)
 
// This is a comment. 
// The computer will ignore lines following two backslashes 
// Commenting is important when your program is complex.  
// This will help others follow your code
 
/* To comments a block, 
start with a backslash and a star, 
and end with a star followed by a backslash. */
	
#include <iostream.h> 
// iostream.h is a file containing code for input/ouput operations		
main() // Every program must have a main function.  
/* Don’t worry, in the next unit you’ll learn how to create your OWN function!! */
{
cout<<" \t Hello World! \n"; 
    // ‘\t’ is just a tab and ‘\n’ is an endline  
	
cout<<"Welcome to Computer Programming.";
cout<<endl;	// endline
	
cout<<"I hope you have fun this semester!"<<endl;
 
return 0; // Some functions return integers.  
    // You will learn more about this later.
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
Declaring and Initializing
 
#include <iostream.h>
main()
{
int peterpan;  /* When you declare an integer (in this case 
peterpan) the computer will reserve space in memory for it */

int tinkerbell=5;  /* tinkerbell is assigned a value of 5. What is 
on the right is always assigned to what is on the left. */

peterpan=16;  /* I didn’t assign a value to peterpan at first, 
     		but now I’m assigning it a value of 16. */
 
cout<<"peterpan: "<<peterpan;
cout<<endl;
 
cout<<"tinkerbell: "<<tinkerbell<<endl;
 
return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
cin (Inputing Data)     
 
#include <iostream.h>
main()
{
	int simba;
 
cout<<”Insert an integer:“<<endl;
cin>>simba;		// Computer reads in an integer
	
cout<<simba<<endl;
 
return 0;
 
  // The output that follows depends on whatever the user inputs.
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Picture from "Beauty and the Beast" (C) The Walt Disney Co.
 

 

 


Text Box:  
Different Data Types
 
#include <iostream.h>
main()
{
	int belle;	
	float beast;	// floats are used for decimals
	double mrs_pots;	/* doubles can store values of much greater
		magnitude or with greater precision than floats */
 
	belle=4.6;	// belle is an integer. 
/* The computer always truncates decimals if they are assigned 
to an int. */
	
beast=4.6;
	mrs_pots=4.6;
 
	cout<<"belle (int): "<<belle<<endl;	
	cout<<"beast (float): "<<beast<<endl;
	cout<<"mrs_pots (double): "<<mrs_pots<<endl;
 
	cout<<endl;
 
	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
Using Operators
 
#include <iostream.h>
main()
{
	int sneezy=2, sleepy=7, happy=12;
 
	cout<<sneezy+sleepy<<endl;	// 2+7
 
	cout<<sneezy*happy-sleepy<<endl;  /*  2 * 12 – 7
		Don’t forget your order of operations (multipiclation and 
division come before addition and subtraction */
	
// Parentheses are used here to emphasize order of operations:
cout<<sneezy*(happy-sleepy)<<endl;	// 2 * (12-7)

 	cout<<happy/sneezy<<endl;	// 	12/2 
 
	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
Modulus Operator 
 
#include <iostream.h>
main()
{
   
/* The mod operator (%) is associated to the remainder of a certain 
division. */

int tarzan=10;
	int t;
 
	t=tarzan%5;		// The remainder when 10 is divided by 5 is 0
	cout<<t<<endl;
 
	t=tarzan%4;		// The remainder when 10 is divided by 4 is 2
    	cout<<t<<endl;
	
	t=tarzan%3;		// The remainder when 10 is divided by 3 is 1
	cout<<t<<endl;
	
	t=tarzan%7;		// The remainder when 10 is divided by 7 is 3
	cout<<t<<endl;
 
	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
Conditional Expressions
 
#include <iostream.h>
 
main()
{
	int snow_white;
 
	cout<<"Insert amount of apples: ";
	cin>>snow_white;
	cout<<"You have "<<snow_white
 		    << (snow_white==1 ? " apple." : " apples.") <<endl;	
	
// First operand is a condition (Is there just one apple?)
	// Second operand is what should print out if condition is TRUE
	// Third operand is what should print out if condition is FALSE
 
	cout<<endl<<"Insert new amount of apples: ";
	cin>>snow_white;
	cout<<"Now you have "<<snow_white
    << (snow_white==1 ? " apple." : " apples.") <<endl;
 
	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
Characters
 
#include <iostream.h>
main()
{ 
	char robin_hood=’*’;	// robin_hood is a character, the *

	char maid_marian;		
 
	cout<<"Robin_hood: "<<robin_hood<<endl;
	
	cout<<"Insert a character: ";
	cin>>maid_marian; 		
 
cout<<"Maid_marian: "<<maid_marian<<endl;  /* You can’t have two 
symbols together and expect the computer to consider it as one 
single character (In this case I inputed ‘&*’, the and the 
computer interpreted Maid_marian as ‘&’ only) */

return 0;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

dumbo18.gif (13062 bytes)
 

 

 


Text Box:  
if, else if, else
 
#include <iostream.h>
main()
{
	float dumbo;
 
	cout<<"Insert an integer: ";
	cin>>dumbo;
 
	if (dumbo<50)
		cout<<"Your number is less than 50"<<endl;
	
else if (dumbo==50) /* Notice how 2 equal signs are used to check if 
      			   dumbo is equal to 50.*/
		cout<<"Your number is 50"<<endl;
	
else	/* Don’t make the computer waste time checking using an 
      else if (dumbo>50)*/
		cout<<"Your number is greater than 50"<<endl;
 
	
return 0;
 
/* You can use else if as many times as you want, or you could just have an if followed by an else */
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
Switch Statements
 
#include <iostream.h>
main()
{
	int cruella_de_vil;
 
	cout<<"Insert an integer from 1-5: ";
	cin>>cruella_de_vil;
 
	cout<<endl<<endl<<"CRUELLA DE VIL FUN FACT:"<<endl<<endl;
	
switch (cruella_de_vil) /* A switch statement is a convenient 
way of testing the values of an integers (instead of using a 
whole bunch of if/else if statements)*/
	{
		case 1: 
			cout<<"Role: comic villain \n";
			break;
		case 2:
			cout<<"Name derives from the words cruel and devil \n";
			break;
		case 3:
			cout<<"She loves dalmatians \n";
			break;			
		case 4:
			cout<<"Debut: January 25, 1961 \n";
            	break;
		default:  // For any other number
			cout<<"Hobbies: bad driving and wearing fur coats \n";
			// You don’t need a break after the last case
	};
 
	cout<<endl;
	
	return 0;
 
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
Increment and Decrement Operators
 
#include <iostream.h>
main()
{

//This concept will be very important when using loops (next 3 programs)

int jiminy, cricket, fairy;
 
	
jiminy=10;
	cout<<"Initial value of jiminy: "<<jiminy<<endl;
		cout<<"jiminy++: "<<jiminy++<<endl;	 /* Print 10 and then 
   postincrement jiminy */
	
cout<<"Final value of jiminy: "<<jiminy<<endl<<endl;
	
	
cricket=15;
	cout<<"Initial value of cricket: "<<cricket<<endl;
	cout<<"++cricket: "<<++cricket<<endl;  /* Preincrement cricket and 
    then print 16 */
	
cout<<"Final value of cricket: "<<cricket<<endl<<endl;
 
	
fairy=4;
	cout<<"Initial value of fairy: "<<fairy<<endl;
	fairy-=2;	/* This is essentially the same thing as reassigning 
   fairy: fairy=fairy-2 */
	cout<<fairy<<endl<<endl;
 
	
fairy=4;
	fairy=fairy-2;	// Reassignment of fairy 
	cout<<fairy<<endl<<endl;
 
	return 0;
 
}