You are on page 1of 41

Computing Fundamentals

Dr. Muhammad Yousaf Hamza


Deputy Chief Engineer, PIEAS
The scanf statement
int number, check;
scanf ("%d",&number);
check= number;
//Correct

int number, check;


check= scanf ("%d",&number);
/*The program may run without error.
However, on printing the value of check, it
would not be same as number */

Dr. Muhammad Yousaf Hamza


The scanf statement
#include <stdio.h>
int main(void)
{
int x, y, z;
x = scanf("%d %d", &y, &z);
printf("%d", x);
getchar();
return 0;
}

Ans: 2
// x will tell how many values scanned.

Dr. Muhammad Yousaf Hamza


#include<stdio.h>
int main()
{
char x = 'h', y;
printf("Character x is %c\n", x); // note the use of %c
printf("Please type a character and then press enter\n");

y = getchar(); // It is valid. It will give correct output.

printf("Character y is %c\n", y); // note the use of %c


getchar(); return 0; }

Note:
y = scanf("%c",&y); is not correct, as discussed in the int (or
float) case, previously. Though, it will run but will give you
incorrect output when you print the value of y.

Dr. Yousaf, PIEAS


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


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
Relational Operators

Dr. Yousaf, PIEAS


Relational Operators
Relational operators allow you to compare
variables.
– They return a 1 value for true and a 0 for false.
Operator Symbol Example
Greater than > x > y
Less than < x< y
Greater than/equals >= x >= y
Less than/equals <= x <= y
Equals == x == y
Not equal != x != y NOT x = y

Dr. Yousaf, PIEAS


Relational Operators
Examples
• 5 > 4 is TRUE, 3 > 4 is FALSE
• 4 < 5 is TRUE, 4 < 4 is FALSE
• 4 >= 4 is TRUE, 3 >= 4 is FALSE
• 3 <= 4 is TRUE, 5 <= 4 is FALSE
• 5 == 5 is TRUE, 3==6 is FALSE
• 5!= 4 is TRUE, 5!=5 is FALSE

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


if else 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, Fee is Rupees
200/=\n");
else if(age < 60 )
printf("Please go to Medical Specialist in Room 15, Fee is
Rupees 400/=\n");
else
printf("Please go to Medical Specialist in Room 19, Fee is
Rupees 200/=\n");
getchar();
return 0;
}
Dr. Yousaf, PIEAS
The if/else Selection Structure
– Pseudocode for a nested if/else structure
If student’s grade is greater than or equal to 90
Print “A”
else
If student’s grade is greater than or equal to 80
Print “B”
else
If student’s grade is greater than or equal to 70
Print “C”
else
If student’s grade is greater than or equal to 60
Print “D”
else
Print “F”
Dr. Yousaf, PIEAS
The if else if example
#include <stdio.h>
int main ()
{
int x,y;
printf ("\nInput an integer value for x: ");
scanf ("%d", &x);
printf ("\nInput an integer value for y: ");
scanf ("%d",&y);
if (x==y)
printf ("x is equal to y\n");
else if (x > y)
printf ("x is greater than y\n");
else
printf ("x is smaller than y\n");
getchar(); return 0;
}
Dr. Yousaf, PIEAS
The if else examples
(1) Write a complete and efficient C program that would get
an integer from the user, if the number is greater than zero
print message “Positive” else if number is zero, print “Zero”
else print “Negative”.

(2) Get two integer numbers a and b from the user, if a is


greater than b print message “ A is greater”, if b is greater
print “B is greater”.

(3) Write a program that gets three numbers, a, b and c from


the user, the program should find and display the greatest of
the three numbers.

Dr. Yousaf, PIEAS


Logical Operators

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
Logical Operators

• && AND

• || OR

• ! NOT

Dr. Yousaf, PIEAS


Logical Operators
• && ( logical AND )
– Returns true if both conditions are true
• || ( logical OR )
– Returns true if either of its conditions are true
• ! ( logical NOT, logical negation )
– Reverses the truth/falsity of its condition
– Unary operator, has one operand
• Useful as conditions in loops
Expression Result
true && false false
true || false true
!false true
Dr. Yousaf, PIEAS
Logical Operators
Examples
• If c=5 and d=2 then,((c==5) && (d>5)) returns false.

• If c=5 and d=2 then, ((c==5) || (d>5)) returns true.

• If c=5 then, !(c==5) returns false.

Dr. Yousaf, PIEAS


Be careful about Equality (==) and
Assignment (=) Operators
• Dangerous error
– Does not ordinarily cause syntax errors
if (x == 4 )
printf( “You are happy\n" );
• Checks value of x, if it is 4 then it prints You are happy
– Example, replacing == with =:
if ( x = 4 )
printf( “You are happy\n" );
• This always prints You are happy
• 4 is nonzero, so expression is always true.
• Logic error, not a syntax error
if ( x = 0 )
printf( “You are happy\n" );
What’s output?

Dr. Yousaf, PIEAS


Logical Operators
More Examples
Statement Expression

A and B are positive numbers A>0 && B>0

B and C are non zero numbers B!=0 && C!=0

B is an odd number Try It

A and B are greater than 20 or Try It


C and D are less than 100

Dr. Yousaf, PIEAS


Operator Precedence
Operator Precedence level
() 1
~, ++, --, unary - 2
*, /, % 3
+, - 4
<<, >> 5
<, <=, >, >= 6
==, != 7
& 8
^ 9
| 10
&& 11
|| 12
=, +=, -=, etc. 14

Dr. Yousaf, PIEAS

You might also like