What's new

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

Status
Not open for further replies.

fazz12

Honorary Poster
Joined
Aug 3, 2013
Posts
605
Reaction
291
Points
213
hello mga ka PH! ang topic po natin ngayon ay Control Structures part 2 . 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++ ----- 04 Control Structures part 1 - https://phcorner.net/t/basic-programming-using-c-04-control-structures-part-1.94638/

if . . else statement

if else give you more control on how to branch out your program logic.
kung ang if statement irrun mo lang ang code na nasa loob ng if kung mameet yung condition ng if, ang else statement naman irrun mo yung statement KUNG hindi nameet ng inevaluate na expression ang condition ng if. so parang yung else yung nagsisilbing default given na hindi magrun ang nasa loob ng if.

syntax:
ganun pa din naman ang syntax sa if, dadagdagan lang natin ng else

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

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

int main()
{
int age;

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

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

if(age <=16)
{
cout<<"You are still too young, you can't register on this site yet";
}
else
{
cout<<"You can register on this site.";
}


system("pause");
return 0;
}


sa program na ginawa ko nanghingi ng age yung program sa user. kung ang ilagay nyang number value ng age ay 16 or pababa, ang irerespond ng program ay yung message na
"You are still too young, you can't register on this site yet".
pero kung ang ininput ng user ay 17 pataas,
ang iaoutput na message ay
"You can register on this site."

ngayon limitado lang ba sa dalawang logical condition ang program? of course not. at dahil jan pwede kang gumamit ng:

if .. else if ... else statement


kapag madaming condition ang expected ng programmer sa logic ng program nya, ginagamit nya ang if, else if at else statements.

syntax:
if(condition A)
{
codes to run..
}
else if (condition B)
{
codes to run..
}
else if (condition C)
{
codes to run..
}
else if (condition D)
{
codes to run..
}
else if (condition E)
{
codes to run..
}
else
{
default codes to run
}

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

int main()
{
int command;
int num1, num2;

cout<<"Select command number:"<<endl;
cout<<"1 - Add"<<endl;
cout<<"2 - Subtract"<<endl;
cout<<"3 - Multiply"<<endl;
cout<<"4 - Divide"<<endl;

cout<<"Your command: ";
cin>>command;

cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;

if(command==1 )
{
cout<<"Sum: "<< num1 + num2;
}
else if(command==2)
{
cout<<"Difference: "<< num1 - num2;
}
else if(command==3)
{
cout<<"Product: "<< num1 * num2;
}
else if(command==4)
{
if(num2==0)
{
cout<<"Undefined";
}
else
{
cout<<"Quotient: "<< num1 / num2;
}

}
else
{
cout<<"Not a valid command.";
}


system("pause");
return 0;
}


sa program hiningan si user ng program ng number as command, from 1 to 4.
tapos hiningan din si user ng dalawang numbers.
after na ibigay ni user yung mga hinihinging input,
kung:
1 ang binigay nya as command, iaadd ng program yung dalawang numbers
2 issubtract
3 immultiply
4 ididivide
anything else na iinput ng user maliban dun sa apat, ang ididisplay ng program ay
"Not a valid command."


take note na sa loob nito:
else if(command==4)
{
...
}

may if else condition ulit. ito yon:
if(num2==0)
{
cout<<"Undefined";
}
else
{
cout<<"Quotient: "<< num1 / num2;
}


ginawa natin to para hindi ituloy ng program ang pagdivide sa dalawang number kung sakaling ang value ng num2 ay 0 (or else magccrash ang program, we all know that we cant divide any number by zero, just as we cant eat 2 or 3 pizza slices out of 0 pizza slices right?)

sinasabing "nested" if ang isang if.. na nasa loob pa ng isang if.. statement thus we call them
nested if.

kung napapansin nyo, hindi pwede ang else if kung walang naunang if sa taas nito. hindi din pwede ang else nang walang if, pero gaya ng sa part 1 ng tuts ko na to, pwede ang if nang walang else if at else.

kung ganon, bakit pa ko nag eelse if at else
kung pwede namang ganto na lang:
if(condition1)
{
...
}

if(condition 2)
{
...
}

if(condition3)
{
...
}

(puro if condition lang)

althoug pwede yan, may mga pagkakataon sa mga program natin na kailangang ISA lang sa mga statements na yan ang mag run, depende sa kundisyon ng program mo, highly situational yan e.

kasi halimbawang mag true ang nasa if

>if(x == 1) //true
{
> ..run these..
..run these..
}
else if(x== 2) //ignored
{}
else //ignored
{}

hindi na irrun pa yung mga nasa baba na mga else if at if kasi nabranch out na ng program kung alin lang sa mga code blocks ang dapat nyang irun.
unlike kapag ganito:

if(isAlive)
{
//feed the character
}

if(isAButterfly)
{
//burn it
}

o diba if ever na maging true yan both hindi magtutugma ang logic ng program mo.
minsan kasi may mga logic tayong total opposite sa isat isa, na hindi pwedeng irun ang side A
kapag nagrun na ang side B.
also aksaya pa sa time ng program kasi iisa isahin pa yang icheck ng program, despite alam mo na dapat na nyang itigil ang pagcheck sa condition.

>if(isAlive) //true
{
//feed the character
}

>if(isDead) //why still check this if you already know that the answer is false?
{
//do something
}

next post
Tutorial - Basic Programming using C++ ----- 04 Control Structures part 3 - https://phcorner.net/t/basic-programming-using-c-04-control-structures-part-3.94664/
 
Last edited:
Dear fazz12,

Since 2 years have passed since the last reply in this thread, I am locking it to prevent necroposting. Feel free to start a new thread or contact any forum staff if you want this to be reopened.

Thread closed.
 
Status
Not open for further replies.

Similar threads

Back
Top