SlideShare a Scribd company logo
Program
 A Program is a set of instructions that a computer uses to perform a
specific function.
To use an analogy, a program is like a computer’s recipe.
It contains a list of ingredients (called variables) and a list of
directions(called statements) that tell the computer how to execute a
specific task.
 A statement causes an action to be performed by the program.
 It translates directly into one or more executable computer
instructions.
 Generally statement is ended with semicolon.
 Most statements need a semicolon at the end; some do not.
Statements
Types of Statements
Compound Statement
 Compound statements are used to group the statements into a single
executable unit.
 It consists of one or more individual statements enclosed within the
braces { }
 The decision is described to the computer as a conditional
statement that can be answered either true or false.
 If the answer is true, one or more action statements are executed.
 If the answer is false, then a different action or set of actions is
executed.
Types of decision control structures:
 if
 if..else
 nested if…else
 else if ladder
 dangling else
 switch statement
Decision Control Structures
if (condition)
{
statement-block;
}
Decision Control Statement: if
The general form of a simple if statement is:
Following are the properties of an if statement:
 If the condition is true then the statement-block will be executed.
 If the condition is false it does not do anything ( or the statement is
skipped)
 The condition is given in parentheses and must be evaluated as true
(nonzero value) or false (zero value).
 If a compound statement is provided, it must be enclosed in opening
and closing braces.
Enter
Condition
Body of the IF Statement
Exit
Decision Control Statement: if
Flow Chart
Example:
main()
{
int a=10,b=20;
if(a>b)
{
printf(“%d”,a);
}
printf(“%d”,b);
}
if...else Logic Flow
Decision Control Statement: if..else
;
;
Syntactical Rules for if…else Statements
 The expression or condition which is followed by if statement
must be enclosed in parenthesis.
 No semicolon is needed for an if…else statement.
 Both the true and false statements can be any statement (even
another if…else).
 Multiple statements under if and else should be enclosed
between curly braces.
 No need to enclose a single statement in curly braces.
A Simple if...else Statement
Decision Control Statement: if..else
Compound Statements in an if...else
Example for Decision Control Statement: if..else
Example for Decision Control Statement: if..else
Nested if…else Statements
Decision Control Statement: nested if…else
 Nested if…else means within the if…else you can include another if…else either in
if block or else block.
Decision Control Statement: nested if…else
Decision Control Statement: nested if…else
if (condition1)
statements1;
else if (condition2)
statements2;
else if (condition3)
statements3;
else if (condition4)
statements4;
……
else if(conditionn)
statementsn;
else
default_statement;
statement x;
Decision Control Statement: else if
 The conditions are evaluated from the top to down.
 As soon as a true condition is found the statement associated with it is executed
and the control is transferred to the statementx by skipping the rest of the ladder.
 When all n conditions become false,final else containing default_statement
that will be executed
main() {
float m1,m2,m3,m4;
float perc;
printf(“Enter marksn”);
scanf(“%f%f%f%f”,&m1,&m2,&m3,&m4);
perc=(m1+m2+m3+m4)/4;
if(perc>=75)
printf(“nDistinction”);
else {
if(per<75 && per>=60)
printf(“nFirst Class”);
else {
if(per<60 && per>=50)
printf(“nSecond Class”);
else {
if(per<50 && per>=40)
printf(“nThird Class”);
else
Example program for nested if…else
Example program for else if
main()
{
float m1,m2,m3,m4;
float perc;
printf(“enter marksn”);
scanf(“%f%f%f%f”,&m1,&m2,&m3,&m4);
perc=(m1+m2+m3+m4)/4;
if(perc>=75)
printf(“nDistinction”);
else if(per<75 && per>=60)
printf(“nFirst Class”);
else if(per<60 && per>=50)
printf(“nSecond Class”);
else if(per<50 && per>=40)
printf(“nThird Class”);
else
printf(“nFail”);
}//main
Dangling else
Dangling else
else is always paired with the most recent unpaired if.
Dangling else Solution
Dangling else (contd…)
 To avoid dangling else problem place the inner if statement with in
the curly braces.
Conditional Expression
 A simple if…else can be represented using the conditional (ternary)
expression.
 It is a multi-way conditional statement generalizing the if…else
statement.
 It is a conditional control statement that allows some particular group
of statements to be chosen from several available groups.
 A switch statement allows a single variable to be compared with
several possible case labels, which are represented by constant values.
 If the variable matches with one of the constants, then an execution
jump is made to that point.
 A case label cannot appear more than once and there can only be one
default expression.
Decision Control Statement: switch
 Note: switch statement does not allow less than ( < ), greater than (
> ).
 ONLY the equality operator (==) is used with a switch statement.
 The control variable must be integral (int or char) only.
 When the switch statement is encountered, the control variable is
evaluated.
 Then, if that evaluated value is equal to any of the values specified
in a case clause, the statements immediately following the colon
(“:”) begin to run.
 Default case is optional and if specified, default statements will be
executed, if there is no match for the case labels.
 Once the program flow enters a case label, the statements
associated with case have been executed, the program flow
continues with the statement for the next case. (if there is no break
statement after case label.)
Decision Control Statement: switch
General format of switch:
Decision Control Statement: switch
 The following results are possible, depending on the value of printFlag.
 If printFlag is 1, then all three printf statements are executed.
 If printFlag is 2, then the first print statement is skipped and the last two
are executed.
 Finally, if printFlag is neither 1 nor 2, then only the statement defined by
the default is executed.
Decision Control Statement: switch
Example1 for switch statement:
Decision Control Statement: switch
 If you want to execute only one case-label, C provides break
statement.
 It causes the program to jump out of the switch statement, that is go
to the closing braces (}) and continues the remaining code of the
program.
 If we add break to the last statement of the case, the general form of
switch case is as follows:
Decision Control Statement: switch
General format of switch:
Decision Control Statement: switch
Example2 for switch statement:
Decision Control Statement: switch
Concept of a loop(Iterative)
 The real power of computers is in their ability to repeat an
operation or a series of operations many times.
 This repetition, called looping, is one of the basic structured
programming concepts.
 Each loop must have an expression that determines if the loop is
done.
 If it is not done, the loop repeats one more time; if it is done, the
loop terminates.
Concept of a Loop
Pretest Loop
In each iteration, the control expression is tested first. If it is
true, the loop continues; otherwise, the loop is terminated.
Post-test Loop
In each iteration, the loop action(s) are executed. Then the
control expression is tested. If it is true, a new iteration is
started; otherwise, the loop terminates.
Note
Pretest and Post-test Loops
Loop Comparisons
C Loop Constructs
while
 The "while" loop is a generalized looping structure that employs a variable
or expression for testing the condition.
 It is a repetition statement that allows an action to be repeated while some
conditions remain true.
 The body of while statement can be a single statement or compound
statements.
 It doesn’t perform even a single operation if condition fails.
The while Statement
Example 1: To print 1 to 10 natural numbers
#include<stdio.h>
main()
{
int i;
i=1;
while (i<=10)
{
printf(“%dn”,i);
i++;
}
}
Example 2: To print the reverse of the given number.
void main()
{
int n, n1,rem, rev = 0;
printf("n Enter a positive number: ");
scanf("%d",&n);
n1=n;
while(n != 0)
{
rem = n%10;
rev = rev*10+rem;
n = n/10;
}
printf("The reverse of %d is %d",n1,rev);
}
do…while Statement
Example 3: To print fibonacci sequence for the given number.
#include<stdio.h>
main()
{
int a=0,b=1,c,i;
i=1;
printf("%d%d",a,b);
do
{
c=a+b;
i++;
printf("%3d",c);
a=b;
b=c;
}while(i<=10);
}
Example 4: To print multiplication table for 5.
#include <stdio.h>
void main()
{
int i = 1, n=5;
do
{
printf(“ %d * %d = %d “, n, i, n*i);
i = i + 1;
} while ( i<= 10);
}
 A for loop is used when a loop is to be executed a known number
of times.
We can do the same thing with a while loop, but the for loop is
easier to read and more natural for counting loops.
General form of the for is:
for( initialization; test-condition; updation)
{
Body of the loop
}
for
for Statement
C statements.ppt presentation in c language
break
 When a break statement is enclosed inside a block or loop, the loop is
immediately exited and program continues with the next statement
immediately following the loop.
 When loop are nested break only exit from the inner loop containing it.
The format of the break statement is:
Example 6: Program to demonstrate break statement.
#include<stdio.h>
main()
{
int i;
i=1;
while(i<=10)
{
if(i==8)
break;
printf(“%dt”,i);
i=i+1;
}
printf(“n Thanking You”);
}
continue
 When a continue statement is enclosed inside a block or loop, the loop is
to be continued with the next iteration.
 The continue statement tells the compiler, skip the following statements
and continue with the next iteration.
 The format of the continue statement is:
Example 5: Program to demonstrate continue statement.
#include<stdio.h>
main()
{
int i;
for(i=1;i<=5;i++)
{
if(i = = 3)
continue;
printf(" %d",i);
}
}

More Related Content

PPTX
Training and development
PPTX
Participation Constraints in ER diagram
PPTX
Xml security overview and canonicalization
PPT
Dbms
 
PPTX
Pemrograman Konsep Routing
PDF
Overview of Database and Database Management
PDF
Database Management System NOTES for 2nd year
PPT
Role%20 analysis
Training and development
Participation Constraints in ER diagram
Xml security overview and canonicalization
Dbms
 
Pemrograman Konsep Routing
Overview of Database and Database Management
Database Management System NOTES for 2nd year
Role%20 analysis

What's hot (14)

DOC
New visiting card request form
PPTX
File Organization
PPTX
Materi 1 - Struktur Hirarki Basis data.pptx
PPTX
Erd dan contoh kasus
PDF
Algoritma fuzzy c means fcm java c++ contoh program
PDF
Aplikasi sistem informasi akademik berbasis desktop
PDF
Linked List Static and Dynamic Memory Allocation
PPTX
The Relational Model
PPT
Entity relationship diagram (erd)
PPT
Unit 1 introduction to data structure
PPTX
Implementation of queue using singly and doubly linked list.
DOCX
DATABASE MANAGEMENT SYSTEM
PDF
OSA Con 2022 - Switching Jaeger Distributed Tracing to ClickHouse to Enable A...
PPT
Normalisation revision
New visiting card request form
File Organization
Materi 1 - Struktur Hirarki Basis data.pptx
Erd dan contoh kasus
Algoritma fuzzy c means fcm java c++ contoh program
Aplikasi sistem informasi akademik berbasis desktop
Linked List Static and Dynamic Memory Allocation
The Relational Model
Entity relationship diagram (erd)
Unit 1 introduction to data structure
Implementation of queue using singly and doubly linked list.
DATABASE MANAGEMENT SYSTEM
OSA Con 2022 - Switching Jaeger Distributed Tracing to ClickHouse to Enable A...
Normalisation revision
Ad

Similar to C statements.ppt presentation in c language (20)

PDF
Controls & Loops in C
PPTX
Selection Statements in C Programming
PPTX
Decision statements in c language
PPTX
Decision statements in c laguage
PPTX
control-statements in C Language MH.pptx
PPTX
Dti2143 chap 4 control structures aka_selection
PPTX
Dti2143 chap 4 control structures aka_selection
PDF
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
PDF
Control structure and Looping statements
PDF
CS305PC_C++_UNIT 2.pdf jntuh third semester
PPTX
CH-4 (1).pptx
PPTX
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
PPTX
Control Statement programming
PDF
Chapter 8 - Conditional Statement
PPT
Lecture 9- Control Structures 1
PDF
Control statements-Computer programming
PPT
Lecture 3
PDF
Loops and conditional statements
PPT
Control Structures
Controls & Loops in C
Selection Statements in C Programming
Decision statements in c language
Decision statements in c laguage
control-statements in C Language MH.pptx
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
Control structure and Looping statements
CS305PC_C++_UNIT 2.pdf jntuh third semester
CH-4 (1).pptx
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
Control Statement programming
Chapter 8 - Conditional Statement
Lecture 9- Control Structures 1
Control statements-Computer programming
Lecture 3
Loops and conditional statements
Control Structures
Ad

Recently uploaded (20)

PPTX
ai tools demonstartion for schools and inter college
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Nekopoi APK 2025 free lastest update
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
assetexplorer- product-overview - presentation
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
Understanding Forklifts - TECH EHS Solution
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Digital Strategies for Manufacturing Companies
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
medical staffing services at VALiNTRY
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Transform Your Business with a Software ERP System
ai tools demonstartion for schools and inter college
Which alternative to Crystal Reports is best for small or large businesses.pdf
Nekopoi APK 2025 free lastest update
Operating system designcfffgfgggggggvggggggggg
PTS Company Brochure 2025 (1).pdf.......
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
VVF-Customer-Presentation2025-Ver1.9.pptx
assetexplorer- product-overview - presentation
How to Choose the Right IT Partner for Your Business in Malaysia
Digital Systems & Binary Numbers (comprehensive )
Understanding Forklifts - TECH EHS Solution
wealthsignaloriginal-com-DS-text-... (1).pdf
Odoo Companies in India – Driving Business Transformation.pdf
Digital Strategies for Manufacturing Companies
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Computer Software and OS of computer science of grade 11.pptx
medical staffing services at VALiNTRY
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Wondershare Filmora 15 Crack With Activation Key [2025
Transform Your Business with a Software ERP System

C statements.ppt presentation in c language

  • 1. Program  A Program is a set of instructions that a computer uses to perform a specific function. To use an analogy, a program is like a computer’s recipe. It contains a list of ingredients (called variables) and a list of directions(called statements) that tell the computer how to execute a specific task.
  • 2.  A statement causes an action to be performed by the program.  It translates directly into one or more executable computer instructions.  Generally statement is ended with semicolon.  Most statements need a semicolon at the end; some do not. Statements
  • 4. Compound Statement  Compound statements are used to group the statements into a single executable unit.  It consists of one or more individual statements enclosed within the braces { }
  • 5.  The decision is described to the computer as a conditional statement that can be answered either true or false.  If the answer is true, one or more action statements are executed.  If the answer is false, then a different action or set of actions is executed. Types of decision control structures:  if  if..else  nested if…else  else if ladder  dangling else  switch statement Decision Control Structures
  • 6. if (condition) { statement-block; } Decision Control Statement: if The general form of a simple if statement is: Following are the properties of an if statement:  If the condition is true then the statement-block will be executed.  If the condition is false it does not do anything ( or the statement is skipped)  The condition is given in parentheses and must be evaluated as true (nonzero value) or false (zero value).  If a compound statement is provided, it must be enclosed in opening and closing braces.
  • 7. Enter Condition Body of the IF Statement Exit Decision Control Statement: if Flow Chart Example: main() { int a=10,b=20; if(a>b) { printf(“%d”,a); } printf(“%d”,b); }
  • 8. if...else Logic Flow Decision Control Statement: if..else ; ;
  • 9. Syntactical Rules for if…else Statements  The expression or condition which is followed by if statement must be enclosed in parenthesis.  No semicolon is needed for an if…else statement.  Both the true and false statements can be any statement (even another if…else).  Multiple statements under if and else should be enclosed between curly braces.  No need to enclose a single statement in curly braces.
  • 10. A Simple if...else Statement Decision Control Statement: if..else
  • 11. Compound Statements in an if...else
  • 12. Example for Decision Control Statement: if..else
  • 13. Example for Decision Control Statement: if..else
  • 14. Nested if…else Statements Decision Control Statement: nested if…else  Nested if…else means within the if…else you can include another if…else either in if block or else block.
  • 15. Decision Control Statement: nested if…else
  • 16. Decision Control Statement: nested if…else
  • 17. if (condition1) statements1; else if (condition2) statements2; else if (condition3) statements3; else if (condition4) statements4; …… else if(conditionn) statementsn; else default_statement; statement x; Decision Control Statement: else if  The conditions are evaluated from the top to down.  As soon as a true condition is found the statement associated with it is executed and the control is transferred to the statementx by skipping the rest of the ladder.  When all n conditions become false,final else containing default_statement that will be executed
  • 18. main() { float m1,m2,m3,m4; float perc; printf(“Enter marksn”); scanf(“%f%f%f%f”,&m1,&m2,&m3,&m4); perc=(m1+m2+m3+m4)/4; if(perc>=75) printf(“nDistinction”); else { if(per<75 && per>=60) printf(“nFirst Class”); else { if(per<60 && per>=50) printf(“nSecond Class”); else { if(per<50 && per>=40) printf(“nThird Class”); else Example program for nested if…else
  • 19. Example program for else if main() { float m1,m2,m3,m4; float perc; printf(“enter marksn”); scanf(“%f%f%f%f”,&m1,&m2,&m3,&m4); perc=(m1+m2+m3+m4)/4; if(perc>=75) printf(“nDistinction”); else if(per<75 && per>=60) printf(“nFirst Class”); else if(per<60 && per>=50) printf(“nSecond Class”); else if(per<50 && per>=40) printf(“nThird Class”); else printf(“nFail”); }//main
  • 20. Dangling else Dangling else else is always paired with the most recent unpaired if.
  • 21. Dangling else Solution Dangling else (contd…)  To avoid dangling else problem place the inner if statement with in the curly braces.
  • 22. Conditional Expression  A simple if…else can be represented using the conditional (ternary) expression.
  • 23.  It is a multi-way conditional statement generalizing the if…else statement.  It is a conditional control statement that allows some particular group of statements to be chosen from several available groups.  A switch statement allows a single variable to be compared with several possible case labels, which are represented by constant values.  If the variable matches with one of the constants, then an execution jump is made to that point.  A case label cannot appear more than once and there can only be one default expression. Decision Control Statement: switch
  • 24.  Note: switch statement does not allow less than ( < ), greater than ( > ).  ONLY the equality operator (==) is used with a switch statement.  The control variable must be integral (int or char) only.  When the switch statement is encountered, the control variable is evaluated.  Then, if that evaluated value is equal to any of the values specified in a case clause, the statements immediately following the colon (“:”) begin to run.  Default case is optional and if specified, default statements will be executed, if there is no match for the case labels.  Once the program flow enters a case label, the statements associated with case have been executed, the program flow continues with the statement for the next case. (if there is no break statement after case label.) Decision Control Statement: switch
  • 25. General format of switch: Decision Control Statement: switch
  • 26.  The following results are possible, depending on the value of printFlag.  If printFlag is 1, then all three printf statements are executed.  If printFlag is 2, then the first print statement is skipped and the last two are executed.  Finally, if printFlag is neither 1 nor 2, then only the statement defined by the default is executed. Decision Control Statement: switch
  • 27. Example1 for switch statement: Decision Control Statement: switch
  • 28.  If you want to execute only one case-label, C provides break statement.  It causes the program to jump out of the switch statement, that is go to the closing braces (}) and continues the remaining code of the program.  If we add break to the last statement of the case, the general form of switch case is as follows: Decision Control Statement: switch
  • 29. General format of switch: Decision Control Statement: switch
  • 30. Example2 for switch statement: Decision Control Statement: switch
  • 31. Concept of a loop(Iterative)  The real power of computers is in their ability to repeat an operation or a series of operations many times.  This repetition, called looping, is one of the basic structured programming concepts.  Each loop must have an expression that determines if the loop is done.  If it is not done, the loop repeats one more time; if it is done, the loop terminates.
  • 32. Concept of a Loop
  • 33. Pretest Loop In each iteration, the control expression is tested first. If it is true, the loop continues; otherwise, the loop is terminated. Post-test Loop In each iteration, the loop action(s) are executed. Then the control expression is tested. If it is true, a new iteration is started; otherwise, the loop terminates. Note
  • 37. while  The "while" loop is a generalized looping structure that employs a variable or expression for testing the condition.  It is a repetition statement that allows an action to be repeated while some conditions remain true.  The body of while statement can be a single statement or compound statements.  It doesn’t perform even a single operation if condition fails.
  • 39. Example 1: To print 1 to 10 natural numbers #include<stdio.h> main() { int i; i=1; while (i<=10) { printf(“%dn”,i); i++; } }
  • 40. Example 2: To print the reverse of the given number. void main() { int n, n1,rem, rev = 0; printf("n Enter a positive number: "); scanf("%d",&n); n1=n; while(n != 0) { rem = n%10; rev = rev*10+rem; n = n/10; } printf("The reverse of %d is %d",n1,rev); }
  • 42. Example 3: To print fibonacci sequence for the given number. #include<stdio.h> main() { int a=0,b=1,c,i; i=1; printf("%d%d",a,b); do { c=a+b; i++; printf("%3d",c); a=b; b=c; }while(i<=10); }
  • 43. Example 4: To print multiplication table for 5. #include <stdio.h> void main() { int i = 1, n=5; do { printf(“ %d * %d = %d “, n, i, n*i); i = i + 1; } while ( i<= 10); }
  • 44.  A for loop is used when a loop is to be executed a known number of times. We can do the same thing with a while loop, but the for loop is easier to read and more natural for counting loops. General form of the for is: for( initialization; test-condition; updation) { Body of the loop } for
  • 47. break  When a break statement is enclosed inside a block or loop, the loop is immediately exited and program continues with the next statement immediately following the loop.  When loop are nested break only exit from the inner loop containing it. The format of the break statement is:
  • 48. Example 6: Program to demonstrate break statement. #include<stdio.h> main() { int i; i=1; while(i<=10) { if(i==8) break; printf(“%dt”,i); i=i+1; } printf(“n Thanking You”); }
  • 49. continue  When a continue statement is enclosed inside a block or loop, the loop is to be continued with the next iteration.  The continue statement tells the compiler, skip the following statements and continue with the next iteration.  The format of the continue statement is:
  • 50. Example 5: Program to demonstrate continue statement. #include<stdio.h> main() { int i; for(i=1;i<=5;i++) { if(i = = 3) continue; printf(" %d",i); } }