While (Again == y || Again == y) Cout << Enter a Number: Cin Num1 Cout Enter Another Number:

BASICs of C/C++ Programming

Writing simple C++ programs
Instance 1
//  Simple printing code.  #include <iostream> using namespace std;  int main() { 	int a = 10, b = xx; 	     cout << "sum is" << a + b << endl; 	cout << "production is " << a*b << endl;  	return 0; }          

Effort This Example!

A typical c++ program uses several header files in order to use the library routines that has been developed already. In the to a higher place instance, the statement #include <iostream> indicates that we need the I/O library. The argument "#using namespace std;" says that this example use output operator "<<" defined in the standard name space. In this example, a user has declared two integers a and b; and, they are initialized to 10 and xx respectively. Finally it computes the sum and products of a and b and outputs them on the panel equally shown below.

Instance 2
//  Simple printing code. //  #include <iostream> using namespace std;  int main() { 	int a, b; 	cout << "Input a:"; cin >> a; 	     cout << "Input b:"; cin >> b; 	     cout << "sum is" << a + b << endl; 	     cout << "product is " << a*b << endl;  	return 0; }          

Endeavor This Example!

This instance is very similar to the previous ane except that the user inputs the values "a" and "b" and they are read using the input operator "cin". Finally the code calculates and prints the values of the sum and product of a and b

Information types

Computer stores the variables in the memory. Therefore, estimator needs to know what kind of information we store and then that estimator reserves some memory space for the variable. A Byte [B] is the minimum corporeality of memory infinite for a figurer. 1 Byte is viii $.25 and a bit is either 0 or 1. 1KB=two^8=1024 Bytes Data type is defined as a fix of values together with a set of operations. C++ data types may be grouped into three categories:

  • Unproblematic
  • Structured
  • Pointer
  • Unproblematic Data Types:

    At that place are three categories inside the uncomplicated information blazon

      1. Integral: integer (Whole numbers)
      2. Floating-point: Real numbers
      3. Enumeration type: user-divers information blazon
      4. Important Variants integral information types are
          1. int
          2. bool
          3. Char

    It is also the number of bytes taken past a information type is compiler dependent. short or short int information type covers whole numbers that can be positive or negative (-128 to 127). unsigned short int takes only positive number (0 to 255)

    Examples:
    • -6728
    • 0
    • 78
    • +763
    • P78
        1. Note that + sign is optional
        2. No commas should be used inside an integer (e.g. 10.825 is wrong)

      In order to apply a variable in C++, the variables should be declared first with the types.

    Example.1

    Following program has int a, b, and c. a and b values are given and find c. Prints a, b, and c

    #include <iostream>  using namespace std;   int chief()   { 	int a=x, b=5, c; 	    c = a*b; 	        cout << "a = " << a <<", b= "<<b<<endl;  	cout << "c = " << c << endl;  	return 0; }          

    Endeavor This Case!

    Example.1:

    Following program has int a, b, and c. user tin enter a and b values and calculate c. Prints a, b, and c

                #include <iostream>  using namespace std;   int primary()   { 	int a, b, c;  	cout<<"Enter integer a and b values :";  	cin>>a>>b; 	c = a*b;  	cout << "a = " << a <<", b= "<<b<<endl;  	cout << "c = " << c << endl;  	return 0; }          

    Endeavour This Example!

    It is good habit to have prompt before cin command so that user knows that he has to respond.

    bool Data Blazon
  • bool blazon
    • Ii values: true and false
    • Manipulate logical (Boolean) expressions
  • true and false are called logical values
  • bool, true, and simulated are reserved words
  • char Data Type
  • The smallest integral data type
  • Used for characters: messages, digits, and special symbols
  • Each character is enclosed in single quotes
    • 'A', 'a', '0', '*', '+', '$', '&'
  • A bare space is a character and is written ' ', with a space left between the single quotes
  • Example
    #include <iostream>  using namespace std;   int primary()   { 	char x1='C';  	char x2='P';  	cout<<" This is a test..."<<endl;  	cout<<"Respond : "<<x1<<x2<<x2<<endl;  	return 0; }        

    Endeavor This Example!

    Result

    Instance

    It is good habit to have prompt before cin command so that user knows that he has to respond.

    #include <iostream>  using namespace std;   int chief()   { 	char x1='C', x2='P';  	cout<<" This is a test..."<<endl;  	cout<<" Reply : "<<x1<<x2<<x2<<endl;  	cout<<endl;   	return 0; }        

    Try This Example!

    Example
    #include <iostream>  using namespace std;   int master()   { 	char x1, x2;  	cout<<" Enter x1 and x2 chacaters:";  	cin>>x1>>x2;  	cout<<" This is a test..."<<endl;  	cout<<" Respond : "<<x1<<x2<<x2<<endl;  	cout<<endl;    return 0;  }        

    Try This Example!

    Issue

    Real Numbers
    1. C++ uses scientific notation to represent real numbers (floating-indicate note or scientific annotation)
    2. This allows a user to represent too minor and too big numbers
        1. 3.141592
        2. 0.3679
        3. 1.602e-19
        4. 3.4e+64 ( 50! L factorial)
        5. 1.2e9 (one.two GHz)
    3. Real numbers may be classified as bladder and double

    [We e'er use double in our code as it is more accurate than float]

    Example
    #include <iostream>  using namespace std;   int master()   { 	double x1, x2;  	cout<<" Enter x1 and x2 double numbers:";  	cin>>x1>>x2;  	cout<<" This is a test..."<<endl;  	cout<<" Full : "<< x1 <<" + "<< x2 <<" = "<< x1+x2 << endl;  	cout<<endl;    return 0; }        

    Try This Example!

    Result

    Example
    #include <iostream>  using namespace std;   int chief()   { 	double x1, x2, total;  	cout<<" Enter x1 and x2 double numbers:";  	cin>>x1>>x2; 	total=x1+x2;  	cout<<endl;  	cout<<" Total : "<< x1 <<" + "<< x2 <<" = "<< total << endl;  	cout<<endl;    return 0; }        
    Result

    Enumerated data types allows to designate a symbolic name for each integer.

    This method Improves readability of your code

      Examples
  • enum direction {North, S, East, Due west};
  • // Here North=0, South =ane, Eastward =2, and West =3

  • enum radixchoice {binary, octal, decimal, hex};
  • Now i define a variable around this enumerated blazon direction myheading = East; radixchoice mybase=hex;

    Variables

    Variable is a location in memory where a value can be stored. Declare variables with data type and proper name before utilize

    Example int x1; int x2; int sum;

    Y'all can declare several variables of same type in ane announcement Comma separated list

    int x1, x2, sum;

  • Variable name must be a valid identifier
  • Series of characters (messages, digits, underscores)
  • Cannot brainstorm with digit
  • Case sensitive (capital letter letters are unlike from lowercase letters)
  • Use identifiers of 31 characters or fewer
  • Avert identifiers that begin with underscores and double underscores, considering C++ compilers may use names like that for their own purposes internally
  • There are some reserved keywords for C++, avoid using them every bit variable names.
  • It is better to place a space after each comma (,) to make programs more readable. If you like y'all tin declare each variable on a separate line. So that yous to place a annotate next to each declaration
  • It is amend to place declarations at the kickoff of a function and separate them from the executable statements with one bare line to highlight where the declarations end and the executable statements begin
  • The following list shows the some reserved words in C++. These reserved words may not be used as constant or variable or whatsoever other identifier names.
  • Example
              #include <iostream>  using namespace std;   int main()   { 	double x1, x2, total;  	cout<<" Enter x1 and x2 double numbers:";  	cin>>x1>>x2; 	total=x1+x2;  	cout<<endl;  	cout<<" Total : "<< x1 <<" + "<< x2 <<" = "<< full << endl;  	cout<<endl;    render 0; }        

    Try This Example!

    Global Variables:

    Global variables are divers outside of all the functions, commonly on top of the program. The global variables are bachelor for employ the entire program after the declaration and can be accessed past whatsoever function in the plan. Following are some examples using global and local variables:

    Instance
    #include <iostream>  using namespace std;  // Global variable announcement: int Y;   int main ()   {  	// Local variable announcement:   	int a, b; // bodily initialization   	a =10;   	b =20;   	Y = a + b;   	cout <<"The total is :"<< Y<<endl;     return 0; }        

    Try This Example!

    Result

    Note:Note: If a plan tin can take same name for local and global variables, the value of local variable within a function will take preference.

    Example
    #include <iostream>  using namespace std;   // Global variable proclamation:   int Y;   int main ()   {  	// Local variable annunciation and nitialization :   	int a=x, b=xx,Y=xxx;  	      Y = a + b + Y;   	cout <<"The total is :"<< Y << endl;    return 0; }        

    Endeavour This Example!

    Result

    Operators:

    Operators tells the compiler to perform specific mathematical or logical operations. C++ has following built-in operators:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Consignment Operators
  • Arithmetic Operators:The post-obit arithmetics operators supported past C++ language:

    The increment operator ++ adds ane to its operand, and the decrement operator -- subtracts 1 from its operand

    Example:

    Example x = x+1; can be written equally x++; //postfix form

    Related Videos

    Render to peak Go back to peak menu

    petersonsuind1974.blogspot.com

    Source: https://www.cpp.edu/~elab/ECE114/Basic-of-C++.html

    0 Response to "While (Again == y || Again == y) Cout << Enter a Number: Cin Num1 Cout Enter Another Number:"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel