3. 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
4. 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
6. 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
7. 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
8. 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
10. #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 10n");
printf(“Allah Hafiz");
getchar();
return 0;
}
Dr. Yousaf, PIEAS
11. 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
10n");
• 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
12. The if Selection Structure
if structure is a single-entry/single-exit
structure
true
false
age <= 12
Diamond symbol
(decision symbol)
Indicates decision is
to be made contains
an expression that
can be true or
false
Test the condition,
follow appropriate
path
Print “Please go to
Child Specialist in
Room 10”
Print “Allah Hafiz”
Dr. Yousaf, PIEAS
13. #include<stdio.h>
int main()
{
int age;
printf("Please enter your age in yearsn");
scanf("%d",&age);
if(age<=12)
{
printf("Please go to Child Specialist in Room 10n");
printf(“ Fee is Rupees 400/=n");
} // Note the use of braces
printf(“Allah Hafiz");
getchar();
return 0;
}
Dr. Yousaf, PIEAS
14. The if Statement
• Form 1:
if (expression)
statement1;
next statement;
Dr. Yousaf, PIEAS
15. #include<stdio.h>
int main()
{
int age;
printf("Please enter your age in yearsn");
scanf("%d",&age);
if(age<=12)
printf("Please go to Pediatrics in Room 10nn");
if(age>12)
printf("Please go to Medical Specialist in Room 15n");
printf(“Allah Hafiz");
getchar();
return 0;
}
Dr. Yousaf, PIEAS
17. #include<stdio.h>
int main()
{
int age;
printf("Please enter your age in yearsn");
scanf("%d",&age);
if (age<=12)
printf("Please go to Child Specialist in Room 10nn");
else
printf("Please go to Medical Specialist in Room 15n");
printf(“Allah Hafiz");
getchar();
return 0;
}
Dr. Yousaf, PIEAS
18. The if Statement
• Form 1:
if (expression)
statement1;
next statement;
• Form 2:
if (expression)
statement1;
else
statement2;
next statement;
Dr. Yousaf, PIEAS
19. 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 10nn");
else
printf("Please go to Med. Spec. in Room 15n");
– Note spacing/indentation conventions
Dr. Yousaf, PIEAS
20. The if/else Selection Structure
Flow chart of the if/else selection
structure
truefalse
print “15” print “10”
age < = 12
Dr. Yousaf, PIEAS
21. #include<stdio.h>
int main()
{
int age;
printf("Please enter your age in yearsn");
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
23. #include<stdio.h>
int main()
{
int age;
printf("Please enter your age in yearsn");
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
24. 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
25. 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 yn");
else if (x > y)
printf ("x is greater than yn");
else
printf ("x is smaller than yn");
getchar(); return 0;
}
Dr. Yousaf, PIEAS
26. 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
28. Relational Operators
Relational operators allow you to compare
variables.
– They return a 1 value for true and a 0 for false.
Operator SymbolExample
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
29. 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
31. #include<stdio.h>
int main()
{
int age;
printf("Please enter your age in yearsn");
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
33. 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
34. Logical Operators
Examples
Dr. Yousaf, PIEAS
• 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.
35. Be careful about Equality (==) and
Assignment (=) Operators
• Dangerous error
– Does not ordinarily cause syntax errors
if (x == 4 )
printf( “You are happyn" );
• Checks value of x, if it is 4 then it prints You are happy
– Example, replacing == with =:
if ( x = 4 )
printf( “You are happyn" );
• 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 happyn" );
What’s output?
Dr. Yousaf, PIEAS
36. Logical Operators
More Examples
Dr. Yousaf, PIEAS
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
C and D are less than 100
Try It
39. Conditional Operator
if ( a < b)
c = a + 5;
else
c = b + 8;
// We can do this in compact form as
c = a < b ? a + 5 : b + 8; //Ternary conditional operator (?:)
Evaluate first expression. If true, evaluate second,
otherwise evaluate third.
Dr. Yousaf, PIEAS
40. Conditional Operator
• Ternary conditional operator (?:)
– Takes three arguments (condition, value if true,
value if false)
– Our pseudocode could be written:
grade >= 60 ? printf( “Passedn” ) :
printf( “Failedn” );
Dr. Yousaf, PIEAS
41. • The conditional operator essentially allows you to embed an
“if” statement into an expression
• Generic Form
exp1 ? exp2 : exp3 if exp1 is true
value is exp2
(exp3 is not evaluated)
if exp1 is false,
value is exp3
(exp2 is not evaluated)
Dr. Yousaf, PIEAS
Conditional Operator
42. • Example:
z = (x > y) ? x : y;
• This is equivalent to:
if (x > y)
z = x;
else
z = y;
Dr. Yousaf, PIEAS
Conditional Operator