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++ ----- 04 Control Structures part 3

Discussion in 'Coding, Programming' started by fazz12, Jul 18, 2015.

  1. fazz12

    fazz12 Addict Established

    hello mga ka PH! this is the part 3 of the our control structures

    previous post:
    Please or Register to view links

    AND , OR and NOT

    marami akong condition bago ko irun ang isang code block, masyado namang mahaba kung inenested if ko pa like this:
    if( age == 15)
    {
    if(isFit)
    {
    ...run statement
    }
    }

    pwede namang gumamit ng AND sa situation na yan
    && AND operator
    || OR operator
    ! NOT operator

    thus:
    bool isFit = true;
    age>= 15;
    if( age >= 15 && isFit) //true and true
    {
    // logic to join CAT
    }

    pag compund na yung evaluation gaya nyan,
    kapag && (AND) ang ginamit, dapat masatisfy both ng condition

    kapag naman || (OR) AT LEAST isa sa mga condition ang masatisfy

    if(isDoneWorking || isHalfWayDone) //pwedeng true and true, true or false, at false or true
    {
    // run statements
    }

    kapag naman ! (NOT) dapat kabaligtaran ng condition ang masatisfy

    if(! (x==1000) ) //negate a positive means negative
    {
    x++;
    }

    although pwede ding eto
    if(x!=1000 ) //negate a positive means negative
    {
    x++;
    }

    " != " means not equals to


    switch statement
    switch statements can be called a simpler version of if.. else
    syntax:

    switch (expression)
    {
    case condition:
    statements
    break;
    case condition;
    statements
    break;
    case condition;
    statements
    break;
    default:
    statements
    break;
    }

    example:

    #include "stdafx.h"
    #include <iostream>
    #include <cstdlib>
    using namespace std;

    int main()
    {
    gradeLetter;

    cout<<"Enter student's grade letter."<<endl;
    cin>>gradeLetter;


    switch (gradeLetter)
    {
    case 'A':
    cout<<"Outstanding";
    break;
    case 'B';
    cout<<"Very Satisfactory";
    break;
    case 'C';
    cout<<"Satisfactory";
    break;
    default:
    cout<<"Needs Improvement";
    break;
    }


    system("pause");
    return 0;
    }


    sa program mag iinput ng letter yung user ng A, B, C at D.
    depende sa input ng user ang magiging remark ng program
    kung A , ang output ay "Outstanding", and so on.
    kung ang iinput ng user ay D or E or F or anything else
    ang magiging remark ay yung nasa default which is yung needs improvement.

    that break though.. para san yan?

    yung break ang nagsasabi na yun na hanggang sa kanya na lang yung dulo ng isang case.
    kung mawawala yung break sa isang case, magtutuluy tuloy ang pag execute ng code from one case to another like this one:


    switch (gradeLetter)
    {
    case 'A':
    cout<<"Outstanding";
    break;
    case 'B';
    cout<<"Very Satisfactory";
    break;
    case 'C';
    cout<<"Satisfactory";
    break;
    case 'D';
    cout<<"Not Satisfactory" <<endl; //walang break sa case D
    default:
    cout<<"Needs Improvement";
    break;
    }

    in case na 'D'
    ang inilagay na input, ganto ang magiging output:
    Not Satisfactory
    Needs Improvement



    so pano kapag multiple condition?
    in case na maraming condition ang makakafulfill sa isang statement pwedeng ganito ang gawin
    int endDigit=4;
    switch (endDigit)
    {
    case 0:
    case 2;
    case 4;
    case 6;
    case 8;
    cout<<"The number is even";
    break;
    default:
    cout<<"The number is odd";
    break;
    }


    kahit na 4 ang value ng endDigit, since walang break mag "islide down"
    ang pagrun ng code into running code statements provided to case 8.
     
    hnaej_29 likes this.
  2. wala akong naintindihan sa c++ nung nag aaral ako...:ROFLMAO::ROFLMAO:....buti pa ikaw meron...
     
  3. thanks for sharing ;)
     
    fazz12 likes this.
  4. thanks for sharing sir
     
Tags / Keywords: