Programming Fundamentals
Taimur Sajjad
MS(Mob. Computing)
MS(Data Science)
Lecturer (Computer Science),,
CUI Wah Campus
2
Nested if
An if statement can be inside another if statement to form a nested if statement.
Task 1: Write a code using nested if in which user enter three numbers, and find if 1st number is greater or
not.
int num1,num2,num3
cin>>num1>>num2>>num3;
if(num>num2)
{
If(num2>num3)
{
cout<<num1<<“ is
greater”
}
else
{
cout<<num1<<“ is
not greater”;
}
}
else
{
cout<<num1<<“ is not
Matching the “else”
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cout<<“Enter three numbers, a, b, and c:\n”;
cin >> a >> b >> c;
if( a==b )
if( a==b )
{ if( b==c )
if( b==c cout
) << “a, b, and c are the same\
n”; cout << “a, b, and c are the same\n”;
} else
else cout << “b and c are different\n”;
else
cout
cout<< “a <<and“ab and b are different\n”;
are different\n”;
return 0;
Conditional Operator (Ternary operator)
if (x > 0)
y = 1;
else
y = -1;
is equivalent to
y = (x > 0) ? 1 : -1;
Syntax:
result = (condition) ? Expression1 : Expression2;
Conditional Operator, examples
cout << ((num % 2 == 0) ? "num is even" : "num is odd");
int min_value = (num1<num2) ? num1 : num2;
unsigned int absvalue = (n< 0) ? -n : n;
Switch - Syntax
switch (Variable)
{
case <value_1> : statement1;
statement2;
statement3;
break;
case <value_2> : statement1;
statement2;
break;
case <value_3> : statement1;
break;
default: statement1; Optional
statement2;
}
Switch – Example-1
char grade;
cin>>grade;
switch (grade)
{
case ‘A’: tution_fees *= 0.20;
break;
case ‘B’: tution_fees *= 0.40;
break;
case ‘C’: tution_fees *= 0.60;
break;
default: tution_fees *= 1;
}
Switch – Example-2
int day;
cout<<“Enter day number“; cin>>day;
switch (day)
{
case 1 :
case 7 : cout << "This is a weekend day";
break;
case 2 :
case 3 :
case 4 :
case 5 :
case 6 : cout << "This is a weekday"; break;
default : cout << "Not a legal day";
}
Switch – Example-3
int a, b; char key;
cout<<“\nEnter numbers: “;
cin>>a>>b;
cout<<“Enter an arithmetic operator”;
cin>> key;
switch (key)
{
case ‘+’ : cout<<(a+b); break;
case ‘-’ : cout<<(a-b); break;
case ‘*’ : cout<<(a*b); break;
case ‘/’ : cout<<(a/b); break;
default : cout<<“Error: Invalid key…";
}
Class Exercise-1
What will be the output of the following code?
int marks = 60;
switch(marks)
{
case 50 : cout<<”Passed”;
case 60 : cout<<”Got C”;
case 70 : cout<<”Got B”;
case 80 : cout<<”Got A”;
}
Class Exercise-2
What will be the output of the following
code?
int n = -29;
int temp = (n<0)?-n:n;
cout<<temp;
Class Exercise-3
What will be the output of the following code?
int x=21, y=31, z=44;
if( ( x>16) && (y>x) || (z%2==0) )
cout<<“Hello“;
else
cout<<“World!“;
Class Exercise-4
What will be the output of the following code?
int a = 5, b = 30;
if(a > b)
if(b > 0)
a = a + 5;
else
if(b >= 30)
b = b * 10;
cout<<”a=”<<a<<” ”<<”, b=”<<b;
Class Exercise-5
What will be the output of the following code?
int x = 5, y = 30;
if(y/x > 2)
if(y % x !=2)
x = x + 2;
cout<<x<<”\n“<<y;