SlideShare a Scribd company logo
C Control Statements
For this purpose, C Programming uses the control statements to
control the flow of a program. If the condition is satisfied the code
followed by that condition will execute. If not simply that the code
will be neglected by the compiler.
Types of Control statements
 if
 if else
 if-else-if control statement
 switch() case control statement
C if statement
Why if Statement?
All programming languages enable you to make decisions. They
enable the program to follow a certain course of action depending on
whether a particular condition is met. This is what gives programming
language their intelligence.
C if Syntax And Definition
 C uses the keyword if to execute a set of statements when logical
condition is true.
 When the logical condition is false, the compiler simply skips the
statement within the block.
 The "if" statement is also known as one way decision statement.
Syntax if statement
Syntax
if(a>10) a=20
{
here goes statements;
.
.
.
.
here goes statements;
}
if Statement Uses
if statements are commonly used in following scenarios
 Is A bigger than B?
 Is X equal to Y?
 Is M not equal to N?
Realtime Time if Statement Uses
realtimeif
if(x = 123)
digitalWrite(LEDpin, high)
Note:
x is a variable which get its input from a sensor continuously.
if Statement Rules
 The expression (or) condition is always enclosed within pair of
parenthesis. e.g.) if ( a > b )
 The if statement should not be terminated with a semicolon. If it
happens, the block of statements will not execute even the condition
is true.
 The statements following the if condition is normally enclosed
between 2 braces (in curly braces).
if statement Flow Chart
Following flow chart will clearly explain how if statement works
if statement C Program
ifstatement.c
#include <stdio.h> //header file section
#include <conio.h>
int main()
{
int age = 18;
if(age > 17)
{
printf("you are eligible for voting ");
}
printf("nThis is normal flow ");
return 0;
}
 you are eligible for voting
 This is normal flow
Note:
If the user input is greater than 17, then the condition will be true
and the statements under if condition gets executed. Otherwise, it
executes next to the if block
C if-else Statement
Why if-else Statement?
 As We've seen, the if statement allows us to run a block of code if an
expression evaluates to true.
 If the expression evalutes to false, the code is skipped. Thus we can
enhance this decision-making process by adding an else statement to an
if construction.
 This lets us run one block of code if an expression is true, and a different
block of code if the expression is false.
C if-else Syntax And Definition
 C uses the keywords if and else to execute a set of statements either
logical condition is true or false.
 If the condition is true, the statements under the keyword if will be
executed.
 If the condition is false, the statements under the keyword else will be
executed.
Syntax if-else statement
Syntax
if(condition)
{
here go statements....
}
else
{
here go statements....
}
if else Statement Flow Chart
Following flow chart will clearly explain how if else statement works
Realtime Time if else Statement Uses
Arduino microcontroller make use C programming, where you can alarm if your
sensors output in greater than or lesser than expected value, else blink green
light always.
realtimeifelse
if(x = 123)
digitalWrite(LEDpin,high)
else
digitalWrite(LEDpin,low)
Note:
x is a variable which get its input from a sensor continuously.
if-else C Program
ifelsestatement.c
#include <stdio.h> //header file section
int main()
{
int age;
printf("Enter your age : ");
scanf("%d",&age);
if(age > 17)
{
printf("nyou are eligible for voting ");
}
else
{
printf("nSorry, you are not eligible for voting ");
}
printf("nThis is normal flow ");
return 0;
}
 Enter your age : 16
 Sorry, you are not eligible for voting
 This is normal flow
Note:
The user input for a variable 'age' is 16, the expression or condition tends
to false. So, the else block gets executed.
Realtime Time if else Statement Uses
Arduino microcontroller make use C programming, where you can alarm if your
sensors output in greater than or lesser than expected value, else blink
green light always.
realtimeifelse
if(x = 123)
digitalWrite(LEDpin,high)
else
digitalWrite(LEDpin,low)
Note:
x is a variable which get its input from a sensor continuously.
if-else C Program
ifelsestatement.c
#include <stdio.h> //header file section
int main()
{
int age;
printf("Enter your age : ");
scanf("%d",&age);
if(age > 17)
{
printf("nyou are eligible for voting ");
}
else
{
printf("nSorry, you are not eligible for voting ");
}
printf("nThis is normal flow ");
return 0;
}
 Enter your age : 16
 Sorry, you are not eligible for voting
 This is normal flow
Note:
The user input for a variable 'age' is 16, the expression or condition tends
to false. So, the else block gets executed.
Realtime Time if else Statement Uses
Arduino microcontroller make use C programming, where you can alarm if your
sensors output in greater than or lesser than expected value, else blink green
light always.
realtimeifelse
if(x = 123)
digitalWrite(LEDpin,high)
else
digitalWrite(LEDpin,low)
Note:
x is a variable which get its input from a sensor continuously.
if-else C Program
ifelsestatement.c
#include <stdio.h> //header file section
int main()
{
int age;
printf("Enter your age : ");
scanf("%d",&age);
if(age > 17)
{
printf("nyou are eligible for voting ");
}
else
{
printf("nSorry, you are not eligible for voting ");
}
printf("nThis is normal flow ");
return 0;
}
 Enter your age : 16
 Sorry, you are not eligible for voting
 This is normal flow
Note:
The user input for a variable 'age' is 16, the expression or condition tends to
false. So, the else block gets executed.
C Nested if Statement
Why Nested if Statement
The statement that is executed when an if expression is true can be
another if, as can the statement in an else clause. This enables you to
express such convoluted logic as "if age of Lingcoln is greater than
age of john "and if age of Lingcoln is greater than age of renu". Then
we decide Lingan is elder of all
Syntax and Definition Nested if statement
 In C programming, control statements like as if can be nested, that
means we can write one within another.
 If outer if statement fails, then the compiler skips the entire block
irrespective of their inner if statement.
Syntax nested if statement
Syntax
if(condition)
{
if(condition)
{
here goes statements;
}
}
Nested if Statement Flowchart
The following flowchart will clearly demonstrate how nested if
statement works
Program Nested if statement
Let us write a C program using Nested if statement
nestedif.c
#include <stdio.h> //header file section
int main()
{
int a = 47, b = 25, c = 3;
if (a > b)
{
if (a > c)
{
printf("The value a is greater than b and c ");
}
}
printf("nThis is normal flow ");
return 0;
}
 The value a is greater than b and c
 This is normal flow
Note:
When the first if statement executed, the value 'a' is compared with
value 'b', if the expression is true, then inner if statement executed,
the inner expression compares the value a with value c, if the inner if
statement also true, the compiler will display the output.
C Nested else if Statement
In nested else if statement, the number of logical conditions can be
checked for executing various statements.
 If any logical condition is true the compiler executes the block under that
else if condition, otherwise, it skips and executes else block.
 By default else block will be executed, if all the remaining conditions are
false.
 It must be noted in nested else if statement, if more than one else if
statement is true then the first else if statement which satisfies the
condition in it will alone execute.
C Nested else if Syntax
Syntax Nested else if Statement
Syntax
if(test_expression 1)
{
here goes statements;
}
else if(test_expression 2)
{
here goes statements;
}
else
{
here goes statements;
}
else-if Flowchart
The following flowchart will clearly demonstrate, how else-if works?
else if Statement Program
Lets take a look at the else if statement in action.
nestedif.c
#include <stdio.h> //header file section
int main()
{
int a;
printf("Enter a number: ");
scanf ("%d ", &a);
if(a > 0)
{
printf("n%d is a positive number", a);
}
else if(a < 0)
{
printf("n%d is a negative number", a);
}
else
{
printf("n%d is a zero",a);
}
return 0;
}
 Enter a number: -47
 -47 is a negative number
Note:
Here, the compiler will ask user to enter the number, when user enters the
number compiler will check if statement, as if statement resulted in false the
compiler will check else if statement, as the condition is true compiler execute
the block followed by else if statement and then exit.
Swith case:
A switch statement allows a variable to be tested for equality against a list of values.
Each value is called a case, and the variable being switched on is checked for
each switch case.
Syntax
The syntax for a switch statement in C programming language is as follows −
switch(expression)
{
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
statement(s);
}
The following rules apply to a switch statement −
 The expression used in a switch statement must have an integral or
enumerated type, or be of a class type in which the class has a single
conversion function to an integral or enumerated type.
 You can have any number of case statements within a switch. Each case is
followed by the value to be compared to and a colon.
 The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a constant or a literal.
 When the variable being switched on is equal to a case, the statements following
that case will execute until a break statement is reached.
 When a break statement is reached, the switch terminates, and the flow of
control jumps to the next line following the switch statement.
 Not every case needs to contain a break. If no break appears, the flow of control
will fall through to subsequent cases until a break is reached.
 A switch statement can have an optional default case, which must appear at
the end of the switch. The default case can be used for performing a task when
none of the cases is true. No break is needed in the default case.
Flow Diagram
Example
#include <stdio.h>
int main () {
/* local variable definition */
char grade = 'B';
switch(grade)
{
case 'A' :
printf("Excellent!n" );
break;
case 'B' :
case 'C' :
printf("Well donen" );
break;
case 'D' :
printf("You passedn" );
break;
case 'F' :
printf("Better try againn" );
break;
default :
printf("Invalid graden" );
}
printf("Your grade is %cn", grade );
return 0;
}
When the above code is compiled and executed, it produces the following result −
Well done
Your grade is B
break, continue and goto statements
The break; continue; and goto; statements are used to alter the normal flow
of a program.
Loops perform a set of repetitive task until text expression becomes false
but it is sometimes desirable to skip some statement/s inside loop or
terminate the loop immediately without checking the test expression. In
such cases, break and continue statements are used.
switch
Here, we will write a C program to find the grade of a student using switch case
statements. The below table shows the grading system.
Score in subject Grade
>=90 A
80-89 B
70-79 C
60-69 D
50-59 E
<50 F
C program to find grade of a student using switch case
statement
#include<stdio.h>
int main()
{
int score;
printf("Enter score( 0-100 ): ");
scanf("%d", &score);
switch( score / 10 )
{
case 10:
case 9:
printf("Grade: A");
break;
case 8:
printf("Grade: B");
break;
case 7:
printf("Grade: C");
break;
case 6:
printf("Grade: D");
break;
case 5:
printf("Grade: E");
break;
default:
printf("Grade: F");
break;
}
return 0;
}
break statement
In C programming, break statement is used with conditional if statement.
The break is used in terminating the loop immediately after it is
encountered.
it is also used in switch...casestatement. which is explained in next topic.
Syntax:
break;
The break statement can be used in terminating loops like for, while and do...while
Example:
//Write a C Program Which use of break statement.
#include<stdio.h>
#include<conio.h>
void main(){
int num, sum=0;
int i,n;
printf("Note: Enter Zero for break loop!n");
printf("Enter Number of inputsn");
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("Enter num%d: ",i);
scanf("%d",&num);
if(num==0) {
break; /*this breaks loop if num == 0 */
printf("Loop Breakedn");
}
sum=sum+num;
}
printf("Total is %d",sum);
getch();
}
Note: Enter Zero for break loop!
Enter Number of inputs
5
Enter num1: 5
Enter num2: 10
Enter num3: 0
Loop Breaked
Total is 15
continue statement
It is sometimes desirable to skip some statements inside the loop. In such cases, continue statement is used.
Syntax:
continue;
Just like break, continue is also used with conditional if statement.
Example:
//Write a C Program Which use of continue statment.
#include<stdio.h>
#include<conio.h>
void main(){
int i, n=20;
clrscr();
for(i=1;i<=n;++i){
if(i % 5 == 0) {
printf("passn");
continue; /*this continue the execution of loop if i % 5 == 0 */
}
printf("%dn",i);
}
getch();
}
Download
Output:
Command Prompt
1
2
3
4
pass
6
7
8
9
pass
11
12
13
14
pass
16
17
18
19
pass
goto statement
In C programming, goto statement is used for altering the normal sequence
of program execution by transferring control to some other part of the
program.
Syntax:
goto label;
.............
.............
.............
label:
statement;
In this syntax, label is an identifier.
When, the control of program reaches to goto statement, the control of the
program will jump to the label: and executes the code below it.
goto-statement
Example:
//Write a C Program Which Print 1 To 10 Number Using goto statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
count: //This is Label
printf("%dn",i);
i++;
if(i<=10) {
goto count; //This jumps to label "count:"
}
getch();
}
Output:
Command Prompt
1
2
3
4
5
6
7
8
9
10
Note:
Though goto statement is included in ANSI standard of C, use of goto
statement should be reduced as much as possible in a program.
Reasons to avoid goto statement
Though, using goto statement give power to jump to any part of program,
using goto statement makes the logic of the program complex and tangled.
In modern programming, goto statement is considered a harmful construct
and a bad programming practice.
The goto statement can be replaced in most of C program with the use of
break and continue statements.
In fact, any program in C programming can be perfectly written without the
use of goto statement.
All programmer should try to avoid goto statement as possible as they can.

More Related Content

What's hot (6)

PPTX
Angka Penting dan Notasi Ilmiah - Pertemuan 3.pptx
andryanihutabarat12
 
PPTX
Politik Luar Negeri, Bebas Aktif Indonesia dan Lembaga Internasional (ASEAN, ...
Alviony Charisa
 
PPTX
hakikat keterampilan berbahasa
safitkafit
 
PPTX
Powerpoint: Tata Surya
Yuliana Masitoh
 
DOCX
Rencana Aksi Pengimbasan Diklat Penguasaan TIK (edit).docx
FebrinaTarigan3
 
PPSX
Persepsi dan Mekanisme penglihatan
Navia Fathona
 
Angka Penting dan Notasi Ilmiah - Pertemuan 3.pptx
andryanihutabarat12
 
Politik Luar Negeri, Bebas Aktif Indonesia dan Lembaga Internasional (ASEAN, ...
Alviony Charisa
 
hakikat keterampilan berbahasa
safitkafit
 
Powerpoint: Tata Surya
Yuliana Masitoh
 
Rencana Aksi Pengimbasan Diklat Penguasaan TIK (edit).docx
FebrinaTarigan3
 
Persepsi dan Mekanisme penglihatan
Navia Fathona
 

Similar to C Control Statements.docx (20)

PDF
Fundamental of Information Technology - UNIT 8
Shipra Swati
 
PDF
Control statements-Computer programming
nmahi96
 
PPTX
Basics of Control Statement in C Languages
Dr. Chandrakant Divate
 
PPTX
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
PDF
Control structure and Looping statements
BalaKrishnan466
 
PPTX
Dti2143 chap 4 control structures aka_selection
alish sha
 
PPTX
Dti2143 chap 4 control structures aka_selection
alish sha
 
PPTX
CH-4 (1).pptx
Mehul Desai
 
PPTX
control-statements in C Language MH.pptx
mehedi_hasan
 
PDF
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
Carlos701746
 
PPTX
Control Statement programming
University of Potsdam
 
PPTX
Decision statements in c laguage
Tanmay Modi
 
PPTX
Decision statements in c language
tanmaymodi4
 
PDF
Loops and conditional statements
Saad Sheikh
 
PDF
Principals of Programming in CModule -5.pdfModule_2_P2 (1).pdf
anilcsbs
 
PPTX
Control Structures in C
sana shaikh
 
PDF
Programming basics
246paa
 
PPT
C statements.ppt presentation in c language
chintupro9
 
PPTX
Introduction& Overview-to-C++_programming.pptx
divyadhanwani67
 
Fundamental of Information Technology - UNIT 8
Shipra Swati
 
Control statements-Computer programming
nmahi96
 
Basics of Control Statement in C Languages
Dr. Chandrakant Divate
 
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
Control structure and Looping statements
BalaKrishnan466
 
Dti2143 chap 4 control structures aka_selection
alish sha
 
Dti2143 chap 4 control structures aka_selection
alish sha
 
CH-4 (1).pptx
Mehul Desai
 
control-statements in C Language MH.pptx
mehedi_hasan
 
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
Carlos701746
 
Control Statement programming
University of Potsdam
 
Decision statements in c laguage
Tanmay Modi
 
Decision statements in c language
tanmaymodi4
 
Loops and conditional statements
Saad Sheikh
 
Principals of Programming in CModule -5.pdfModule_2_P2 (1).pdf
anilcsbs
 
Control Structures in C
sana shaikh
 
Programming basics
246paa
 
C statements.ppt presentation in c language
chintupro9
 
Introduction& Overview-to-C++_programming.pptx
divyadhanwani67
 
Ad

More from JavvajiVenkat (6)

PPTX
CONTROL STMTS.pptx
JavvajiVenkat
 
DOCX
itretion.docx
JavvajiVenkat
 
PPTX
unit2 C-ProgrammingChapter 2 Control statements.pptx
JavvajiVenkat
 
DOCX
loops and iteration.docx
JavvajiVenkat
 
DOCX
Nested Loops in C unit2.docx
JavvajiVenkat
 
DOCX
UNIT-II CP DOC.docx
JavvajiVenkat
 
CONTROL STMTS.pptx
JavvajiVenkat
 
itretion.docx
JavvajiVenkat
 
unit2 C-ProgrammingChapter 2 Control statements.pptx
JavvajiVenkat
 
loops and iteration.docx
JavvajiVenkat
 
Nested Loops in C unit2.docx
JavvajiVenkat
 
UNIT-II CP DOC.docx
JavvajiVenkat
 
Ad

Recently uploaded (20)

PPTX
cybersecurityandthe importance of the that
JayachanduHNJc
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
Air -Powered Car PPT by ER. SHRESTH SUDHIR KOKNE.pdf
SHRESTHKOKNE
 
PPTX
filteration _ pre.pptx 11111110001.pptx
awasthivaibhav825
 
PPTX
Water resources Engineering GIS KRT.pptx
Krunal Thanki
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PPTX
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
PDF
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
PDF
4 Tier Teamcenter Installation part1.pdf
VnyKumar1
 
PPTX
ENSA_Module_7.pptx_wide_area_network_concepts
RanaMukherjee24
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PPTX
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
PPTX
Precedence and Associativity in C prog. language
Mahendra Dheer
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
PPTX
Online Cab Booking and Management System.pptx
diptipaneri80
 
PDF
AI-Driven IoT-Enabled UAV Inspection Framework for Predictive Maintenance and...
ijcncjournal019
 
PDF
All chapters of Strength of materials.ppt
girmabiniyam1234
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
cybersecurityandthe importance of the that
JayachanduHNJc
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Air -Powered Car PPT by ER. SHRESTH SUDHIR KOKNE.pdf
SHRESTHKOKNE
 
filteration _ pre.pptx 11111110001.pptx
awasthivaibhav825
 
Water resources Engineering GIS KRT.pptx
Krunal Thanki
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
4 Tier Teamcenter Installation part1.pdf
VnyKumar1
 
ENSA_Module_7.pptx_wide_area_network_concepts
RanaMukherjee24
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
Precedence and Associativity in C prog. language
Mahendra Dheer
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
Online Cab Booking and Management System.pptx
diptipaneri80
 
AI-Driven IoT-Enabled UAV Inspection Framework for Predictive Maintenance and...
ijcncjournal019
 
All chapters of Strength of materials.ppt
girmabiniyam1234
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 

C Control Statements.docx

  • 1. C Control Statements For this purpose, C Programming uses the control statements to control the flow of a program. If the condition is satisfied the code followed by that condition will execute. If not simply that the code will be neglected by the compiler. Types of Control statements  if  if else  if-else-if control statement  switch() case control statement C if statement Why if Statement? All programming languages enable you to make decisions. They enable the program to follow a certain course of action depending on whether a particular condition is met. This is what gives programming language their intelligence. C if Syntax And Definition
  • 2.  C uses the keyword if to execute a set of statements when logical condition is true.  When the logical condition is false, the compiler simply skips the statement within the block.  The "if" statement is also known as one way decision statement. Syntax if statement Syntax if(a>10) a=20 { here goes statements; . . . . here goes statements; } if Statement Uses if statements are commonly used in following scenarios  Is A bigger than B?  Is X equal to Y?  Is M not equal to N? Realtime Time if Statement Uses realtimeif if(x = 123) digitalWrite(LEDpin, high) Note:
  • 3. x is a variable which get its input from a sensor continuously. if Statement Rules  The expression (or) condition is always enclosed within pair of parenthesis. e.g.) if ( a > b )  The if statement should not be terminated with a semicolon. If it happens, the block of statements will not execute even the condition is true.  The statements following the if condition is normally enclosed between 2 braces (in curly braces). if statement Flow Chart Following flow chart will clearly explain how if statement works if statement C Program ifstatement.c
  • 4. #include <stdio.h> //header file section #include <conio.h> int main() { int age = 18; if(age > 17) { printf("you are eligible for voting "); } printf("nThis is normal flow "); return 0; }  you are eligible for voting  This is normal flow Note: If the user input is greater than 17, then the condition will be true and the statements under if condition gets executed. Otherwise, it executes next to the if block C if-else Statement Why if-else Statement?  As We've seen, the if statement allows us to run a block of code if an expression evaluates to true.  If the expression evalutes to false, the code is skipped. Thus we can enhance this decision-making process by adding an else statement to an if construction.  This lets us run one block of code if an expression is true, and a different block of code if the expression is false. C if-else Syntax And Definition  C uses the keywords if and else to execute a set of statements either logical condition is true or false.
  • 5.  If the condition is true, the statements under the keyword if will be executed.  If the condition is false, the statements under the keyword else will be executed. Syntax if-else statement Syntax if(condition) { here go statements.... } else { here go statements.... } if else Statement Flow Chart Following flow chart will clearly explain how if else statement works
  • 6. Realtime Time if else Statement Uses Arduino microcontroller make use C programming, where you can alarm if your sensors output in greater than or lesser than expected value, else blink green light always. realtimeifelse if(x = 123) digitalWrite(LEDpin,high) else digitalWrite(LEDpin,low) Note: x is a variable which get its input from a sensor continuously. if-else C Program ifelsestatement.c
  • 7. #include <stdio.h> //header file section int main() { int age; printf("Enter your age : "); scanf("%d",&age); if(age > 17) { printf("nyou are eligible for voting "); } else { printf("nSorry, you are not eligible for voting "); } printf("nThis is normal flow "); return 0; }  Enter your age : 16  Sorry, you are not eligible for voting  This is normal flow Note: The user input for a variable 'age' is 16, the expression or condition tends to false. So, the else block gets executed. Realtime Time if else Statement Uses Arduino microcontroller make use C programming, where you can alarm if your sensors output in greater than or lesser than expected value, else blink green light always. realtimeifelse
  • 8. if(x = 123) digitalWrite(LEDpin,high) else digitalWrite(LEDpin,low) Note: x is a variable which get its input from a sensor continuously. if-else C Program ifelsestatement.c #include <stdio.h> //header file section int main() { int age; printf("Enter your age : "); scanf("%d",&age); if(age > 17) { printf("nyou are eligible for voting "); } else { printf("nSorry, you are not eligible for voting "); } printf("nThis is normal flow "); return 0; }  Enter your age : 16  Sorry, you are not eligible for voting
  • 9.  This is normal flow Note: The user input for a variable 'age' is 16, the expression or condition tends to false. So, the else block gets executed. Realtime Time if else Statement Uses Arduino microcontroller make use C programming, where you can alarm if your sensors output in greater than or lesser than expected value, else blink green light always. realtimeifelse if(x = 123) digitalWrite(LEDpin,high) else digitalWrite(LEDpin,low) Note: x is a variable which get its input from a sensor continuously. if-else C Program ifelsestatement.c #include <stdio.h> //header file section int main() { int age; printf("Enter your age : "); scanf("%d",&age); if(age > 17) { printf("nyou are eligible for voting ");
  • 10. } else { printf("nSorry, you are not eligible for voting "); } printf("nThis is normal flow "); return 0; }  Enter your age : 16  Sorry, you are not eligible for voting  This is normal flow Note: The user input for a variable 'age' is 16, the expression or condition tends to false. So, the else block gets executed. C Nested if Statement Why Nested if Statement The statement that is executed when an if expression is true can be another if, as can the statement in an else clause. This enables you to express such convoluted logic as "if age of Lingcoln is greater than age of john "and if age of Lingcoln is greater than age of renu". Then we decide Lingan is elder of all Syntax and Definition Nested if statement  In C programming, control statements like as if can be nested, that means we can write one within another.  If outer if statement fails, then the compiler skips the entire block irrespective of their inner if statement. Syntax nested if statement Syntax
  • 11. if(condition) { if(condition) { here goes statements; } } Nested if Statement Flowchart The following flowchart will clearly demonstrate how nested if statement works Program Nested if statement
  • 12. Let us write a C program using Nested if statement nestedif.c #include <stdio.h> //header file section int main() { int a = 47, b = 25, c = 3; if (a > b) { if (a > c) { printf("The value a is greater than b and c "); } } printf("nThis is normal flow "); return 0; }  The value a is greater than b and c  This is normal flow Note: When the first if statement executed, the value 'a' is compared with value 'b', if the expression is true, then inner if statement executed, the inner expression compares the value a with value c, if the inner if statement also true, the compiler will display the output. C Nested else if Statement In nested else if statement, the number of logical conditions can be checked for executing various statements.
  • 13.  If any logical condition is true the compiler executes the block under that else if condition, otherwise, it skips and executes else block.  By default else block will be executed, if all the remaining conditions are false.  It must be noted in nested else if statement, if more than one else if statement is true then the first else if statement which satisfies the condition in it will alone execute. C Nested else if Syntax Syntax Nested else if Statement Syntax if(test_expression 1) { here goes statements; } else if(test_expression 2) { here goes statements; } else { here goes statements; } else-if Flowchart
  • 14. The following flowchart will clearly demonstrate, how else-if works? else if Statement Program Lets take a look at the else if statement in action. nestedif.c #include <stdio.h> //header file section int main() { int a; printf("Enter a number: "); scanf ("%d ", &a); if(a > 0) { printf("n%d is a positive number", a);
  • 15. } else if(a < 0) { printf("n%d is a negative number", a); } else { printf("n%d is a zero",a); } return 0; }  Enter a number: -47  -47 is a negative number Note: Here, the compiler will ask user to enter the number, when user enters the number compiler will check if statement, as if statement resulted in false the compiler will check else if statement, as the condition is true compiler execute the block followed by else if statement and then exit. Swith case: A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case. Syntax The syntax for a switch statement in C programming language is as follows − switch(expression) { case constant-expression : statement(s); break; /* optional */ case constant-expression :
  • 16. statement(s); break; /* optional */ /* you can have any number of case statements */ default : /* Optional */ statement(s); } The following rules apply to a switch statement −  The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.  You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.  The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.  When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.  When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.  Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.  A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case. Flow Diagram
  • 17. Example #include <stdio.h> int main () { /* local variable definition */ char grade = 'B'; switch(grade) { case 'A' : printf("Excellent!n" ); break; case 'B' : case 'C' : printf("Well donen" ); break; case 'D' : printf("You passedn" ); break; case 'F' :
  • 18. printf("Better try againn" ); break; default : printf("Invalid graden" ); } printf("Your grade is %cn", grade ); return 0; } When the above code is compiled and executed, it produces the following result − Well done Your grade is B break, continue and goto statements The break; continue; and goto; statements are used to alter the normal flow of a program. Loops perform a set of repetitive task until text expression becomes false but it is sometimes desirable to skip some statement/s inside loop or terminate the loop immediately without checking the test expression. In such cases, break and continue statements are used. switch Here, we will write a C program to find the grade of a student using switch case statements. The below table shows the grading system. Score in subject Grade >=90 A 80-89 B 70-79 C
  • 19. 60-69 D 50-59 E <50 F C program to find grade of a student using switch case statement #include<stdio.h> int main() { int score; printf("Enter score( 0-100 ): "); scanf("%d", &score); switch( score / 10 ) { case 10: case 9: printf("Grade: A"); break; case 8: printf("Grade: B"); break; case 7: printf("Grade: C"); break; case 6: printf("Grade: D"); break; case 5: printf("Grade: E"); break; default: printf("Grade: F"); break;
  • 20. } return 0; } break statement In C programming, break statement is used with conditional if statement. The break is used in terminating the loop immediately after it is encountered. it is also used in switch...casestatement. which is explained in next topic. Syntax: break; The break statement can be used in terminating loops like for, while and do...while Example: //Write a C Program Which use of break statement. #include<stdio.h> #include<conio.h> void main(){ int num, sum=0;
  • 21. int i,n; printf("Note: Enter Zero for break loop!n"); printf("Enter Number of inputsn"); scanf("%d",&n); for(i=1;i<=n;++i){ printf("Enter num%d: ",i); scanf("%d",&num); if(num==0) { break; /*this breaks loop if num == 0 */ printf("Loop Breakedn"); } sum=sum+num; } printf("Total is %d",sum); getch(); } Note: Enter Zero for break loop! Enter Number of inputs 5 Enter num1: 5 Enter num2: 10 Enter num3: 0 Loop Breaked Total is 15
  • 22. continue statement It is sometimes desirable to skip some statements inside the loop. In such cases, continue statement is used. Syntax: continue; Just like break, continue is also used with conditional if statement. Example: //Write a C Program Which use of continue statment. #include<stdio.h> #include<conio.h> void main(){ int i, n=20; clrscr(); for(i=1;i<=n;++i){ if(i % 5 == 0) { printf("passn"); continue; /*this continue the execution of loop if i % 5 == 0 */ } printf("%dn",i); } getch();
  • 23. } Download Output: Command Prompt 1 2 3 4 pass 6 7 8 9 pass 11 12 13 14 pass 16 17 18 19 pass goto statement In C programming, goto statement is used for altering the normal sequence of program execution by transferring control to some other part of the program. Syntax: goto label; ............. ............. ............. label: statement;
  • 24. In this syntax, label is an identifier. When, the control of program reaches to goto statement, the control of the program will jump to the label: and executes the code below it. goto-statement Example: //Write a C Program Which Print 1 To 10 Number Using goto statement. #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); count: //This is Label printf("%dn",i); i++; if(i<=10) { goto count; //This jumps to label "count:" } getch(); } Output: Command Prompt 1
  • 25. 2 3 4 5 6 7 8 9 10 Note: Though goto statement is included in ANSI standard of C, use of goto statement should be reduced as much as possible in a program. Reasons to avoid goto statement Though, using goto statement give power to jump to any part of program, using goto statement makes the logic of the program complex and tangled. In modern programming, goto statement is considered a harmful construct and a bad programming practice. The goto statement can be replaced in most of C program with the use of break and continue statements. In fact, any program in C programming can be perfectly written without the use of goto statement. All programmer should try to avoid goto statement as possible as they can.