You are on page 1of 42

Computing Fundamentals

Dr. Muhammad Yousaf Hamza


Increment and Decrement Operators

Dr. Muhammad Yousaf Hamza


Increment and Decrement
• Increment and decrement operators.
– Increment: ++ It increases the value by 1
i = 7; // i is a variable name
++i; // (i = i + 1 or i + = 1). It increases the value of i by 1
i = 7;
i++;

–Decrement: -- (similar to ++) It decreases the value by 1


i = 8;
--i; // --i is the same as : (i = i – 1 or i - = 1).
i = 8;
i--;

Dr. Muhammad Yousaf Hamza


Increment and Decrement
Pre-fix and Post-fix
• ++i means increment i then use it
• i++ means use i then increment it
int i= 6;
printf ("%d\n",i++); /* Prints 6 sets i to 7 */

Note this important difference


int i= 6;
printf ("%d\n",++i); /* prints 7 and sets i to 7 */

All of the above also applies to --.

Dr. Muhammad Yousaf Hamza


Increment and Decrement
#include<stdio.h>
Pre-fix and Post-fix
int main()
{
int a = 7, b = 20, c, d;
c = a++;
printf("%d", c);
printf("\n%d",a);

d = ++b;
printf("\n%d", d);
printf("\n%d",b);

getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
Data Types

Dr. Muhammad Yousaf Hamza


Data Types in C
• We must declare the type of every variable we
use in C.
• Every variable has a type (e.g. int) and a name
(e.g. no_students), i.e. int no_students
• Basic data types in C
– char: a single byte, capable of holding one
character
– int: an integer of fixed length, typically reflecting
the natural size of integers on the host
machine (i.e., 32 or 64 bits)
– float: single-precision floating point
– double: double precision floating point
Dr. Muhammad Yousaf Hamza
Data Types in C
• Floating-point variables represent numbers
with a decimal place—like 9.3, 3.1415927,
0.0000625, and –10.2.
• They have both an integer part to the left of
the decimal point, and a fractional part to the
right.
• Floating-point variables represent what
mathematicians call real numbers.

Dr. Muhammad Yousaf Hamza


Conversion Specifiers

#include <stdio.h>
int main( )
{
int x = 5;
printf(“\n x is %d", x); // %d is format specifier
getchar();
return 0;
}
Format specifiers are used in printf function for printing
numbers and characters. A format specifier acts like a place
holder, it reserves a place in a string for numbers and
characters.
Dr. Muhammad Yousaf Hamza
Conversion Specifiers
Specifier Meaning
%c Single character
%d Decimal integer
%f Decimal floating point number
%lf Decimal floating point number (double)
There must be one conversion specifier for each argument
being printed out.
• Ensure you use the correct specifier for the type of data you are
printing.
• Format specifiers are used in printf function for printing
numbers and characters. A format specifier acts like a place
holder, it reserves a place in a string for numbers and
characters.

Dr. Muhammad Yousaf Hamza


Variable Declaration
• Generic Form
typename varname1, varname2, ...;

• Examples:
int count, x, y, z;
float a, b, m;
double percent, total, average;

Dr. Muhammad Yousaf Hamza


Variable Declaration
Initialization
• ALWAYS initialize a variable before using it
– Failure to do so in C is asking for trouble
– The value of an uninitialized variables is undefined in
the C standards
• Examples:
int count; /* Set aside storage space for count */
count = 0; /* Store 0 in count */
• This can be done at definition:
int count = 0;
double percent = 10.0, rate = 0.56;

Dr. Muhammad Yousaf Hamza


Example
#include <stdio.h>
int main ()
{
double radius, area;
printf ("Enter the value of radius ");
scanf ( "%lf", &radius);

area = 3.14159 * radius * radius;


printf ("\nArea = %lf\n\n", area);

getchar();
return 0;
} Dr. Muhammad Yousaf Hamza
Reading Numeric Data with scanf
– For example:
int n1, n2,x;
float f, rate;
scanf ("%d",&x); /*reads a decimal integer */
scanf ("%f",&rate); /*reads a floating point value*/
scanf("%d%d%f",&n1,&n2,&f);
• Use white spaces to separate numbers when input.
5 10 20.3 then press Enter Key
OR, you can also enter these values one by one by
pressing enter Key. Both are OK.

Dr. Muhammad Yousaf Hamza


Type Conversion
#include<stdio.h>
int main()
{
int a= 23, b= 4;
float c;
c = a/b; // 5.00
printf("\n%f",c); // 5.00
getchar();
return 0;
}
Actually 23/4 = 5.75, but here output is 5.00 In order to have
correct answer (with decimal value), we need type casting.
Dr. Muhammad Yousaf Hamza
Type Conversion
• C allows for conversions between the basic types, implicitly or
explicitly. It is also called casting.
• A cast is a way of telling one variable type to temporarily look
like another.
• Explicit conversion uses the cast operator.
• Example :
int x=10;
float y, z=3.14;
y=(float) x; /* y=10.0 */
x=(int) z; /* x=3 */
x=(int) (-z); /* x=-3 */

Dr. Muhammad Yousaf Hamza


Casting of Variables
By using (type) in front of a variable we tell
the variable to act like another type of variable.
We can cast between any type usually. However,
the only reason to cast is to stop
ints being rounded by division.

Dr. Muhammad Yousaf Hamza


Casting of Variables
#include<stdio.h>
int main()
{
int a= 23, b= 4;
float c;
c = a/b;
printf("\n%f",c); // 5.00
Cast ints a and b to be float
c= (float)a/(float)b;
// c = (float)a/b; or c= a/(float)b; are also same.

printf("\n%f",c); // 5.75
getchar();
return 0;
}
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;
onst 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
Decisions

Dr. Muhammad Yousaf Hamza


To decide even/odd
#include<stdio.h>
int main()
{
int num;
printf("Please enter an integer number:\n");
scanf("%d",&num);

if(num%2==0)
printf("\nThe number %d is an even number",num);

if(num%2!=0)
printf("\nThe number %d is an odd number",num);
getchar(); getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
To decide even/odd
#include<stdio.h>
int main()
{
int num;
printf("Please enter an integer number:\n");
scanf("%d",&num);

if(num%2==0) // Here only one check.


printf("\nThe number %d is an even number",num);

else
printf("\nThe number %d is an odd number",num);
getchar(); getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
Control Statements

Dr. Yousaf, PIEAS


Conditional Tasks
• if it is the condition, then I will do task A
Real Life Examples:
• if it is the condition, then I will do task A, else
(i.e. otherwise), I will do task B.
Real Life Examples:
• if it is the condition, then I will do task A,
else if it is the condition then I will do task B,
else I will do task C.
Real Life Examples:
Dr. Yousaf, PIEAS
Sequential execution
• Sequential execution
– Statements executed one after the other in the
order written
– Sequence structures: Built into C.
Programs executed sequentially by default

Dr. Yousaf, PIEAS


Control Structures
Transfer of control
– When the next statement executed is not the next
one in sequence

• Selection structures: C has three types:


if, if/else, and switch

• Repetition structures: C has three types:


for, while, and do/while (Later)

Dr. Yousaf, PIEAS


if statements

Dr. Yousaf, PIEAS


#include<stdio.h>
int main()
{
int age;
printf("Please enter your age in years:\n");
scanf("%d",&age);
if(age<=12)
printf("Please go to Child Specialist in Room 10\n");
printf(“Allah Hafiz");
getchar();
return 0;
}

Dr. Yousaf, PIEAS


The if Selection Structure
Selection structure:
• Used to choose among alternative courses of action
if(age<=12)
printf("Please go to Child Specialist in Room
10\n");
• If condition is true Print statement is executed and
program goes on to next statement
• If false, print statement is ignored and the program
goes onto the next statement
• Indenting makes programs easier to read

Dr. Yousaf, PIEAS


The if Selection Structure
if structure is a single-entry/single-exit
structure Diamond symbol
(decision symbol)
Indicates decision is
to be made contains
Print “Please go to an expression that
true Child Specialist in can be true or
age <= 12 false
Room 10”
Test the condition,
false
follow appropriate
path
Print “Allah Hafiz”

Dr. Yousaf, PIEAS


#include<stdio.h>
int main()
{
int age;
printf("Please enter your age in years\n");
scanf("%d",&age);
if(age<=12)
{
printf("Please go to Child Specialist in Room 10\n");
printf(“ Fee is Rupees 400/=\n");
} // Note the use of braces
printf(“Allah Hafiz");
getchar();
return 0;
}

Dr. Yousaf, PIEAS


The if Statement
• Form 1:
if (expression)
statement1;
next statement;

Dr. Yousaf, PIEAS


#include<stdio.h>
int main()
{
int age;
printf("Please enter your age in years\n");
scanf("%d",&age);
if(age<=12)
printf("Please go to Pediatrics in Room 10\n\n");
if(age>12)
printf("Please go to Medical Specialist in Room 15\n");
printf(“Allah Hafiz");
getchar();
return 0;
}

Dr. Yousaf, PIEAS


if else statements

Dr. Yousaf, PIEAS


#include<stdio.h>
int main()
{
int age;
printf("Please enter your age in years\n");
scanf("%d",&age);

if (age<=12)
printf("Please go to Child Specialist in Room 10\n\n");
else
printf("Please go to Medical Specialist in Room 15\n");

printf(“Allah Hafiz");
getchar();
return 0;
}
Dr. Yousaf, PIEAS
The if Statement
• Form 1:
if (expression)
statement1;
next statement;
• Form 2:
if (expression)
statement1;
else
statement2;

next statement;

Dr. Yousaf, PIEAS


The if/else Selection Structure
• if
– Only performs an action if the condition is true
• if/else
– Specifies an action to be performed both when the
condition is true and when it is false
• Once again
if (age<=12)
printf("Please go to Pediatrics in Room 10\n\n");
else
printf("Please go to Med. Spec. in Room 15\n");

– Note spacing/indentation conventions

Dr. Yousaf, PIEAS


The if/else Selection Structure
Flow chart of the if/else selection
structure

false true
age < = 12
print “15” print “10”

Dr. Yousaf, PIEAS


#include<stdio.h>
int main()
{
int age;
printf("Please enter your age in years\n");
scanf("%d",&age);
if (age<=12)
printf("Please go to Child Specialist in Room 10, Fee is
Rupees 400/=\n“);
if(age > 12 && age < 60 )
printf("Please go to Medical Specialist in Room 15, Fee is
Rupees 400/=\n");
if(age >= 60)
printf("Please go to Medical Specialist in Room 19, Fee is
Rupees 200/=\n");
getchar();
return 0; }

Dr. Yousaf, PIEAS

You might also like