You are on page 1of 22

Computing Fundamentals

Dr. Muhammad Yousaf Hamza


More types: const
const means a variable which doesn't vary – useful for
physical constants or things like pi or e
– You can also declare variables as being constants
– Use the const qualifier:
const double pi=3.1415926;
const long double e = 2.718281828;
const int maxlength=2356; #include<stdio.h>
const int val=(3*7+6)*5; int main()
•(scientific) notation {
(mantissa/exponent) const float n = 6.18e2;
const double n = 6.18e2; printf("%f",n); 618.00
getchar();
return 0;
• 6.18e2 = 6.18x10^2 }

Dr. Muhammad Yousaf Hamza


More types: const

Constants
– Constants are useful for a number of reasons
• Tells the reader of the code that a value does not
change
• Tells the compiler that a value does not change
– The compiler can potentially compile faster code

• Use constants where appropriate

Dr. Muhammad Yousaf Hamza


More types: const
#include<stdio.h>
int main()
{

const double pi=3.1415926;


float radius = 4.5,circum ;

circum = 2*pi*radius;

printf("\n%f", circum); // 28.274334

getchar();
return 0;
}

Dr. Muhammad Yousaf Hamza


More types: const
#include<stdio.h>
int main()
{
const double pi=3.1415926;
float radius = 4.5,circum ;
circum = 2*pi*radius;
printf("\n%f", circum);

radius = 7.3;
pi = 2.9; // Error
circum = 2*pi*radius;
printf("\n%f", circum);
getchar();
return 0;
} Dr. Muhammad Yousaf Hamza
switch statement

Dr. Yousaf, PIEAS


#include <stdio.h>
int main()
{
int day;
printf("The day number 1 means Monday\n");
printf("Please enter the number of day. \n The number must be any
integer value from 1 to 7\n");
scanf("%d",&day);
if (day == 1)
printf("Monday\n");
else if (day == 2)
printf("Tuesday\n");
else if (day == 3)
printf("Wednesday\n");
// You may complete it yourself

Dr. Yousaf, PIEAS


The if/else Selection Structure
Nested if/else structures
• In previous example, the nested if/else structure is to
be used.
• Test for multiple cases by placing if/else selection
structures inside if/else selection structures
• Deep indentation usually not used in practice
• How can we solve this example more conveniently?

• Switch statement is a convenient way to code it.

Dr. Yousaf, PIEAS


Switch Statement
If you have a large decision tree, and all
the decisions depend on the value of the
same variable, you will probably want to
consider a switch statement instead of a
ladder of if...else or else if constructions.

Dr. Yousaf, PIEAS


// Example of switch statement
#include <stdio.h>
int main()
{
int day;
printf("The day number 1 means Monday\n");
printf("Please enter the number of day. \n
The number must be any integer value from
1 to 7\n");
scanf("%d",&day);
// Contd. (next page)

Dr. Yousaf, PIEAS


switch(day)
{
case 1: // 1 is one of possible value of day and so on.
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
// please write cases 3 to 6 yourself
case 7:
printf("Sunday\n");
break;
default:
printf(“You eneterd a wtong number. The number must be an
integer value from 1 to 7\n");
break;
}
getchar(); return 0;
} Dr. Yousaf, PIEAS
• switch
– Useful when a variable or expression is tested for
all the values which can happen and different
actions are taken
• Format
– Series of case labels and an optional default case
switch ( value )
{
case 1:
actions
case 2:
actions
default:
actions
}
– break; exits from structure
Dr. Yousaf, PIEAS
The switch Structure
• Flowchart of the switch structure
true case a break
case a
action(s)
false
true case b break
case b
false action(s)

.
.
.
true case z break
case z
false action(s)

default
action(s)
Dr. Yousaf, PIEAS
Switch Statement
• The expression used in a switch statement must have an integral or
character type.
• You can have any number of case statements within a switch. Each case
is followed by the value to be compared and a colon.
• The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a constant or a literal.
• When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached.
• When a break statement is reached, the switch terminates, and the flow
of control jumps to the next line following the switch statement.
• Not every case needs to contain a break. If no break appears, the flow of
control will fall through to subsequent cases until a break is reached.
• A switch statement can have an optional default case, which appears at
the end of the switch. The default case can be used for performing a task
when none of the cases is true. No break is needed in the default case.

Dr. Muhammad Yousaf Hamza


Mathematical Functions

Dr. Yousaf, PIEAS


#include<stdio.h>
#include<math.h> // use of math.h
#define PI 3.14
int main()
{
float y;

y = sin(PI/2.0); // argument is in radian

printf("%f", y);
getchar();
return 0;
}
Output: 1.000000
Dr. Yousaf, PIEAS
// If given theta is in degree, then convert it first into radians.
#include<stdio.h>
#include<math.h> // use of math.h
#define PI 3.14
int main()
{
float y;
float theta_deg, theta_rad;
theta_deg = 90.0;
theta_rad = (PI/180)*theta_deg;
y = sin(theta_rad); // argument is in radian
printf("%f", y);
getchar(); return 0; }
Output: 1.000000

Dr. Yousaf, PIEAS


Dr. Yousaf, PIEAS
• log() ……..> natural log
• log10() …………> base-10 logarithm
Dr. Yousaf, PIEAS
Dr. Yousaf, PIEAS
#include<stdio.h> k = floor(15.2);
#include<math.h>
printf(" k = %d\n",k);
#define PI 3.14 // output: 15
j = ceil (15.2);
int main()
{ printf(" j = %d\n",j);
// output: 16
int k, j, r;
double x = 25.0, y, z; r = abs (-67);
printf(" r = %d\n",r);
y = sqrt(x); // output: 67
printf("y = %lf\n", y); // y = 5.000000 getchar();
return 0;
z = pow(y,3); // y^3, z = 125.000000
}
printf(" z = %lf\n", z);

Dr. Yousaf, PIEAS


Dr. Yousaf, PIEAS

You might also like