C++ Loops and Decisions

2. Loops & Decisions

Relational operators

#include

using namespace std;

int main()

{

cout<<"Enter a number"x;

cout<<"\nNumebr < 10 is counter="<<(x<10)<<endl; //check if the number is < 10 and if true displays 1 and false 0

cout<<"\nNumebr = 10 is counter="<<(x==10)<<endl; //check if the number equals 10 note we use == not = which is assignment

cout< 10 is counter="<10)<10 note that 0 is false and any other value (-13 or 159) is true

return 0;

}
Enter a number
19

Numebr 10 is counter=1

Process returned 0 (0x0) execution time : 2.529 s

Loops

For loop

#include

using namespace std;

int main()

{

int x;

for (x=0; x<10; x++) //x=0 is initialization expression, x<10 is test expression, x++ is increment expression

cout<<x*x<<"\t"; //loop body

cout<<endl;

return 0;

}
0       1       4       9       16      25      36      49      64      81

Process returned 0 (0x0)   execution time : 0.419 s

Press any key to continue.
#include

#include

using namespace std;

int main()

{

int x;

for (x=0; x<10; x++)

{ //we have multiple statements in the loop, so we  must use those curly brackets

cout<<setw(4)<<x; //those statements are called a block

int c=x*x*x; //and this variable is only seen visible within this block

cout<<setw(6)<<c<<endl;

} //and end them

cout<<endl;

return 0;

}
0     0

1     1

2     8

3    27

4    64

5   125

6   216

7   343

8   512

9   729

Process returned 0 (0x0)   execution time : 0.390 s

Press any key to continue.
#include

using namespace std;

int main()

{

unsigned int x;

unsigned long factorial=1;

cout<=1; i--) //note that we can define a variable here, but it's seen in the whole program

{ //note that we can use more than an initialization expression and more than an increment expression, but only 1 test

factorial*=i;

} //or we can drop all expressions like for(;;)

cout<<"Factorial of "<<x<<" is "<<factorial<<endl;

return 0;

}
Enter a number

8

Factorial of 8 is 40320

Process returned 0 (0x0)   execution time : 2.713 s

Press any key to continue.

While loop

#include

using namespace std;

int main()

{

int x=10;

cout<<"Enter any number, type 0 to terminate" << endl;
while (x != 0)
{
	cout  x;
};

cout<<endl;

return 0;

}
Enter any number, type 0 to terminate

15

19

10

1954

46

0

Process returned 0 (0x0)   execution time : 14.132 s

Press any key to continue.
#include

using namespace std;

int main()

{

int x;

int x=1, p=1;

while(p<9999) //to make sure it fits into 4 digits format

{

cout<<setw(2)<<x;

cout<<setw(5)<<p<<endl;

p=x*x*x*x;

x++;

}

cout<<endl;

return 0;

}
1 1

2 1

3 16

4 81

5 256

6 625

7 1296

8 2401

9 4096

10 6561

Process returned 0 (0x0) execution time : 0.479 s

Press any key to continue.
#include

using namespace std;

int main()

{

unsigned long x=0,y=1;

const unsigned long l=999999999;

while(x<l/2) //see here the precedence; arithmetic operation has higher precedence

{

cout<<y<<" ";

long s=x+y;  //creates Fibonacci series which is next terms = summation of previous 2

x=y;

y=s;

}

cout<<endl;

return 0;

}
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 2

8657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702

887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 43

3494437 701408733

Process returned 0 (0x0) execution time : 0.408 s

Press any key to continue.

Do loop

#include

using namespace std;

int main()

{

long x,y;

char r;

do //use the do loop when you want the loop to run at least once

{

coutx;

couty;

cout<<"The quotient is "<<x/y;

cout<<" ,remainder is "<<x%y;

coutr;

}

while(r!='n');

return 0;

}
Enter Dividend: 18

Enter Divisor: 2

The quotient is 9 ,remainder is 0

Do another? (y/n) y

Enter Dividend: 6

Enter Divisor: 5

The quotient is 1 ,remainder is 1

Do another? (y/n) y

Enter Dividend: 87

Enter Divisor: 3

The quotient is 29 ,remainder is 0

Do another? (y/n) y

Enter Dividend: 10

Enter Divisor: 4

The quotient is 2 ,remainder is 2

Do another? (y/n) n

Process returned 0 (0x0) execution time : 58.066 s

Press any key to continue.

 

Decisions

If

#include

using namespace std;

int main()

{

int x;

cout<100)

{ //need curly brackets if more than one statement

cout<<"The number "<<x;

cout<<" is greater than 100"<<endl;

} //don’t forget to close them

return 0;

}
Enter a number

150

The number 150 is greater than 100

Process returned 0 (0x0) execution time : 2.995 s

Press any key to continue.
#include

#include //needed for exit(0) function

using namespace std;

int main()

{

unsigned long x,j;

cout<<"Enter a positive number"x;

for(j=2;j<=x/2;j++) //note that we don’t need brackets for the for loop because the if statement is seen as single statement by the loop

if(x%j==0)

{

cout<<"It's not prime";

exit(0);

}

cout<<"It's prime";

return 0;

}
Enter a positive number

13

It's prime

Process returned 0 (0x0) execution time : 2.407 s

Press any key to continue.

Enter a positive number

20

It's not prime

Process returned 0 (0x0) execution time : 1.863 s

Press any key to continue.
#include

using namespace std;

int main()

{

int x;

cout<100)

cout< 100 "<<endl;

else

cout<<"The number is < 100"<<endl;

return 0;

}
Enter a number

81

The number is 100

Process returned 0 (0x0) execution time : 4.497 s

Press any key to continue.
#include

#include //needed for getche() library function

using namespace std;

int main()

{

int c=0,w=1;

char m='a';

cout<<"Enter a phrase"<<endl;

while (m!='\r')

{

m=getche(); //that’s a new way to take input without waiting for the enter to be pressed, note the difference with getch() which doesn’t echo the character to the screen

if(m==' ') //space means w new word

w++; //so, update words counter

else //otherwise

c++; //update character counter

}

cout<<"\nNumber of words: "<<w<<" and number of characters: "<<c<<endl;

return 0;

}
Enter a phrase

i love programming

Number of words: 3 and number of characters: 17

Process returned 0 (0x0) execution time : 10.718 s

Press any key to continue.
#include

#include

using namespace std;

int main()

{

int c=0,w=1;

char m='a';

cout<<"Enter a phrase"<<endl;

while ((m=getche())!='\r') //this works as above, note we use parentheses because = lower precedence than !=

{

if(m==' ')

w++;

else

c++;

}

cout<<"\nNumber of words: "<<w<<" and number of characters: "<<c<<endl;

return 0;

}
Enter a phrase

I still love programming

Number of words: 4 and number of characters: 21

Process returned 0 (0x0) execution time : 10.542 s

Press any key to continue.
#include

#include

using namespace std;

int main()

{

int x=10,y=10;

char d='a';

cout<<"Your location is: "<<x<<" ,"<<y<<endl;

cout<<"Type Enter to quit"<<endl;

while (d!='\r')

{

cout<<"\nYour location is: "<<x<<","<<y<<endl;

cout<<"\nType your direction key (n,s,e,w)"<<endl;

d=getche();

if(d=='n')

y--;

else //take care of matching else with the correct if

if(d=='s')

y++;

else

if(d=='e')

x++;

else

if(d=='w')

x--;

}

return 0;

}
Your location is: 10 ,10

Type Enter to quit

Your location is: 10,10

Type your direction key (n,s,e,w)

n

Your location is: 10,9

Type your direction key (n,s,e,w)

e

Your location is: 11,9

Type your direction key (n,s,e,w)

w

Your location is: 10,9

Type your direction key (n,s,e,w)

n

Your location is: 10,8

Type your direction key (n,s,e,w)

s

Your location is: 10,9

Type your direction key (n,s,e,w)

Process returned 0 (0x0) execution time : 19.695 s

Press any key to continue.

Switch

#include

#include

using namespace std;

int main()

{

int x=10,y=10;

char d='a';

cout<<"Your location is: "<<x<<" ,"<<y<<endl;

cout<<"Type Enter to quit"<<endl;

while (d!='\r')

{

cout<<"\nYour location is: "<<x<<","<<y<<endl;

cout<<"\nType your direction key (n,s,e,w)"<<endl;

d=getche();

switch(d)

{

case 'n': y--; break;

case 's': y++; break;

case 'e': x++; break;

case 'w': x--; break;

case '\r': cout<<"\nQuitting\n"; break;

default: cout<<"\nTry Again!"<<endl;

}

}

return 0;

}
Your location is: 10 ,10

Type Enter to quit

Your location is: 10,10

Type your direction key (n,s,e,w)

n

Your location is: 10,9

Type your direction key (n,s,e,w)

o

Try Again!

Your location is: 10,9

Type your direction key (n,s,e,w)

r

Try Again!

Your location is: 10,9

Type your direction key (n,s,e,w)

s

Your location is: 10,10

Type your direction key (n,s,e,w)

e

Your location is: 11,10

Type your direction key (n,s,e,w)

e

Your location is: 12,10

Type your direction key (n,s,e,w)

e

Your location is: 13,10

Type your direction key (n,s,e,w)

n

Your location is: 13,9

Type your direction key (n,s,e,w)

n

Your location is: 13,8

Type your direction key (n,s,e,w)

w

Your location is: 12,8

Type your direction key (n,s,e,w)

Quitting

Process returned 0 (0x0) execution time : 32.634 s

Press any key to continue.

Conditional operator

#include

using namespace std;

int main()

{

for(int j=0;j<80;j++)

{

char c= (j%8) ? ' ' : 'x'; //that's conditional operator; the variable c is assigned space or x depending on test expression j%8 if true it's a space, if false, it's x

cout<<c;

}

return 0;

}
testing
on
#include

using namespace std;

int main()

{

for(int j=0;j<80;j++)

{

cout<<((j%8) ? ' ' : 'x'); // if true it's a space, if false, it's x

}

return 0;

}
x x x x x x x x x x

Process returned 0 (0x0) execution time : 0.138 s

Press any key to continue.

Logical Operator

#include

#include

#include

using namespace std;

int main()

{

int x=10,y=10;

char d='a';

cout<<"Type Enter to quit"<<endl;

while (d!='\r')

{

cout<<"\nYour location is: "<<x<<","<<y<<endl;

cout<<"\nType your direction key (n,s,e,w)"<<endl;

d=getche();

switch(d)

{

case 'n': y--; break;

case 's': y++; break;

case 'e': x++; break;

case 'w': x--; break;

case '\r': cout<<"\nQuitting\n"; break;

default: cout<<"\nTry Again!"<<endl;

}

if(x==7&&y==11)

{

cout<<"\nYou found the treasure! \n";

exit(0);

}

if(x15)

cout<<"\nBEWARE dragon here!\n";

if(!(x%7) && !(y%7))

cout<<"\nMushroom here!\n";

}

return 0;

}
Type Enter to quit

Your location is: 10,10

Type your direction key (n,s,e,w)

n

Your location is: 10,9

Type your direction key (n,s,e,w)

n

Your location is: 10,8

Type your direction key (n,s,e,w)

n

Your location is: 10,7

Type your direction key (n,s,e,w)

w

Your location is: 9,7

Type your direction key (n,s,e,w)

w

Your location is: 8,7

Type your direction key (n,s,e,w)

w

Mushroom here!

Your location is: 7,7

Type your direction key (n,s,e,w)

e

Your location is: 8,7

Type your direction key (n,s,e,w)

e

Your location is: 9,7

Type your direction key (n,s,e,w)

e

Your location is: 10,7

Type your direction key (n,s,e,w)

e

Your location is: 11,7

Type your direction key (n,s,e,w)

e

Your location is: 12,7

Type your direction key (n,s,e,w)

e

Your location is: 13,7

Type your direction key (n,s,e,w)

e

Mushroom here!

Your location is: 14,7

Type your direction key (n,s,e,w)

e

Your location is: 15,7

Type your direction key (n,s,e,w)

e

BEWARE dragon here!

Your location is: 16,7

Type your direction key (n,s,e,w)

e

BEWARE dragon here!

Your location is: 17,7

Type your direction key (n,s,e,w)

e

BEWARE dragon here!

Your location is: 18,7

Type your direction key (n,s,e,w)

w

BEWARE dragon here!

Your location is: 17,7

Type your direction key (n,s,e,w)

w

BEWARE dragon here!

Your location is: 16,7

Type your direction key (n,s,e,w)

w

Your location is: 15,7

Type your direction key (n,s,e,w)

w

Mushroom here!

Your location is: 14,7

Type your direction key (n,s,e,w)

w

Your location is: 13,7

Type your direction key (n,s,e,w)

w

Your location is: 12,7

Type your direction key (n,s,e,w)

w

Your location is: 11,7

Type your direction key (n,s,e,w)

w

Your location is: 10,7

Type your direction key (n,s,e,w)

w

Your location is: 9,7

Type your direction key (n,s,e,w)

w

Your location is: 8,7

Type your direction key (n,s,e,w)

w

Mushroom here!

Your location is: 7,7

Type your direction key (n,s,e,w)

s

Your location is: 7,8

Type your direction key (n,s,e,w)

s

Your location is: 7,9

Type your direction key (n,s,e,w)

s

Your location is: 7,10

Type your direction key (n,s,e,w)

s

You found the treasure!

Process returned 0 (0x0) execution time : 65.895 s

Press any key to continue.

Other Control Statements

#include

#include

#include

using namespace std;

int main()

{

const unsigned char w=219;

const unsigned char g=176;

unsigned char c;

for(int i=0;i<80*25-1;i++)

{

c=w;

for(int j=2;j<i;j++)

{

if(i%j==0)

{

c=g;

break; //get out the loop

}

}

cout<<c;

}

getch();

return 0;

}
████░█░█░░░█░█░░░█░█░░░█░░░░░█░█░░░░░█░░░█░█░░░█░░░░░█░░░░░█░█░░░░░█░░░█░█░░░░░█

░░░█░░░░░█░░░░░░░█░░░█░█░░░█░█░░░█░░░░░░░░░░░░░█░░░█░░░░░█░█░░░░░░░░░█░█░░░░░█░░

░░░█░░░█░░░░░█░░░░░█░█░░░░░░░░░█░█░░░█░█░░░░░░░░░░░█░░░░░░░░░░░█░░░█░█░░░█░░░░░█

░█░░░░░░░░░█░░░░░█░░░░░█░░░░░█░█░░░░░█░░░█░█░░░░░░░░░█░░░░░░░░░░░░░█░░░█░█░░░█░░

░░░░░░░░░░░█░░░░░█░░░░░░░░░█░█░░░█░░░░░█░░░░░░░█░░░░░█░░░░░█░░░█░░░░░█░░░░░░░█░░

░█░░░░░░░█░░░░░░░░░█░█░░░░░░░░░█░█░░░░░█░░░█░░░░░█░░░░░░░█░░░█░█░░░█░░░░░░░░░░░█

░░░░░░░█░░░█░░░░░░░█░░░█░░░░░█░░░░░░░░░░░█░█░░░░░░░░░░░░░░░░░█░░░░░█░░░░░░░░░█░░

░░░█░░░░░█░█░░░░░█░░░░░░░░░█░░░░░█░░░░░█░█░░░░░█░░░░░█░░░█░█░░░░░░░░░░░█░░░░░░░░

░█░█░░░█░░░░░█░░░░░█░█░░░░░░░░░░░█░░░█░░░░░█░░░░░░░█░░░░░░░░░█░░░░░░░█░░░░░░░░░█

░░░░░░░█░░░░░█░░░░░█░░░█░░░░░░░█░░░░░█░░░█░░░░░░░█░░░█░░░░░░░░░░░░░█░░░░░░░░░█░░

░░░░░░░░░█░█░░░░░░░░░█░█░░░█░█░░░░░░░░░█░░░░░░░░░░░░░█░░░█░█░░░█░░░░░░░░░░░░░█░░

░█░█░░░█░░░░░░░░░░░░░░░░░░░█░░░█░░░░░░░█░░░░░░░░░█░░░░░░░█░░░█░░░░░█░░░░░█░░░░░░

░░░░░░░█░░░█░░░░░█░░░░░█░░░░░░░█░░░░░█░░░░░░░░░░░█░░░█░░░░░█░█░░░░░░░░░█░█░░░░░█

░░░░░░░░░█░█░░░░░░░░░█░█░░░░░█░░░░░░░░░░░░░░░░░█░░░█░█░░░█░░░░░█░░░░░█░░░░░░░█░░

░░░█░░░░░█░░░░░░░░░░░░░░░░░░░░░█░█░░░░░░░░░█░░░░░░░█░░░░░░░░░█░░░░░█░░░░░█░░░░░░

░█░░░░░░░░░░░█░░░█░░░░░█░░░░░█░█░░░░░█░░░░░░░░░░░█░░░░░░░░░█░░░░░░░░░░░░░░░░░█░█

░░░█░░░░░█░█░░░░░█░░░█░█░░░█░░░░░░░░░░░█░█░░░░░█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

░█░░░░░█░░░░░█░░░░░░░█░░░░░░░░░░░░░░░░░█░░░░░░░░░█░░░░░░░░░░░░░█░░░█░█░░░█░░░░░█

░░░░░░░█░░░█░█░░░░░█░░░░░░░░░░░█░░░░░░░░░█░█░░░█░█░░░█░░░░░█░░░░░░░░░░░█░░░░░░░░

░░░█░░░░░░░█░░░░░░░░░░░█░░░░░█░░░█░░░░░█░░░░░░░█░░░█░░░░░░░█░░░█░░░░░░░░░░░░░█░░

░█░░░░░█░█░░░█░░░░░█░█░░░░░█░░░░░░░░░█░░░░░░░░░░░░░░░░░░░█░░░░░█░░░█░█░░░░░░░░░░

░░░░░░░░░░░░░█░░░█░█░░░░░░░░░█░░░░░░░░░░░█░█░░░░░░░░░█░░░░░░░█░░░░░█░░░░░█░░░░░█

░░░░░░░░░░░░░░░░░█░░░░░█░░░█░█░░░░░░░░░░░█░░░░░░░░░█░░░░░░░░░░░█░░░░░░░█░░░░░░░░

░░░░░░░█░░░░░░░░░░░░░█░░░░░█░░░█░█░░░█░█░░░░░░░░░█░░░░░░░░░░░█░░░░░█░░░░░█░░░░░░

░░░░░░░░░░░█░█░░░░░░░░░░░░░░░█░█░░░░░░░░░░░░░░░░░░░░░█░░░░░█░░░░░░░█░░░░░█░░░█░

Process returned 0 (0x0) execution time : 11.964 s

Press any key to continue.
#include

using namespace std;

int main()

{

long x,y;

char c;

do

{

coutx;

couty;

if(y==0)

{

cout<<"\n Illegal divisor\n"<<endl;

continue; //will get us back in the beginning of the loop

}

cout<<"Quotient is: "<<x/y;

cout<<" and remainder is: "<<x%y;

coutc;

}while(c!='n');

return 0;

}
Enter dividened

8

Enter divisor

2

Quotient is: 4 and remainder is: 0

Do another? (y/n): y

Enter dividened

9

Enter divisor

2

Quotient is: 4 and remainder is: 1

Do another? (y/n): y

Enter dividened

8

Enter divisor

0

Illegal divisor

Enter dividened

13

Enter divisor

3

Quotient is: 4 and remainder is: 1

Do another? (y/n): n

Process returned 0 (0x0) execution time : 51.597 s

Press any key to continue.

The goto statement

Old-fashioned!

Precedence

Unary

!,++,–

Arithmetic

Multiplicative *,/,%

Additive +,-

Relational

Inequality ,=

Equality ==,!=

Logical

And &&

Or ||

Conditional ?=

Assignment =,+=,*=,/=%=