What's new

Closed Basic Programming using C++ ----- 04 Control Structures part 1

Status
Not open for further replies.

fazz12

Honorary Poster
Joined
Aug 3, 2013
Posts
605
Reaction
291
Points
220
hello mga ka PH! ang topic po natin ngayon ay Control Structures. sorry kung mejo natagalan bago ulit ako nakapagpost ng bago. mejo naging busy lang nung mga nakaraang buwan. hayssss anyway continue tayo.

previous tutorial:
Tutorial - Basic Programming using C++ ----- 03 Basic Input and Operators - https://phcorner.net/t/basic-programming-using-c-03-basic-input-and-operators.85681/

Control Structures
ang mga control structures ang mga nagdedetermine kung san papunta ang logic ng program mo. sa program natin madalas nirurun ang code mo from simple up to down hanggang marating yung end of code. ok lang yon kung simpleng addition of two numbers or display of simple messages like "hello world" lang ang nagagawa ng program mo. pero what if may mga kumplikadong logic ka na gaya ng mamimili ka na ng mga operators na gagamitin mo sa calculator? hindi ka pwedeng mag add all the time kung ang gusto mong gawin ay magdivide
right?

if statement

ginagamit ang if statement kung gusto mong magrun ng code after nitong mameet ang isang certain requirement. meaning hindi laging magrurun ang code blocks na nasa loob ng if statements otherwise.

syntax:
if (condition para irun)
{
mga code statements na irrun kapag nameet yung requirement ng condition sa taas
}

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

int main()
{
int age;

cout<<"Enter your age."<<endl;
cin>>age;

cout<<"Your age: "<<age;

if(age <=5 )
{
cout<<"You are still young";
}


system("pause");
return 0;
}

sa program natin hiningi ng program na mag input yung user ng number na syang magiging value ng age na variable. sumunod, dinisplay ng program yung statement na
"Your age: " at sinunod na dinisplay yung age.
next ay yung if statement na
if(age<=5)
kung ang nilagay na input ng user ay 5 or pababa,
irrun ng program yung lahat ng nasa loob ng curly braces (statements sa pagitan ng { at })
in which sa case natin ngayon, ay yung cout<<"You are still young";

pero pano nalaman na ang dapat palang pagbasihan ng program ay 5 pababa? bakit hindi 5 pataas? o kaya 5 lang?
dahil yun sa operator na "<=" , na ang basa ay less that or equal.
marami pa nyan sa c++:
< less than
> greater than
>= greater than or equal to (hindi na sya kagaya ng sa math na > )
<= less than or equal to (hindi na sya kagaya ng sa math na < )

e pano sa equal? sabi mo sa past lessons mo ang = ay assignment operator na kay hindi na sya mag eevaluate diba?

answer: ang equality operator sa c++ ay ==
yup, hindi yan typo, dalawang equal sign talaga (no spaces in between ha).

more examples:
if(yourMoney >100000)
{
cout<<"wow you are rich!";
}

if(yourMoney < 1000)
{
cout<<"aw, you're poor, like me LOL";
}

if(age==20)
{
cout<<"you're not a teenager anymore. don't worry, we are still young at heart =) ";
}

take note na pwede ka mag lagay ng mas maraming code statements sa loob ng {}
if(age==20)
{
cout<<"you're not a teenager anymore. don't worry, we are still young at heart =) "<<endl;
cout<<"maybe you've finished your studies in college already"<<endl;
cout<<"about time you get some work";
//these statements will all work.
}

so pano ba ineevaluate yung mga conditions?
once na binasa na ng program yung conditions like

> if(y == 5) //think of the > as where the computer is currently reading your code
dalawa lang ang pwedeng maging value nyan,
true or false
if true yan
babasahin ng program ang nasa loob ng {} na mga statements
if(y == 5) //true ang evaluation
{
> some code statements;
some code statements;
some code statements;

}

kung false naman
if(y == 5) //false ang evaluation
{
some code statements;
some code statements;
some code statements;

}

>statements outside the condition;
statements outside the condition;
statements outside the condition;


//nag skip yung pagbabasa ng program

thus hindi lang limited sa mga operator sa taas ang pag eevealuate
pwede din tong gamiting pang evaluate

int life=0;
bool isPlayerDead;

if(life ==0)
{
isPlayerDead=true;
}

if(isPlayerDead)
{
cout<<"game over";
}
//mejo gets na ba kung pano iniimplement sa mga games yan? =)

next tutorial:
Tutorial - Basic Programming using C++ ----- 04 Control Structures part 2 - https://phcorner.net/t/basic-programming-using-c-04-control-structures-part-2.94643/
 
Last edited:
youre welcome..galing mo fazz12 ..pwede ka mag teacher....mag eenroll ako pag ikaw teacher hehehe...
 
pag nag coding ka may lalabas na pag kain if the statement is true...hahha
 
Status
Not open for further replies.

Similar threads

Back
Top