Introduction To Programming: Engr. Rashid Farid Chishti
Introduction To Programming: Engr. Rashid Farid Chishti
Programming
Engr. Rashid Farid Chishti
[email protected]
Chapter 03: Loops and Decisions
International Islamic University H-10, Islamabad, Pakistan
http://www.iiu.edu.pk
Relational Operators
//demonstrates relational operators
#include <iostream>
using namespace std;
int main(){
int n;
cout << "Enter a number: ";
cin >> n;
cout<<n<<"< 10 is "<<(n < 10)<<endl;
cout<<n<<"<=10 is "<<(n <= 10)<<endl;
cout<<n<<"> 10 is "<<(n > 10)<<endl;
cout<<n<<">=10 is "<<(n >= 10)<<endl;
cout<<n<<"==10 is "<<(n == 10)<<endl;
cout<<n<<"!=10 is "<<(n != 10)<<endl;
system("PAUSE");
return 0;
}
Number Systems
Base 16 Base 10 Base 8 Base 2
0 0 0 0000
1 1 1 0001
2 2 2 0010
3 3 3 0011
4 4 4 0100
5 5 5 0101
6 6 6 0110
7 7 7 0111
8 8 10 1000
9 9 11 1001
A 10 12 1010
B 11 13 1011
C 12 14 1100
D 13 15 1101
E 14 16 1110
F 15 17 1111
Bits and Bytes
All data is represented internally by
computers as sequences of bits.
Each bit can assume the value 0 or 1.
4 bits = 1 nibble
8 bits = 2 nibble or 1 byte
210 bytes = 1 Kilo Byte or 1KB
220 bytes = 1 Mega Byte or 1MB
230 bytes = 1 Giga Byte or 1GB
240 bytes = 1 Tera Byte or 1TB
Logic Gates and Bitwise
Operators
A B A OR B A AND B A XOR B NOT A NOT B
A|B A&B A^B ~A ~B
0 0 0 0 0 1 1
0 1 1 0 1 1 0
1 0 1 0 1 0 1
1 1 1 1 0 0 0
Bitwise Operators in C++ are
Bitwise AND ( & )
Bitwise OR ( | )
Bitwise Exclusive OR ( ^ )
Bitwise NOT ( ~ )
Left Shift ( << )
Right Shift ( >> )
Left Shift( << ) and Right Shift
( >> ) left shift (<<) and right shift (>>)
//demonstrates
#include <iostream>
using namespace std;
int main(){
short n;
cout << "Enter a number: "; cin >> n;
cout <<n<< "<<1 = " <<(n << 1)<<endl;
cout <<n<< "<<2 = " <<(n << 2)<<endl;
cout <<n<< "<<3 = " <<(n << 3)<<endl;
cout <<n<< "<<4 = " <<(n << 4)<<endl<<endl;
cout <<n<< ">>1 = " <<(n >> 1)<<endl;
cout <<n<< ">>2 = " <<(n >> 2)<<endl;
cout <<n<< ">>3 = " <<(n >> 3)<<endl;
cout <<n<< ">>4 = " <<(n >> 4)<<endl;
system("PAUSE"); return 0;
}
Bitwise Operators
#include <iostream>
#include <iomanip> // for setw()
using namespace std;
int main(void) {
short var1 = 0x0035 & 0x000F; // bitwise AND
short var2 = 0x0004 | 0x0068; // bitwise OR
short var3 = 0x0054 ^ 0x00F0; // bitwise XOR
short var4 = ~ 0x0055; // bitwise NOT
if(condition)
statement;
where condition is an integral expression and
statement is any executable statement.
The statement will be executed only if the value
of the integral expression is nonzero (TRUE).
When we want to run more than one statements
based on some condition, the syntax looks like
this:
if(condition){
many_statement_terminated_with ;
}
Decision: Using the if
statement
// This program tests if one positive integer is not
// divisible by another:
#include <iostream>
using namespace std;
int main(){
int n,d;
cout << "Enter two positive integers: ";
cin >> n >> d;
if (n%d)
cout << n << " is not divisible by " << d << endl;
system("PAUSE");
return 0;
}
Decision: Using the if
statement
// demonstrates IF with multiline body
#include <iostream>
using namespace std;
int main(){
int x;
cout << "Enter a number: ";
cin >> x;
if( x > 100 ){
cout << "The number " << x;
cout << " is greater than 100\n";
}
system("PAUSE");
return 0;
}
Decision: Using the if
statement
// The Minimum of Three Integers
#include <iostream>
using namespace std;
int main(){
int n1,n2,n3,min;
cout << "Enter three integers: ";
cin >> n1 >> n2 >> n3;
min=n1; // now min <= n1
if (n2 < min)
min = n2; // now min <= n1 and min <= n2
if (n3 < min)
min = n3; // now min <= n1,min <= n2,and min <= n3
cout << "Their minimum is " << min << endl;
system("PAUSE");
return 0;
}
Decision: Using the ifelse
statement
The if..else statement causes one of two
alternative statements to execute depending upon
whether the condition is true. Its syntax is
if(condition) statement1;
else statement2;
where condition is an integral expression and
statement1 and statement2 are executable state-
ments.
If the value of the condition is nonzero (TRUE)
then statement1 will execute; otherwise (FALSE)
statement2 will execute.
Decision: Using the ifelse
statement
// demonstrates Ifelse
#include <iostream>
using namespace std;
int main(){
int x;
cout << "Enter a number: ";
cin >> x;
if( x % 2 )
cout << "This is an Odd Number" <<endl;
else
cout << "This is an Even Number" <<endl;
system("PAUSE");
return 0;
}
Decision: Using Nested if
// Using Nested Selection Statements
#include <iostream>
using namespace std;
int main(){
int x,y;
cout << "Enter Value of X: "; cin >> x ;
cout << "Enter Value of Y: "; cin >> y ;
if ( x > 5 ){
if ( y > 5 )
cout << "(X > 5) and (Y > 5 ) \n";
else
cout<<"(X > 5 ) but (Y <= 5 )\n";
}
else
cout << "(Y might be > 5 ) but (X <= 5 ) \n";
system("PAUSE");
return 0;
}
Decision: Using the else if
statement
// demonstrates Ifelse
#include <iostream>
using namespace std;
int main(){
int age;
cout << "Enter your age: ";
cin >> age;
if (age < 18)
cout << "You are a child.\n";
else
if(age < 65)
cout << "You are an adult.\n";
else
cout << "you are a senior citizen.\n";
system("PAUSE"); return 0;
}
Decision: Using compound
Conditions
// using compound statements
#include <iostream>
using namespace std;
int main(){
int x,y;
cout << "Enter Value of X: "; cin >> x ;
cout << "Enter Value of Y: "; cin >> y ;
if (( x >= 5) &&( y >= 5))
cout << "(X >= 5) and (Y >= 5 ) \n";
if (( x > 5) &&( y < 5))
cout << "(X >= 5) and (Y < 5 ) \n";
if (( x < 5) &&( y >= 5))
cout << "(X < 5) and (Y >= 5 ) \n";
if (( x < 5) &&( y < 5))
cout << "(X < 5) and (Y < 5 ) \n";
system("PAUSE"); return 0;
}
Decision: Using compound
Conditions
// using compound statements
#include <iostream>
using namespace std;
int main(){
int n1,n2,n3;
cout << "Enter three integers: ";
cin >> n1 >> n2 >> n3;
if (n1 <= n2 && n1 <= n3)
cout << "Their minimum is " << n1 <<endl;
if (n2 <= n1 && n2 <= n3)
cout << "Their minimum is " << n2 <<endl;
if (n3 <= n1 && n3 <= n2)
cout << "Their minimum is " << n3 <<endl;
system("PAUSE");
return 0;
}
Decision: Using compound
Conditions
// using compound statements
#include <iostream>
using namespace std;
int main(){
char ans;
cout << "Are you enrolled (y/n): ";
cin >> ans;
if (ans == 'Y' || ans == 'y')
cout << "You are enrolled.\n";
else
cout << "You are not enrolled.\n";
system("PAUSE");
return 0;
}
Decision: Using the else if
statement
// This program converts a Marks into Grades
#include <iostream>
using namespace std;
int main(){ int score;
cout << "Enter your Marks in Subject: "; cin >> score;
if(score>100) cout<<"Error: Marks Can not be > 100\n";
else if (score >= 80) cout << "Your grade is [A] \n";
else if (score >= 75) cout << "Your grade is [B+] \n";
else if (score >= 70) cout << "Your grade is [B] \n";
else if (score >= 65) cout << "Your grade is [C+] \n";
else if (score >= 60) cout << "Your grade is [C] \n";
else if (score >= 55) cout << "Your grade is [D+] \n";
else if (score >= 50) cout << "Your grade is [D] \n";
else if (score >= 0) cout << "Your grade is [F] \n";
else cout << "Error: Marks can not be less than 0.\n";
system("PAUSE"); return 0;
}
Decision: Using the switch
statement
// demonstrates switch ... Case
#include <iostream>
using namespace std;
int main(){
int x,y; char op;
cout << "Enter two integers: "; cin >> x >> y;
cout << "Enter an operator [+,-,*,/,%]: "; cin >> op;
switch (op){
case '+': cout << x + y << endl; break;
case '-': cout << x - y << endl; break;
case '*': cout << x * y << endl; break;
case '/': cout << x / y << endl; break;
case '%': cout << x % y << endl; break;
default: cout <<"Invalid Operator \n";
}
system("PAUSE"); return 0;
}
Decision: Using the ? :
statement
// demonstrates ? :
#include <iostream>
using namespace std;
int main(){
int m,n;
cout << "Enter two integers: ";
cin >> m >> n;
cout << ( m < n ? m : n ) << is the minimum. \n;
system("PAUSE"); return 0;
}
// The conditional expression ( m<n ? m : n ) evaluates
// to m if m < n, and to n otherwise.
Loops
Loops cause a section of your program to be
repeated a certain number of times.
The repetition continues while a condition is
true.
When the condition becomes false, the loop
ends and control passes to the statements
following the loop.
There are three kinds of loops in C++:
the for loop,
the while loop,
and the do...while loop.
while loop
// Using Compound Statements
#include <iostream>
using namespace std;
int main()
{
int x,loop_count=0;
cout << "How much counting you want? ";
cin >> x ;
while(loop_count <= x)
{
cout << loop_count <<" ";
loop_count++;
}
system("PAUSE");
return 0;
}
while loop
// computes the sum 1 + 2 + 3 + + n,
#include <iostream>
using namespace std;
int main(){
int last_no, sum = 0, current_no = 1;
cout << "Enter a Positive Number: ";
cin >> last_no;
cout << "0";
while (current_no <= last_no){
sum = sum + current_no;
cout << "+" << current_no;
current_no++;
}
cout << " = " << sum <<endl;
system("PAUSE");
return 0;
}
Using break to terminate a loop
#include <iostream>
using namespace std;
int main(){
int last_no,sum=0, current_no=1;
cout << "Enter a Positive Number: ";
cin >> last_no;
cout << "0";
while (true){
if (current_no > last_no)
break; // terminates the loop immediately
sum = sum + current_no;
cout<<"+"<<current_no;
current_no++;
}
cout << " = " << sum <<endl;
system("PAUSE");
return 0;
}
Using break to terminate a
loop<iostream>
// prints all the Fibonacci numbers up to an input limit:
#include
using namespace std;
int main(){
long bound;
cout << "Enter a positive integer: "; cin >> bound;
cout << "Fibonacci numbers < " << bound << " = 0,1";
long f0=0,f1=1;
while (true){
long f2 = f0 + f1;
if (f2 > bound)
break; // terminates the loop immediately
cout << "," << f2;
f0 = f1; f1 = f2;
}
cout<<endl;
system("PAUSE"); return 0;
}
Using dowhile loop
#include <iostream>
using namespace std;
int main()
{
unsigned char choice=0;
do{
cout<<"\n Do U want exit [y,n]:";
cin >>choice;
}
while (choice!='y' && (choice!='Y'));