0% found this document useful (0 votes)
4 views

PF Week 6

Uploaded by

yasirasad.uos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

PF Week 6

Uploaded by

yasirasad.uos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

LECTURE NOTES / LAB HANDOUTS

PROGRAMMING FUNDAMENTALS (312-313) WEEK. 6

OBJECT: To understand if-else statement and switch statement

INTRODUCTION
There are three major decision making structures. The ‘if’ statement, the ‘if-else’ statement, and the ‘switch’
statement. Another less commonly used structure is the conditional operator.

THE IF STATEMENT
The if statement enables you to test for a condition (such as whether two variables are equal) and branch to
different parts of your code, depending on the result or the conditions with relational and logical operators
are also included.
The simplest form of an ‘if’ statement is:
if (expression)
statement;

THE IF-ELSE STATEMENT


Often your program will want to take one branch if your condition is true, another if it is false. If only one
statement is to be followed by the ‘if’ or ‘else’ condition then there is no need of parenthesis. The keyword
else can be used to perform this functionality:
if (expression)
{
statement/s;
}
else
{
statement/s;
}

PROGRAM 1: Demonstrating the simple if statement

#include <iostream>
using namespace std;

int main()
{
int a;
cout << "Enter a number: ";
cin >> a; //get number

if (a == 10)
cout << "The number is equal to 10";

if (a > 10)
cout << "The number is greater than 10";

if (a < 10)
cout << "The number is less than 10";

return 0;
}

Course Instructor: Nazish Basir Page: 1/8


Faculty of Engineering and Technology, University of Sindh.
LECTURE NOTES / LAB HANDOUTS
PROGRAMMING FUNDAMENTALS (312-313) WEEK. 6

PROGRAM 2: Demonstrating the simple if-else statement

#include <iostream>
using namespace std;
int main()
{
int age;
cout << "Enter age: "; cin >> age; //get number

if (age <= 15)


cout << " Welcome to children gift shop";
else
cout << " Welcome to glossary shop";

return 0;
}

PROGRAM 3: Demonstrating the if-else statement inside a for loop

#include <process.h> //for exit()


#include <iostream>
using namespace std;
int main()
{
unsigned long n, j;
cout << "Enter a number: "; cin >> n; //get number to test

for(j=2; j <= n/2; j++) //divide by every integer from


if(n%j == 0) //2 on up; if remainder is 0,
{ //it's divisible by j
cout << "It's not prime; divisible by " << j << endl;
exit(0); //exit from the program
}
cout << "It's prime\n";
return 0;
}

Course Instructor: Nazish Basir Page: 2/8


Faculty of Engineering and Technology, University of Sindh.
LECTURE NOTES / LAB HANDOUTS
PROGRAMMING FUNDAMENTALS (312-313) WEEK. 6

PROGRAM 4: Demonstrating the if-else statement inside a while loop

#include <iostream>
#include <conio.h> // for getche()
using namespace std;
int main()
{
int chcount=0; //counts non-space characters
int wdcount=1; //counts spaces between words
char ch = 'a'; //ensure it isn't '\r'

cout << "Enter a phrase: ";


while( ch != '\r' ) //loop until Enter typed
{
ch = getche(); //read one character
if( ch==' ' ) //if it's a space
wdcount++; //count a word
else //otherwise,
chcount++; //count a character
} //display results
cout << "\nWords=" << wdcount << endl
<< "Letters=" << (chcount-1) << endl;
return 0;
}

RAND() FUNCTION
The rand() function is used to generate random numbers. If we generate a sequence of random number with
rand() function, it will create the same sequence again and again every time program runs.

int rand(void):
Returns a pseudo-random number in the range of 0 to RAND_MAX. RAND_MAX: is a constant whose default
value may vary between implementations but it is granted to be at least 32767.

PROGRAM 5: Demonstrating the rand() function

#include <iostream>
#include <conio.h>
#include <cstdlib> // for rand() function
#include <iomanip> // for setw()

using namespace std;


int main() {
cout<<"RAND_MAX = "<<RAND_MAX<<endl;
for (int a=1 ; a<=10 ; a++)
if(a%4) //to display 4 values in one line
cout<<setw(8)<<rand();
else
cout<<setw(8)<<rand()<<endl;
return 0;
}

Course Instructor: Nazish Basir Page: 3/8


Faculty of Engineering and Technology, University of Sindh.
LECTURE NOTES / LAB HANDOUTS
PROGRAMMING FUNDAMENTALS (312-313) WEEK. 6

PROGRAM 6: Demonstrating the if-else inside do-while loop

#include <iostream>
#include <conio.h>
#include <cstdlib>
using namespace std;
int main() {

int a;
char ch;
do{
int x = (rand() % 5) + 1;
cout<<endl<<"Enter number between 1 to 5...";
cin>>a;
if (a==x)
cout<<"You guessed the right number";
else
cout<<"Sorry try again the number was "<< x;
cout<<endl<<"Do you want to run again?"
<<endl<<"Press y for yes...";
ch = getch();
}
while(ch=='y');

return 0;
}

Course Instructor: Nazish Basir Page: 4/8


Faculty of Engineering and Technology, University of Sindh.
LECTURE NOTES / LAB HANDOUTS
PROGRAMMING FUNDAMENTALS (312-313) WEEK. 6

IF...ELSE IF...ELSE STATEMENT


An if statement can be followed by an optional else if...else statement, which is very useful to test various
conditions using single if...else if statement. Once an else if succeeds, none of the remaining else if's or else's
will be tested.

PROGRAM 7: Demonstrate the else-if structure

#include <iostream>
using namespace std;
int main() {
int marks;
float per;
cout << " Enter marks out of (800)"; cin >> marks;
per = (float)marks/800 * 100;
if (per >= 80)
cout << "Percentage = " << per <<" \t A Grade :-D ";
else if (per >= 70)
cout << "Percentage = " << per <<"\t B+ Grade :-D ";
else if (per >= 60)
cout << "Percentage = " << per <<"\t B Grade :-) ";
else if (per >= 50)
cout << "Percentage = " << per <<"\t C Grade :-| ";
else if (per >= 40)
cout << "Percentage = " << per <<"\t D Grade :-| ";
else
cout << "Percentage = " << per <<"\t Fail :-( ";
return 0;
}

THE SWITCH STATEMENT


Unlike if, which evaluates one value, switch statements allow you to branch on any of a number of different
values. There must be break at the end of the statements of each case otherwise all the preceding cases will
be executed including the default condition. The general form of the switch statement is:

switch (identifier variable)


{
case identifier One:
statement(s);
break;
case identifier Two:
statement(s);
break;
....
case identifier N:
statement(s);
break;
default:
statement(s);
}

Course Instructor: Nazish Basir Page: 5/8


Faculty of Engineering and Technology, University of Sindh.
LECTURE NOTES / LAB HANDOUTS
PROGRAMMING FUNDAMENTALS (312-313) WEEK. 6

PROGRAM 8: Demonstrating the switch statement

#include <iostream>
using namespace std;

int main()
{
int a;
char ch;

do
{
cout<<"Enter any number from 1 to 10: "; cin>>a;
cout<<"The number is ";

switch(a)
{
case 1: cout<<"One"; break;
case 2: cout<<"Two"; break;
case 3: cout<<"Three"; break;
case 4: cout<<"Four"; break;
case 5: cout<<"Five"; break;
case 6: cout<<"Six"; break;
case 7: cout<<"Seven"; break;
case 8: cout<<"Eight"; break;
case 9: cout<<"Nine"; break;
case 10: cout<<"Ten"; break;
default: cout<<"Sorry cannot convert this number";
}
cout<<endl<<"Do U want to continue.... (Y / N)?"; cin>>ch;
}
while (ch =='y');
return 0;
}

Course Instructor: Nazish Basir Page: 6/8


Faculty of Engineering and Technology, University of Sindh.
LECTURE NOTES / LAB HANDOUTS
PROGRAMMING FUNDAMENTALS (312-313) WEEK. 6

PROGRAM 9 Demonstrating the switch statement

#include <iostream>
using namespace std;

int main()
{
int a, b;
char ch;
cout<<"Enter number operator number:";
cin>>a>>ch>>b;

switch(ch)
{
case '*':
cout<<"The answer is "<<a*b; break;
case '/':
if (b == 0)
{
cout<<"Cant divide with Zero"; break;
}
cout<<"The answer is "<<a/b; break;
case '-':
cout<<"The answer is "<<a-b; break;
case '+':
cout<<"The answer is "<<a+b; break;
default:
cout<<"Sorry wrong operator";
}
return 0;
}

Course Instructor: Nazish Basir Page: 7/8


Faculty of Engineering and Technology, University of Sindh.
LECTURE NOTES / LAB HANDOUTS
PROGRAMMING FUNDAMENTALS (312-313) WEEK. 6

Exercise 1:
Write a program to input day number (1-7) and print day of week name using switch case.
Input day number(1-7): 2
The day is Tuesday

Exercise 2:
Write a program to input month number and print total number of days in month using switch...case
Input month number(1-12): 2
Total number of days = 28
Exercise 3:
Write two programs to check whether an alphabet is vowel or consonant. First program by using
switch statement and second by using else-if structure.
Input character: a
'a' is a vowel

Input character: b
'b' is a consonant

Exercise 4:
Write a program which prints ASCII table from 129 to 255
(Use setw() for arrangement and make sure there should be only 8 ASCII numbers and characters on
each line. Take hint from program 5 of this handout)

Course Instructor: Nazish Basir Page: 8/8


Faculty of Engineering and Technology, University of Sindh.

You might also like