SlideShare a Scribd company logo
CONTROL STRUCTURE OF
C - LANGUAGE
LUIMM
COMPUTER FUNDAMENTAL & PROGRAMMING
Lesson3
TABLE OF CONTENT
If statement
If else if statement
Nested If structure
COMPUTER FUNDAMENTAL & PROGRAMMING
Switch Case structure
If else statement
LEARNING OUTCOME
1
❖ Understand how to use logical expressions to control
program flow.
❖ Learn to write and interpret simple and nested if-else
statements.
❖ Develop an appreciation for code efficiency and
optimization when using control structures.
❖ Apply control statements to practical programming
scenarios.
❖ Understand how control structures are used in real-
world applications to control program behavior.
COMPUTER FUNDAMENTAL & PROGRAMMING
2
Control structures allow to change the ordering of how the statements in a program is
executed
Control Structure
COMPUTER FUNDAMENTAL & PROGRAMMING
Types of control structures:
❖Decision control structures
allow to select specific sections of code to be executed
❖Repetition control structures
allow to execute specific sections of the code a number of times
3
If else statement
COMPUTER FUNDAMENTAL & PROGRAMMING
C language statements that allows a programmer to select and execute specific
blocks of code while skipping other sections.
Include types such as:
• If-Else
• If-Else-If
• Switch structure
if – else Statement
4
COMPUTER FUNDAMENTAL & PROGRAMMING
"The if-else statement is used to
express decisions” -Dennis Ritchie.
In a simple if-else conditional statement,
there are two given choices the computer
could make.
However, the computer could only choose one
of them. It is like having two girlfriends, you
could only choose one to marry (if you are a
true Christian, not a fake one). The formal
syntax of if-else is:
if (condition)
statement;
else
statement;
Example
5
COMPUTER FUNDAMENTAL & PROGRAMMING
1. Write a program that determines if the input age
is qualified to vote or not. The qualifying age is 18
years old and above.
#include<stdio.h>
int main()
{
int age;
printf(“nEnter Age : ");
scanf("%i",&age);
if(age>=18)
printf("%i is Qualify age to vote",age);
else
printf("%i is NOT Qualify age to vote",age);
return 0;
}
Example
6
COMPUTER FUNDAMENTAL & PROGRAMMING
2. Write a program that determines if the input
number is POSITIVE or NEGATIVE. Consider 0 as
positive (considering that it contains no negative sign).
#include<stdio.h>
int main()
{
int number;
printf("n Enter number : ");
scanf("%i",&number);
if (number>=0)
printf("n %i is a Positive Number",number);
else
printf("n %i is a Positive Number",number);
return 0;
}
Example
7
COMPUTER FUNDAMENTAL & PROGRAMMING
3. Write program that will determine if the student is
passed or failed.
#include<stdio.h>
int main()
{
int grade;
printf("nn GRADE : ");
scanf("%i",&grade);
if(grade>=75)
printf("n ***** PASSED *****");
else
printf("n ***** FAILED *****");
return 0;
}
if Statement
8
COMPUTER FUNDAMENTAL & PROGRAMMING
if (condition)
statement;
if (condition)
statement;
if (condition)
statement;
if (condition)
statement
Example
9
COMPUTER FUNDAMENTAL & PROGRAMMING
#include <stdio.h>
int main()
{
int a;
printf("Enter number ");
scanf("%i",&a);
if (a==1)
printf("one");
if (a==2)
printf("TWO");
if (a==3)
printf("Three");
return 0;
}
if Compound
Statement
10
COMPUTER FUNDAMENTAL & PROGRAMMING
As in the if statement, compound statements may also be
used in if-else statements (provided they are grouped
together by braces).
if (expression)
{
statement1;
statement2;
…
}
else
{
statement3;
statement4;
…
}
If there is a need to
execute several statements
(compound statement),
then left and right {} braces
can group these
statements.
Syntax
11
COMPUTER FUNDAMENTAL & PROGRAMMING
If there is a need to
execute several statements
(compound statement),
then left and right {} braces
can group these
statements.
Every time C language encounters an if statement,
1. It determines whether expression is true or false.
2. If expression is true, then C language executes
statement.
3. If expression is false, then C language ignores or skips
the remainder of the if statement and proceeds to
the next statement.
if Compound
Statement
if – else if Statement
12
COMPUTER FUNDAMENTAL & PROGRAMMING
user can decide among multiple options.
The if statements are executed from the
top down. As soon as one of the
conditions controlling the if is true, the
statement associated with that if is
executed, and the rest of the ladder is
bypassed. If none of the conditions is
true, then the final else statement will be
executed.
if (condition)
statement;
else if (condition)
statement; . .
else statement;
if structure
13
COMPUTER FUNDAMENTAL & PROGRAMMING
If there is a need to
execute several statements
(compound statement),
then left and right {} braces
can group these
statements.
if (condition)
{
statement1;
statement2;
…
}
else if (condition)
{
statement1;
statement2;
…
}
else
{
statement1;
statement2;
}
Example
14
COMPUTER FUNDAMENTAL & PROGRAMMING 2. Write a program that determines if the input number is
POSITIVE, NEGATIVE or ZERO.
#include <stdio.h>
int main() {
int number;
// Input a number from the user
printf("Enter a number: ");
scanf("%d", &number);
// Check and display if the number is positive, negative, or zero
if (number > 0)
{ printf(“%d”,number);
printf("The number is positive.n");
}
else if (number < 0)
{ printf(“%d”,number);
printf("The number is negative.n"); }
else {
printf(“%d”,number); printf("The number is zero.n");
}
return 0;
}
Example
15
COMPUTER FUNDAMENTAL & PROGRAMMING
3. Write program that will determine if the
student is passed or failed.
#include<stdio.h>
int main()
{
int n1,n2,choice;
float age;
printf("Enter Age : ");
scanf("%f",&age);
if(age<16)
{
printf("%.0f not Qualify",age);
}
else if((age>=16) && (age<=17))
{
printf(“You are %.0f, Eligible to vote for SK only",age);
}
else if((age>17) && (age<=25))
printf(“You are %.0f, Eligible to vote for SK & National",age);
else
printf("%.0f You are %.0f, Eligible to vote National only",age);
return 0;
}
QUESTION
OPERATORSOF
C - LANGUAGE

More Related Content

Similar to Lesson 2 C STATEMENT IN PROGRAMMING (M).pdf (20)

Lec16.pptx problem specifications computer
Lec16.pptx problem specifications computerLec16.pptx problem specifications computer
Lec16.pptx problem specifications computer
samiullahamjad06
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
ishaparte4
 
Ch05.pdf
Ch05.pdfCh05.pdf
Ch05.pdf
ShivamChaturvedi67
 
Decision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, StringsDecision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, Strings
Prabu U
 
Lecture 6Decision_Control_OR_Conditional_Control_Structures.ppt
Lecture 6Decision_Control_OR_Conditional_Control_Structures.pptLecture 6Decision_Control_OR_Conditional_Control_Structures.ppt
Lecture 6Decision_Control_OR_Conditional_Control_Structures.ppt
LaxmiVaraprasad1
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
ENGWAU TONNY
 
Lec 10
Lec 10Lec 10
Lec 10
kapil078
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
Dr. Chandrakant Divate
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
nmahi96
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
Mehul Desai
 
3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdf3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdf
ArkSingh7
 
Bsit1
Bsit1Bsit1
Bsit1
jigeno
 
Password protected diary
Password protected diaryPassword protected diary
Password protected diary
SHARDA SHARAN
 
asdasdqwdsadasdsadsadasdqwrrweffscxv wsfrt
asdasdqwdsadasdsadsadasdqwrrweffscxv wsfrtasdasdqwdsadasdsadsadasdqwrrweffscxv wsfrt
asdasdqwdsadasdsadsadasdqwrrweffscxv wsfrt
sadman190214
 
Decision Control Structure If & Else
Decision Control Structure If & ElseDecision Control Structure If & Else
Decision Control Structure If & Else
Abdullah Bhojani
 
Chap7_b Control Statement Looping (3).pptx
Chap7_b Control Statement Looping (3).pptxChap7_b Control Statement Looping (3).pptx
Chap7_b Control Statement Looping (3).pptx
FakhriyArif
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docx
JavvajiVenkat
 
Workbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdfWorkbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdf
DrDineshenScientist
 
Module 2- Control Structures
Module 2- Control StructuresModule 2- Control Structures
Module 2- Control Structures
nikshaikh786
 
Lec16.pptx problem specifications computer
Lec16.pptx problem specifications computerLec16.pptx problem specifications computer
Lec16.pptx problem specifications computer
samiullahamjad06
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
ishaparte4
 
Decision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, StringsDecision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, Strings
Prabu U
 
Lecture 6Decision_Control_OR_Conditional_Control_Structures.ppt
Lecture 6Decision_Control_OR_Conditional_Control_Structures.pptLecture 6Decision_Control_OR_Conditional_Control_Structures.ppt
Lecture 6Decision_Control_OR_Conditional_Control_Structures.ppt
LaxmiVaraprasad1
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
ENGWAU TONNY
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
Dr. Chandrakant Divate
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
nmahi96
 
3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdf3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdf
ArkSingh7
 
Password protected diary
Password protected diaryPassword protected diary
Password protected diary
SHARDA SHARAN
 
asdasdqwdsadasdsadsadasdqwrrweffscxv wsfrt
asdasdqwdsadasdsadsadasdqwrrweffscxv wsfrtasdasdqwdsadasdsadsadasdqwrrweffscxv wsfrt
asdasdqwdsadasdsadsadasdqwrrweffscxv wsfrt
sadman190214
 
Decision Control Structure If & Else
Decision Control Structure If & ElseDecision Control Structure If & Else
Decision Control Structure If & Else
Abdullah Bhojani
 
Chap7_b Control Statement Looping (3).pptx
Chap7_b Control Statement Looping (3).pptxChap7_b Control Statement Looping (3).pptx
Chap7_b Control Statement Looping (3).pptx
FakhriyArif
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docx
JavvajiVenkat
 
Workbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdfWorkbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdf
DrDineshenScientist
 
Module 2- Control Structures
Module 2- Control StructuresModule 2- Control Structures
Module 2- Control Structures
nikshaikh786
 

Recently uploaded (20)

How to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guidesHow to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guides
Celine George
 
Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...
Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...
Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...
wygalkelceqg
 
0b - THE ROMANTIC ERA: FEELINGS AND IDENTITY.pptx
0b - THE ROMANTIC ERA: FEELINGS AND IDENTITY.pptx0b - THE ROMANTIC ERA: FEELINGS AND IDENTITY.pptx
0b - THE ROMANTIC ERA: FEELINGS AND IDENTITY.pptx
Julián Jesús Pérez Fernández
 
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdfপ্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
Pragya - UEM Kolkata Quiz Club
 
Search Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website SuccessSearch Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website Success
Muneeb Rana
 
Writing Research Papers: Guidance for Research Community
Writing Research Papers: Guidance for Research CommunityWriting Research Papers: Guidance for Research Community
Writing Research Papers: Guidance for Research Community
Rishi Bankim Chandra Evening College, Naihati, North 24 Parganas, West Bengal, India
 
How to Setup Renewal of Subscription in Odoo 18
How to Setup Renewal of Subscription in Odoo 18How to Setup Renewal of Subscription in Odoo 18
How to Setup Renewal of Subscription in Odoo 18
Celine George
 
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
Arshad Shaikh
 
Stewart Butler - OECD - How to design and deliver higher technical education ...
Stewart Butler - OECD - How to design and deliver higher technical education ...Stewart Butler - OECD - How to design and deliver higher technical education ...
Stewart Butler - OECD - How to design and deliver higher technical education ...
EduSkills OECD
 
STUDENT LOAN TRUST FUND DEFAULTERS GHANA
STUDENT LOAN TRUST FUND DEFAULTERS GHANASTUDENT LOAN TRUST FUND DEFAULTERS GHANA
STUDENT LOAN TRUST FUND DEFAULTERS GHANA
Kweku Zurek
 
K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910
PankajRodey1
 
Critical Thinking and Bias with Jibi Moses
Critical Thinking and Bias with Jibi MosesCritical Thinking and Bias with Jibi Moses
Critical Thinking and Bias with Jibi Moses
Excellence Foundation for South Sudan
 
How to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time OffHow to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time Off
Celine George
 
LDMMIA Free Reiki Yoga S7 Weekly Workshops
LDMMIA Free Reiki Yoga S7 Weekly WorkshopsLDMMIA Free Reiki Yoga S7 Weekly Workshops
LDMMIA Free Reiki Yoga S7 Weekly Workshops
LDM & Mia eStudios
 
Diana Enriquez Wauconda - A Wauconda-Based Educator
Diana Enriquez Wauconda - A Wauconda-Based EducatorDiana Enriquez Wauconda - A Wauconda-Based Educator
Diana Enriquez Wauconda - A Wauconda-Based Educator
Diana Enriquez Wauconda
 
PHYSIOLOGY & SPORTS INJURY by Diwakar Sir
PHYSIOLOGY & SPORTS INJURY by Diwakar SirPHYSIOLOGY & SPORTS INJURY by Diwakar Sir
PHYSIOLOGY & SPORTS INJURY by Diwakar Sir
Diwakar Kashyap
 
HUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGY
HUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGYHUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGY
HUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGY
DHARMENDRA SAHU
 
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT PatnaSwachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Quiz Club, Indian Institute of Technology, Patna
 
Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..
faizanaltaf231
 
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATIONTHE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
PROF. PAUL ALLIEU KAMARA
 
How to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guidesHow to Setup Lunch in Odoo 18 - Odoo guides
How to Setup Lunch in Odoo 18 - Odoo guides
Celine George
 
Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...
Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...
Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...
wygalkelceqg
 
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdfপ্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
Pragya - UEM Kolkata Quiz Club
 
Search Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website SuccessSearch Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website Success
Muneeb Rana
 
How to Setup Renewal of Subscription in Odoo 18
How to Setup Renewal of Subscription in Odoo 18How to Setup Renewal of Subscription in Odoo 18
How to Setup Renewal of Subscription in Odoo 18
Celine George
 
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
Arshad Shaikh
 
Stewart Butler - OECD - How to design and deliver higher technical education ...
Stewart Butler - OECD - How to design and deliver higher technical education ...Stewart Butler - OECD - How to design and deliver higher technical education ...
Stewart Butler - OECD - How to design and deliver higher technical education ...
EduSkills OECD
 
STUDENT LOAN TRUST FUND DEFAULTERS GHANA
STUDENT LOAN TRUST FUND DEFAULTERS GHANASTUDENT LOAN TRUST FUND DEFAULTERS GHANA
STUDENT LOAN TRUST FUND DEFAULTERS GHANA
Kweku Zurek
 
K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910
PankajRodey1
 
How to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time OffHow to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time Off
Celine George
 
LDMMIA Free Reiki Yoga S7 Weekly Workshops
LDMMIA Free Reiki Yoga S7 Weekly WorkshopsLDMMIA Free Reiki Yoga S7 Weekly Workshops
LDMMIA Free Reiki Yoga S7 Weekly Workshops
LDM & Mia eStudios
 
Diana Enriquez Wauconda - A Wauconda-Based Educator
Diana Enriquez Wauconda - A Wauconda-Based EducatorDiana Enriquez Wauconda - A Wauconda-Based Educator
Diana Enriquez Wauconda - A Wauconda-Based Educator
Diana Enriquez Wauconda
 
PHYSIOLOGY & SPORTS INJURY by Diwakar Sir
PHYSIOLOGY & SPORTS INJURY by Diwakar SirPHYSIOLOGY & SPORTS INJURY by Diwakar Sir
PHYSIOLOGY & SPORTS INJURY by Diwakar Sir
Diwakar Kashyap
 
HUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGY
HUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGYHUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGY
HUMAN SKELETAL SYSTEM ANATAMY AND PHYSIOLOGY
DHARMENDRA SAHU
 
Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..
faizanaltaf231
 
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATIONTHE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
PROF. PAUL ALLIEU KAMARA
 
Ad

Lesson 2 C STATEMENT IN PROGRAMMING (M).pdf

  • 1. CONTROL STRUCTURE OF C - LANGUAGE LUIMM COMPUTER FUNDAMENTAL & PROGRAMMING Lesson3
  • 2. TABLE OF CONTENT If statement If else if statement Nested If structure COMPUTER FUNDAMENTAL & PROGRAMMING Switch Case structure If else statement
  • 3. LEARNING OUTCOME 1 ❖ Understand how to use logical expressions to control program flow. ❖ Learn to write and interpret simple and nested if-else statements. ❖ Develop an appreciation for code efficiency and optimization when using control structures. ❖ Apply control statements to practical programming scenarios. ❖ Understand how control structures are used in real- world applications to control program behavior. COMPUTER FUNDAMENTAL & PROGRAMMING
  • 4. 2 Control structures allow to change the ordering of how the statements in a program is executed Control Structure COMPUTER FUNDAMENTAL & PROGRAMMING Types of control structures: ❖Decision control structures allow to select specific sections of code to be executed ❖Repetition control structures allow to execute specific sections of the code a number of times
  • 5. 3 If else statement COMPUTER FUNDAMENTAL & PROGRAMMING C language statements that allows a programmer to select and execute specific blocks of code while skipping other sections. Include types such as: • If-Else • If-Else-If • Switch structure
  • 6. if – else Statement 4 COMPUTER FUNDAMENTAL & PROGRAMMING "The if-else statement is used to express decisions” -Dennis Ritchie. In a simple if-else conditional statement, there are two given choices the computer could make. However, the computer could only choose one of them. It is like having two girlfriends, you could only choose one to marry (if you are a true Christian, not a fake one). The formal syntax of if-else is: if (condition) statement; else statement;
  • 7. Example 5 COMPUTER FUNDAMENTAL & PROGRAMMING 1. Write a program that determines if the input age is qualified to vote or not. The qualifying age is 18 years old and above. #include<stdio.h> int main() { int age; printf(“nEnter Age : "); scanf("%i",&age); if(age>=18) printf("%i is Qualify age to vote",age); else printf("%i is NOT Qualify age to vote",age); return 0; }
  • 8. Example 6 COMPUTER FUNDAMENTAL & PROGRAMMING 2. Write a program that determines if the input number is POSITIVE or NEGATIVE. Consider 0 as positive (considering that it contains no negative sign). #include<stdio.h> int main() { int number; printf("n Enter number : "); scanf("%i",&number); if (number>=0) printf("n %i is a Positive Number",number); else printf("n %i is a Positive Number",number); return 0; }
  • 9. Example 7 COMPUTER FUNDAMENTAL & PROGRAMMING 3. Write program that will determine if the student is passed or failed. #include<stdio.h> int main() { int grade; printf("nn GRADE : "); scanf("%i",&grade); if(grade>=75) printf("n ***** PASSED *****"); else printf("n ***** FAILED *****"); return 0; }
  • 10. if Statement 8 COMPUTER FUNDAMENTAL & PROGRAMMING if (condition) statement; if (condition) statement; if (condition) statement; if (condition) statement
  • 11. Example 9 COMPUTER FUNDAMENTAL & PROGRAMMING #include <stdio.h> int main() { int a; printf("Enter number "); scanf("%i",&a); if (a==1) printf("one"); if (a==2) printf("TWO"); if (a==3) printf("Three"); return 0; }
  • 12. if Compound Statement 10 COMPUTER FUNDAMENTAL & PROGRAMMING As in the if statement, compound statements may also be used in if-else statements (provided they are grouped together by braces). if (expression) { statement1; statement2; … } else { statement3; statement4; … } If there is a need to execute several statements (compound statement), then left and right {} braces can group these statements. Syntax
  • 13. 11 COMPUTER FUNDAMENTAL & PROGRAMMING If there is a need to execute several statements (compound statement), then left and right {} braces can group these statements. Every time C language encounters an if statement, 1. It determines whether expression is true or false. 2. If expression is true, then C language executes statement. 3. If expression is false, then C language ignores or skips the remainder of the if statement and proceeds to the next statement. if Compound Statement
  • 14. if – else if Statement 12 COMPUTER FUNDAMENTAL & PROGRAMMING user can decide among multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed. if (condition) statement; else if (condition) statement; . . else statement;
  • 15. if structure 13 COMPUTER FUNDAMENTAL & PROGRAMMING If there is a need to execute several statements (compound statement), then left and right {} braces can group these statements. if (condition) { statement1; statement2; … } else if (condition) { statement1; statement2; … } else { statement1; statement2; }
  • 16. Example 14 COMPUTER FUNDAMENTAL & PROGRAMMING 2. Write a program that determines if the input number is POSITIVE, NEGATIVE or ZERO. #include <stdio.h> int main() { int number; // Input a number from the user printf("Enter a number: "); scanf("%d", &number); // Check and display if the number is positive, negative, or zero if (number > 0) { printf(“%d”,number); printf("The number is positive.n"); } else if (number < 0) { printf(“%d”,number); printf("The number is negative.n"); } else { printf(“%d”,number); printf("The number is zero.n"); } return 0; }
  • 17. Example 15 COMPUTER FUNDAMENTAL & PROGRAMMING 3. Write program that will determine if the student is passed or failed. #include<stdio.h> int main() { int n1,n2,choice; float age; printf("Enter Age : "); scanf("%f",&age); if(age<16) { printf("%.0f not Qualify",age); } else if((age>=16) && (age<=17)) { printf(“You are %.0f, Eligible to vote for SK only",age); } else if((age>17) && (age<=25)) printf(“You are %.0f, Eligible to vote for SK & National",age); else printf("%.0f You are %.0f, Eligible to vote National only",age); return 0; }