SlideShare a Scribd company logo
1 of 42
Download to read offline
Computing Fundamentals
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 10n");
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
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
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
#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
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 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
if else statements
Dr. Yousaf, PIEAS
#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
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 10nn");
else
printf("Please go to Med. Spec. in Room 15n");
– Note spacing/indentation conventions
Dr. Yousaf, PIEAS
The if/else Selection Structure
Flow chart of the if/else selection
structure
truefalse
print “15” print “10”
age < = 12
Dr. Yousaf, PIEAS
#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
if else if statements
Dr. Yousaf, PIEAS
#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
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 yn");
else if (x > y)
printf ("x is greater than yn");
else
printf ("x is smaller than yn");
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
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 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
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
Logical Operators
Dr. Yousaf, PIEAS
#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
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
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.
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
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
Operator Precedence
Operator Precedence level
( ) 1
~, ++, --, unary - 2
*, /, % 3
+, - 4
<<, >> 5
<, <=, >, >= 6
==, != 7
& 8
^ 9
| 10
&& 11
|| 12
=, +=, -=, etc. 14
Dr. Yousaf, PIEAS
Conditional Operator
Dr. Yousaf, PIEAS
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
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
• 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
• Example:
z = (x > y) ? x : y;
• This is equivalent to:
if (x > y)
z = x;
else
z = y;
Dr. Yousaf, PIEAS
Conditional Operator

More Related Content

Similar to C Language Lecture 4

CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
Sanjjaayyy
 
Which if statement below tests if letter holds R (letter is a char .pdf
Which if statement below tests if letter holds R (letter is a char .pdfWhich if statement below tests if letter holds R (letter is a char .pdf
Which if statement below tests if letter holds R (letter is a char .pdf
aniarihant
 

Similar to C Language Lecture 4 (20)

C++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPEC++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPE
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
C Language Lecture 19
C Language Lecture 19C Language Lecture 19
C Language Lecture 19
 
C Language Lecture 17
C Language Lecture 17C Language Lecture 17
C Language Lecture 17
 
C Language Lecture 3
C Language Lecture  3C Language Lecture  3
C Language Lecture 3
 
Fcp chapter 2 upto_if_else (2)
Fcp chapter 2 upto_if_else (2)Fcp chapter 2 upto_if_else (2)
Fcp chapter 2 upto_if_else (2)
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
 
Selection & Making Decisions in c
Selection & Making Decisions in cSelection & Making Decisions in c
Selection & Making Decisions in c
 
Computer Programming - if Statements & Relational Operators
Computer Programming - if Statements  & Relational OperatorsComputer Programming - if Statements  & Relational Operators
Computer Programming - if Statements & Relational Operators
 
C Language Lecture 9
C Language Lecture 9C Language Lecture 9
C Language Lecture 9
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
 
control statement
control statement control statement
control statement
 
ICP - Lecture 7 and 8
ICP - Lecture 7 and 8ICP - Lecture 7 and 8
ICP - Lecture 7 and 8
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 
C programming
C programmingC programming
C programming
 
Bsit1
Bsit1Bsit1
Bsit1
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
Ch4
Ch4Ch4
Ch4
 
Which if statement below tests if letter holds R (letter is a char .pdf
Which if statement below tests if letter holds R (letter is a char .pdfWhich if statement below tests if letter holds R (letter is a char .pdf
Which if statement below tests if letter holds R (letter is a char .pdf
 

More from Shahzaib Ajmal (12)

C Language Lecture 22
C Language Lecture 22C Language Lecture 22
C Language Lecture 22
 
C Language Lecture 21
C Language Lecture 21C Language Lecture 21
C Language Lecture 21
 
C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
 
C Language Lecture 16
C Language Lecture 16C Language Lecture 16
C Language Lecture 16
 
C Language Lecture 15
C Language Lecture 15C Language Lecture 15
C Language Lecture 15
 
C Language Lecture 14
C Language Lecture 14C Language Lecture 14
C Language Lecture 14
 
C Language Lecture 13
C Language Lecture 13C Language Lecture 13
C Language Lecture 13
 
C Language Lecture 12
C Language Lecture 12C Language Lecture 12
C Language Lecture 12
 
C Language Lecture 11
C Language Lecture  11C Language Lecture  11
C Language Lecture 11
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
 
C Language Lecture 2
C Language Lecture  2C Language Lecture  2
C Language Lecture 2
 
C Language Lecture 1
C Language Lecture  1C Language Lecture  1
C Language Lecture 1
 

Recently uploaded

會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 

Recently uploaded (20)

Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 

C Language Lecture 4

  • 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
  • 16. if else statements 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
  • 22. if else if statements 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
  • 32. Logical Operators • && AND • || OR • ! NOT 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
  • 37. Operator Precedence Operator Precedence level ( ) 1 ~, ++, --, unary - 2 *, /, % 3 +, - 4 <<, >> 5 <, <=, >, >= 6 ==, != 7 & 8 ^ 9 | 10 && 11 || 12 =, +=, -=, etc. 14 Dr. Yousaf, PIEAS
  • 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