1. Welcome to PHCorner Forums. Take a moment to Sign up and gain unlimited access and extra privileges that guests are not entitled to, such as:

    All that and more! Registration is quick, simple and absolutely free. Join our community today!

Tutorial Basic Programming using C++ ----- 03 Basic Input and Operators

Discussion in 'Coding, Programming' started by fazz12, Mar 6, 2015.

  1. fazz12

    fazz12 Addict Established

    hello mga ka PH! ang topic po natin ngayon ay mas lengthy kaysa mga nakaraan. ito ay about sa basic input using cin, at mga operators.

    my previous post: Please or Register to view links

    operators
    may mga variables na tayo. may mga numerical values na din (as well as literal) silang kinocontain. now pano ba natin sila mamanipulahin?
    katulad ng basics ng math, may mga fundamental operators din tayo sa C++.

    assignment operator (=)
    ginagamit po ito upang mag assign ng value sa isang variable. ang general rule po nya, kung ano ang huling value na naiassign sa isang variable, yun ang magiging value nya (syempre). para ka lang nagpapalit ng sim card sa celphone mo. kung anong sim card ang sinaksak mo, yun din ang number mo. :D

    example:

    [HASHTAG]#include[/HASHTAG] "stdafx.h"
    [HASHTAG]#include[/HASHTAG] <iostream>
    [HASHTAG]#include[/HASHTAG] <cstdlib>
    using namespace std;

    int main()
    {
    int mykids = 0;

    cout<<"una nagkakilala kami, soon nagwedding..."<<endl;
    cout<<"my kids: "<<mykids<<endl;
    cout<<"unang taon ng pagsasama"<<endl;
    mykids = 1;
    cout<<"my kids: "<<mykids<<endl;
    cout<<"after 5 years"<<endl;
    mykids = 7;
    cout<<"my kids: "<<mykids<<endl;
    cout<<"after 3 more years"<<endl;
    mykids = 10;
    cout<<"my kids: "<<mykids<<endl; //abangan ang susunod na kabanata =D

    system("pause");

    return 0;
    }


    output:
    ss6.png

    kung ano ang huling value na inilagay ko sa varialble na mykids, lahat ng previous assignments ko sa kanya ay balewala na. mag assign man ako sa x ng 5, 7, 99 at 1000, pag ang last assignment ko sa kanya ay 0, 0 pa din ang value nya sa huli.

    difference in math's evaluation:
    hindi po natin kailangang ievaluate ang isang statement kung tama o mali gaya sa math. instead nag aasign po tau ng bagong value sa variable.
    halimbawa:
    int a= 4;
    int b=6;

    pagdating dito:
    a=b;

    sasabihin mo in math's point of view:
    "mali yun a, hindi sila equal".

    ganto po ang procedure ng pangyayari:
    a=b
    4=6
    6=6
    babaguhin po ng value na nasa kanan ng = sign ang value na nasa kaliwa.

    therefore,
    cout<<a;

    output: 6


    arithmetic operators (+,-,*,/,%)
    the four legendary arithmetic operators of math is here! in case di nyo pa sila kilala, sila ay sina +,-,*, at /
    diba dapat x si times?
    nope. ang x po ay ginagamiit na lang na variable sa c++ (at kahit saang programming language). ang symbol ng pagmmultiply sa c++ ay asterisk (*).

    let's see them in action:

    [HASHTAG]#include[/HASHTAG] "stdafx.h"
    [HASHTAG]#include[/HASHTAG] <iostream>
    [HASHTAG]#include[/HASHTAG] <cstdlib>
    using namespace std;

    int main()
    {
    int apples=5;
    int mangoes=6;
    int eatenApples=3;
    cout<<"apples: "<<apples<<endl;

    cout<<"5 apples + 6 mangoes = "<< apples + mangoes<<" in total"<<endl<<endl;
    cout<<"5 apples - 3 apples = "<< apples - eatenApples<<" apples left"<<endl<<endl;
    cout<<"multiple 5 apples to 3 = "<< apples * 3<<" apples"<<endl<<endl;
    cout<<"group 20 apples into 2 groups = "<<(apples * 4) /2<<" apples"<<endl<<endl;


    cout<<"group 5 apples into 2 groups. how many are left ungrouped?"<<endl;
    cout<<"answer: "<< apples % 2 <<endl<<endl;


    system("pause");

    return 0;
    }

    ss7.png


    modulus operator
    when you use the modulus operator '%' you are dividing a dividend over a diviser, but the returned answer is not the quotient, instead yung remainder.
    example
    int x= 20 % 17;
    the answer is 3.


    compound assignment
    ginagamitan ng combination ng assignment at arithmetic operator
    example:

    int b = 15;
    b += 4
    cout<< b;

    output:
    19

    katumbas po yan nito;
    b = b + 4
    kung ieevaluate:
    b = b + 4
    15 = 15+4
    15=19
    19=19

    same applies sa ibang operators like -=, *=, /= at %=

    mayron pang ibang compound operators like >>=, <<=, ^=, &= pero hindi ko sila halos ginagamit
    you can check on them sa web kung pano gamitin for further knowledge.

    cin (std::cin)
    ito po ang basic input na gagamitin natin sa c++.


    syntax:
    std::cin>>variable;

    where:
    yung variable ang paglalagyan ng data na ittype ng user


    create a program that will add two values:
    code:

    [HASHTAG]#include[/HASHTAG] "stdafx.h"
    [HASHTAG]#include[/HASHTAG] <iostream>
    [HASHTAG]#include[/HASHTAG] <cstdlib>
    using namespace std;

    int main()
    {
    int x;
    int y;

    cout<<"Enter the first number: ";
    cin>>x;
    cout<<"Enter the second number: ";
    cin>>y;

    cout<<"x: "<<x<<" + y: "<<y<<" = " <<x+y <<endl <<endl;


    system("pause");

    return 0;
    }

    output:

    ss8.png


    next tutorial:
    Please or Register to view links
     
    Last edited: Jul 18, 2015
    shivnta2002 likes this.
  2. derx

    derx El Vampiro Staff Member Moderator

    Thanks for the share! Keep it up! (y) :cat: :geek:
     
  3. DCCS

    DCCS Forum Veteran Established

    Cout and Cin na yan!! :D
     
  4. fazz12

    fazz12 Addict Established

    thanks sa support mga bossing. i'll keep on sharing. (y)(y)(y)
     
  5. DCCS

    DCCS Forum Veteran Established

    (y) /message Short

    (y) /message Short
     
    fazz12 likes this.
Tags / Keywords: