SlideShare a Scribd company logo
1 of 30
Conditional Statements
In our daily life we do many things which depends on some
kind of conditions for e.g. If I study I will pass exams. If he
is hungry he will eat etc.
So in programming languages also we can perform different
sets of actions depending on the circumstances(‫.)حاالت‬
There are three major decision making instructions:
 If statement.
 If else statement.
 If else If statement.
The If statement
If statement is the simplest conditional statement.
If enables us to check condition is true or false.
When the condition is true it will execute the statement and
when false it
will not execute the statement.
If Statement syntax:
if(condition is true)
{
execute statements;
}
If statement flow chart
The If statement
1. Initialization takes place.
2. Condition is checked.
3. If condition is true the body of if is executed.
4. If condition is false then body will not be executed and
program will end.
5. The program will end.
The If statement Example
Code:
main( )
{
int num ;
printf ( “Enter a number less than 10 " ) ;
scanf ( "%d", &num ) ;
if ( num <= 10 )
printf ( “Nice Choice !" ) ;
}
The If else Statement
If else is another useful statement.
It executes statement 1 if the condition is true, and another if its false.
The simplest form of if else statement:
If Else Statement syntax:
if (expression)
{
statement 1;
}
else
{
statement 2;
}
The If else flow chart
The If else
1. Initialization takes place.
2. Condition is checked.
3. If condition is true the body of if is executed.
4. If condition is false then body of else is executed.
5. The program will end.
The If Else If Statement
When you have multiple expressions to evaluate(‫لگانہ‬ ‫,)اندازہ‬
you can use the if. Else if is form of the if statement.
If Else If statement flow chart
The If Else If statement
1. Initialization takes place.
2. Condition is checked.
3. If condition is true the body of if is executed.
4. If condition is false then next condition is checked and body of
if-else is
executed.
5. The condition is checked again and again until the condition
come true.
6. If none of else-if condition is true the body of else is executed.
7. The program will end.
If Else If Example
main( )
{
int m1, m2, m3, m4, m5, per ;
per = ( m1+ m2 + m3 + m4+ m5 ) / per ;
if ( per >= 60 )
printf ( "First division" ) ;
else if ( per >= 50 )
printf ( "Second division" ) ;
else if ( per >= 40 )
printf ( "Third division" ) ;
else
printf ( "fail" ) ;
}
Loops in C
 Loops are used to repeat a block of code.
 Being able to have your program repeatedly execute a
block of code is one of the most basic but useful tasks in
programming.
 If you want to do the same thing many times.
 For instance you want to print the same words ten times.
Loops in C
There are four basic types of loops which are:
 For Loop
 While Loop
 Do While Loop
 Nested Loop
Whenever we need to execute certain action
multiple times, we need to wrap programming
statement in the loop body.
The For Loop
For Loop Flow Chart
The For Loop
1. Initialization takes place.
2. Condition is checked.
3. If condition is true the body of for loop is executed.
4. Increment or decrement takes place.
5. the condition is checked again
6. Step 2 and 3 are checked again until the condition become
false.
7. The program will end.
Example of For Loop
The While Loop
A while loop in C programming repeatedly executes a
target statement as long as a given condition is true.
While Loop Flow Chart
The While Loop
1. Initialization takes place.
2. Condition is checked.
3. If condition is true the body of while loop is executed.
4. Increment or decrement takes place.
5. the condition is checked again.
6. Step 2 and 3 are checked again until the condition become
false.
7. The program will end.
Example of While Loop
The Do While Loop
Unlike for and while loops, which test the loop
condition at the top of the loop, the do while loop in C
programming checks its condition at the bottom( ‫آخر‬‫میں‬ )
of the loop.
A do while loop is similar to a while loop, except the
fact that it is guaranteed to execute at least one time.
The Do While Loop
Do While Loop Flow Chart
The Do While Loop
1. Initialization takes place.
2. The body of do-while loop is executed.
3. Increment or decrement takes place.
4. Condition is checked.
5. If condition is true then body of loop is executed else it
will terminate( ‫ختم‬‫کرنا‬ ).
6. If condition is true then Step 2 and 3 are executed again
until the condition become false.
7. The program will end
Example of Do While Loop
The Nested Loop
C programming allows to use one
loop inside another loop.
Nested Loop Syntax:
for ( int; condition; increment )
{
for ( int; condition; increment )
{
statement(s);
}
statement(s);
}
Example:
#include <stdio.h>
void main (void)
{
int i, j;
for(i = 2; i<100; i++)
{
for(j = 2; j <= (i/j); j++)
if(!(i%j))
break;
if(j > (i/j))
printf("%d n", i);
}
getch();
}

More Related Content

Similar to Conditional statements & Loops

Similar to Conditional statements & Loops (20)

Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
 
[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
4. decision making and some basic problem
4. decision making and some basic problem4. decision making and some basic problem
4. decision making and some basic problem
 
Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
itretion.docx
itretion.docxitretion.docx
itretion.docx
 
UNIT 2 PPT.pdf
UNIT 2 PPT.pdfUNIT 2 PPT.pdf
UNIT 2 PPT.pdf
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
 
BSc. III Unit iii VB.NET
BSc. III Unit iii VB.NETBSc. III Unit iii VB.NET
BSc. III Unit iii VB.NET
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
 
Loops
LoopsLoops
Loops
 
Loops in c
Loops in cLoops in c
Loops in c
 
M C6java6
M C6java6M C6java6
M C6java6
 
Loops and iteration.docx
Loops and iteration.docxLoops and iteration.docx
Loops and iteration.docx
 
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
 
Ch3 repetition
Ch3 repetitionCh3 repetition
Ch3 repetition
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
 

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 

Conditional statements & Loops

  • 1. Conditional Statements In our daily life we do many things which depends on some kind of conditions for e.g. If I study I will pass exams. If he is hungry he will eat etc. So in programming languages also we can perform different sets of actions depending on the circumstances(‫.)حاالت‬ There are three major decision making instructions:  If statement.  If else statement.  If else If statement.
  • 2. The If statement If statement is the simplest conditional statement. If enables us to check condition is true or false. When the condition is true it will execute the statement and when false it will not execute the statement. If Statement syntax: if(condition is true) { execute statements; }
  • 4. The If statement 1. Initialization takes place. 2. Condition is checked. 3. If condition is true the body of if is executed. 4. If condition is false then body will not be executed and program will end. 5. The program will end.
  • 5. The If statement Example Code: main( ) { int num ; printf ( “Enter a number less than 10 " ) ; scanf ( "%d", &num ) ; if ( num <= 10 ) printf ( “Nice Choice !" ) ; }
  • 6. The If else Statement If else is another useful statement. It executes statement 1 if the condition is true, and another if its false. The simplest form of if else statement: If Else Statement syntax: if (expression) { statement 1; } else { statement 2; }
  • 7. The If else flow chart
  • 8. The If else 1. Initialization takes place. 2. Condition is checked. 3. If condition is true the body of if is executed. 4. If condition is false then body of else is executed. 5. The program will end.
  • 9. The If Else If Statement When you have multiple expressions to evaluate(‫لگانہ‬ ‫,)اندازہ‬ you can use the if. Else if is form of the if statement.
  • 10. If Else If statement flow chart
  • 11. The If Else If statement 1. Initialization takes place. 2. Condition is checked. 3. If condition is true the body of if is executed. 4. If condition is false then next condition is checked and body of if-else is executed. 5. The condition is checked again and again until the condition come true. 6. If none of else-if condition is true the body of else is executed. 7. The program will end.
  • 12. If Else If Example main( ) { int m1, m2, m3, m4, m5, per ; per = ( m1+ m2 + m3 + m4+ m5 ) / per ; if ( per >= 60 ) printf ( "First division" ) ; else if ( per >= 50 ) printf ( "Second division" ) ; else if ( per >= 40 ) printf ( "Third division" ) ; else printf ( "fail" ) ; }
  • 13. Loops in C  Loops are used to repeat a block of code.  Being able to have your program repeatedly execute a block of code is one of the most basic but useful tasks in programming.  If you want to do the same thing many times.  For instance you want to print the same words ten times.
  • 14. Loops in C There are four basic types of loops which are:  For Loop  While Loop  Do While Loop  Nested Loop
  • 15. Whenever we need to execute certain action multiple times, we need to wrap programming statement in the loop body. The For Loop
  • 16. For Loop Flow Chart
  • 17. The For Loop 1. Initialization takes place. 2. Condition is checked. 3. If condition is true the body of for loop is executed. 4. Increment or decrement takes place. 5. the condition is checked again 6. Step 2 and 3 are checked again until the condition become false. 7. The program will end.
  • 19. The While Loop A while loop in C programming repeatedly executes a target statement as long as a given condition is true.
  • 21. The While Loop 1. Initialization takes place. 2. Condition is checked. 3. If condition is true the body of while loop is executed. 4. Increment or decrement takes place. 5. the condition is checked again. 6. Step 2 and 3 are checked again until the condition become false. 7. The program will end.
  • 23. The Do While Loop Unlike for and while loops, which test the loop condition at the top of the loop, the do while loop in C programming checks its condition at the bottom( ‫آخر‬‫میں‬ ) of the loop. A do while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time.
  • 24. The Do While Loop
  • 25. Do While Loop Flow Chart
  • 26. The Do While Loop 1. Initialization takes place. 2. The body of do-while loop is executed. 3. Increment or decrement takes place. 4. Condition is checked. 5. If condition is true then body of loop is executed else it will terminate( ‫ختم‬‫کرنا‬ ). 6. If condition is true then Step 2 and 3 are executed again until the condition become false. 7. The program will end
  • 27. Example of Do While Loop
  • 28. The Nested Loop C programming allows to use one loop inside another loop.
  • 29. Nested Loop Syntax: for ( int; condition; increment ) { for ( int; condition; increment ) { statement(s); } statement(s); }
  • 30. Example: #include <stdio.h> void main (void) { int i, j; for(i = 2; i<100; i++) { for(j = 2; j <= (i/j); j++) if(!(i%j)) break; if(j > (i/j)) printf("%d n", i); } getch(); }