What's new

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

Status
Not open for further replies.

fazz12

Honorary Poster
Joined
Aug 3, 2013
Posts
605
Reaction
291
Points
213
hello mga ka PH! this is the part 3 of the our control structures

previous post:
Tutorial - Basic Programming using C++ ----- 04 Control Structures part 2 - https://phcorner.net/t/basic-programming-using-c-04-control-structures-part-2.94643/

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.
 
wala akong naintindihan sa c++ nung nag aaral ako...:ROFLMAO::ROFLMAO:....buti pa ikaw meron...
 
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