SlideShare a Scribd company logo
Chapter 3:
Control Flow/ Structure
PREPARED BY: MS. SA SOKNGIM
Content
1. Decision Making
2. Loops
3. Break and Continue Statement
4. Switch… case Statement
5. goto and label Statement
1. Decision Making
 Decision making is used to
specify the order in which
statements are executed.
• Decision making in a C program using:
• if statement
• if…else statement
• if…else if…else statement
• nested if...else statement
• Switch case Statement
1.1 if statement
if (testExpression)
{
// statements
}
Example: if statement
// Program to display a number if user enters negative number
// If user enters positive number, that number won't be displayed
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// Test expression is true if number is less than 0
if (number < 0) {
printf("You entered %d.n", number);
}
printf("The if statement is easy.");
return 0;
}
1.2 if...else statement
 The if...else statement executes some code if the test expression
is true (nonzero) and some other code if the test expression is
false (0).
Syntax of if...else
if (testExpression) {
// codes inside the body of if
}else {
// codes inside the body of else
}
Example: if...else statement
// Program to check whether an integer entered by the user is odd or even
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d",&number);
// True if remainder is 0
if( number%2 == 0 )
printf("%d is an even integer.",number);
else
printf("%d is an odd integer.",number);
return 0;
}
1.3 if...else if....else Statement
 The if...else statement executes two different codes
depending upon whether the test expression is true or false.
Sometimes, a choice has to be made from more than 2
possibilities.
 The if...else if…else statement allows you to check for
multiple test expressions and execute different codes for
more than two conditions.
Syntax of if...else if....else statement.
if (testExpression1) {
// statements to be executed if testExpression1 is true
} else if(testExpression2) {
// statements to be executed if testExpression1 is false and
testExpression2 is true
} else if (testExpression 3) {
// statements to be executed if testExpression1 and
testExpression2 is false and testExpression3 is true
} else {
// statements to be executed if all test expressions are false
}
Example: if...else if....else statement
// Program to relate two integers
using =, > or <
#include <stdio.h>
int main(){
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1,
&number2);
//checks if two integers are
equal.
if(number1 == number2){
printf("Result: %d = %d“,
number1,number2);
}
//checks if number1 is greater than
number2.
else if (number1 > number2) {
printf("Result: %d > %d",
number1, number2);
}
// if both test expression is false
else {
printf("Result: %d < %d",
number1, number2);
}
return 0;
}
1.4 Nested if else statement
 Nested if else statement is same like if else
statement, where new block of if else statement is
defined in existing if or else block statement.
 Used when user want to check more than one
conditions at a time.
Syntax of Nested If else Statement
if(condition is true){
if(condition is true){
statement;
}else{
statement;
}
}else{
statement;
}
Example of Nested if else Statement
#include <stdio.h>
void main(){
char username;
int password;
printf("Username:");
scanf("%c",&username);
printf("Password:");
scanf("%d",&password);
if(username=='a'){
if(password==12345){
printf("Login successful");
}else{
printf("Password is incorrect, Try again.");
}
}else{
printf("Username is incorrect, Try again.");
}
}
2. Loops
 Loops are used in programming to repeat a specific block until some end
condition is met.
 There are three loops in C programming:
o for loop
o while loop
o do...while loop
o Nested loops
2.1 for Loop
 The syntax of a for loop is:
for (initializationStatement; testExpression; updateStatement)
{
// codes
}
for loop Flowchart
Example: for loop
// Program to calculate the sum of
first n natural numbers
// Positive integers 1,2,3...n are
known as natural numbers
#include <stdio.h>
int main(){
int n, count, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
// for loop terminates when n is less
than count
for(count = 1; count <= n; ++count)
{
sum += count;
}
printf("Sum = %d", sum);
return 0;
}
2.2 while loop
 The syntax of a while loop is:
while (testExpression)
{
//codes
}
Example: while loop
/ Program to find factorial of
a number
// For a positive integer n,
factorial = 1*2*3...n
#include <stdio.h>
int main(){
int number;
long factorial;
printf("Enter an integer: ");
scanf("%d",&number);
factorial = 1;
// loop terminates when number is less
than or equal to 0
while (number > 0) {
// factorial = factorial*number;
factorial *= number;
--number;
}
printf("Factorial= %lld", factorial);
return 0;
}
2.3 do...while loop
 The do..while loop is similar to the while loop with one
important difference.
 The body of do...while loop is executed once, before
checking the test expression.
 The do...while loop is executed at least once.
do...while loop Syntax
The syntax of a do while loop is:
do
{
// codes
}
while (testExpression);
Example: do...while loop
// Program to add numbers until user enters zero
#include <stdio.h>
int main() {
double number, sum = 0;
// loop body is executed at least once
do{
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}while(number != 0.0);
printf("Sum = %.2lf",sum);
return 0;
}
2.4 Nested loops
 C programming allows to use one loop inside another
loop.
 Syntax for loop
for ( init; condition; increment ) {
for ( init; condition; increment ) {
statement(s);
}
statement(s);
}
2.4 Nested loops (Con)
 Syntax while loop
while(condition) {
while(condition) {
statement(s);
}
statement(s);
}
2.4 Nested loops (Con)
 Syntax do while loop
do {
statement(s);
do {
statement(s);
}while( condition );
}while( condition );
Example of Nested Loops
#include <stdio.h>
int main()
{
int n, c, k;
printf("Enter number of rowsn");
scanf("%d",&n);
for ( c = 1 ; c <= n ; c++ ){
for( k = 1 ; k <= c ; k++ )
printf("*");
printf("n");
}
return 0;
}
3. Break And Continue Statement
 What is BREAK meant?
 What is CONTINUE meant?
3.1 Break Statement
 The break statement terminates the loop immediately when
it is encountered.
 The break statement is used with decision making
statement such as if...else.
 Syntax of break statement
break;
Flowchart Of Break Statement
How break statement works?
Example: break statement
// Program to calculate the sum
of maximum of 10 numbers
// Calculates sum until user
enters positive number
# include <stdio.h>
int main() {
int i;
double number, sum = 0.0;
for(i=1; i <= 10; ++i) {
printf("Enter a n%d: ",i);
scanf("%lf",&number);
// If user enters negative
number, loop is terminated
if(number < 0.0) {
break;
}
// sum = sum + number;
sum += number;
}
printf("Sum = %.2lf",sum);
return 0;
}
3.2 Continue Statement
 The continue statement skips some statements inside the loop.
 The continue statement is used with decision making statement
such as if...else.
 Syntax of continue Statement
continue;
Flowchart of Continue Statement
How Continue Statement Works?
Example: continue statement
// Program to calculate sum of
maximum of 10 numbers
// Negative numbers are skipped
from calculation
# include <stdio.h>
int main(){
int i;
double number, sum = 0.0;
for(i=1; i <= 10; ++i) {
printf("Enter a n%d: ",i);
scanf("%lf",&number);
// If user enters negative
number, loop is terminated
if(number < 0.0) {
continue;
}
// sum = sum + number;
sum += number;
}
printf("Sum = %.2lf",sum);
return 0;
}
4. Switch...Case Statement
 The if...else if…else statement allows you to execute a block code among
many alternatives. If you are checking on the value of a single variable in
if...else if…else statement, it is better to use switch statement.
 The switch statement is often faster than nested if...else (not always).
Also, the syntax of switch statement is cleaner and easy to understand.
Syntax of switch...case
switch (n){
case constant1:
// code to be executed if n is equal to constant1;
break;
case constant2:
// code to be executed if n is equal to constant2;
break;
.
.
.
default:
// code to be executed if n doesn't match any constant
}
Switch Statement Flowchart
Example: switch Statement
// Program to create a simple calculator
// Performs addition, subtraction, multiplication or division
depending the input from user
# include <stdio.h>
int main() {
char operator;
double firstNumber,secondNumber;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf",&firstNumber, &secondNumber);
switch(operator) {
case '+':
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber+secondNumber);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-secondNumber);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber*secondNumber);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber);
break;
// operator is doesn't match any case constant (+, -, *, /)
default:
printf("Error! operator is not correct");
}
return 0; }
5. goto Statement
 The goto statement is used to alter the normal sequence of a C
program.
Syntax of goto Statement
goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
What is Label?
 The label is an identifier. When goto statement is encountered,
control of the program jumps to label: and starts executing the code.
Example: goto Statement
// Program to calculate the sum and average of maximum of 5
numbers
// If user enters negative number, the sum and average of
previously entered positive number is displayed
# include <stdio.h>
int main(){
const int maxInput = 5;
int i;
double number, average, sum=0.0;
for(i=1; i<=maxInput; ++i){
printf("%d. Enter a number: ", i);
scanf("%lf",&number);
// If user enters negative number, flow of program moves to label
jump
if(number < 0.0)
goto jump;
sum += number; // sum = sum+number;
}
jump:
average=sum/(i-1);
printf("Sum = %.2fn", sum);
printf("Average = %.2f", average);
return 0;
}
Example: goto Statement
C Programming: Control Structure

More Related Content

PPTX
Control statements in c
PPT
Decision making and branching
PPT
RECURSION IN C
PPTX
Loops in c programming
PDF
Unit ii chapter 2 Decision making and Branching in C
PPTX
Decision Making Statement in C ppt
Control statements in c
Decision making and branching
RECURSION IN C
Loops in c programming
Unit ii chapter 2 Decision making and Branching in C
Decision Making Statement in C ppt

What's hot (20)

PPTX
PPTX
Unit 3. Input and Output
PDF
Operators in c programming
PPT
Strings
PPTX
Functions in c language
PPTX
Presentation on C Switch Case Statements
PPT
FUNCTIONS IN c++ PPT
PPTX
Loops in C Programming Language
PDF
Introduction to c++ ppt
PPT
Pointers C programming
PPTX
Loops c++
PPTX
Conditional and control statement
PPTX
Input output statement in C
PPT
PPTX
Pointers in C Programming
PPT
Constants in C Programming
PPTX
Dynamic memory allocation in c
PPTX
Pointer in c
PPTX
Polymorphism in c++(ppt)
PPT
Function overloading(c++)
Unit 3. Input and Output
Operators in c programming
Strings
Functions in c language
Presentation on C Switch Case Statements
FUNCTIONS IN c++ PPT
Loops in C Programming Language
Introduction to c++ ppt
Pointers C programming
Loops c++
Conditional and control statement
Input output statement in C
Pointers in C Programming
Constants in C Programming
Dynamic memory allocation in c
Pointer in c
Polymorphism in c++(ppt)
Function overloading(c++)
Ad

Viewers also liked (18)

PPT
Control Structures
PPTX
disjoint-set data structures
PPT
Structure in programming in c or c++ or c# or java
PDF
8 statement-level control structure
PDF
Decision models
PPT
Chapter20 capital structure_decision
PPT
Control structure
PPT
Control Structure in C
PPTX
C LANGUAGE - BESTECH SOLUTIONS
PPTX
Classical model of decision making
PPT
Managerial Decision Making
PPTX
Function in C program
PPTX
Decision Making In Management
PPSX
INTRODUCTION TO C PROGRAMMING
PDF
Control structures in Java
PDF
Decision making
PDF
Lecture18 structurein c.ppt
Control Structures
disjoint-set data structures
Structure in programming in c or c++ or c# or java
8 statement-level control structure
Decision models
Chapter20 capital structure_decision
Control structure
Control Structure in C
C LANGUAGE - BESTECH SOLUTIONS
Classical model of decision making
Managerial Decision Making
Function in C program
Decision Making In Management
INTRODUCTION TO C PROGRAMMING
Control structures in Java
Decision making
Lecture18 structurein c.ppt
Ad

Similar to C Programming: Control Structure (20)

PPTX
C programming Control Structure.pptx
PPTX
C decision making and looping.
PDF
4_Decision Making and Branching in C Program.pdf
PPTX
Control structure of c
PPTX
Condition Stmt n Looping stmt.pptx
PDF
Unit 2=Decision Control & Looping Statements.pdf
PPTX
control_structures_c_language_regarding how to represent the loop in language...
PDF
Module 2 - Control Structures c programming.pptm.pdf
PPTX
C Unit-2.ppt
PPT
2. Control structures with for while and do while.ppt
PPTX
Bsc cs pic u-3 handling input output and control statements
PPTX
CONTROL FLOW in C.pptx
PDF
Control structure and Looping statements
PPTX
C Programming Control Structures(if,if-else)
PPTX
Diploma ii cfpc u-3 handling input output and control statements
PPTX
Mca i pic u-3 handling input output and control statements
PDF
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PPTX
Btech i pic u-3 handling input output and control statements
PDF
Controls & Loops in C
C programming Control Structure.pptx
C decision making and looping.
4_Decision Making and Branching in C Program.pdf
Control structure of c
Condition Stmt n Looping stmt.pptx
Unit 2=Decision Control & Looping Statements.pdf
control_structures_c_language_regarding how to represent the loop in language...
Module 2 - Control Structures c programming.pptm.pdf
C Unit-2.ppt
2. Control structures with for while and do while.ppt
Bsc cs pic u-3 handling input output and control statements
CONTROL FLOW in C.pptx
Control structure and Looping statements
C Programming Control Structures(if,if-else)
Diploma ii cfpc u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statements
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
Btech i pic u-3 handling input output and control statements
Controls & Loops in C

More from Sokngim Sa (9)

PPTX
06 UI Layout
PPTX
How to decompile apk
PPTX
05 intent
PPTX
04 activities and activity life cycle
PPTX
03 android application structure
PPTX
02 getting start with android app development
PPTX
01 introduction to android
PPTX
Add eclipse project with git lab
PPTX
Transmitting network data using volley(14 09-16)
06 UI Layout
How to decompile apk
05 intent
04 activities and activity life cycle
03 android application structure
02 getting start with android app development
01 introduction to android
Add eclipse project with git lab
Transmitting network data using volley(14 09-16)

Recently uploaded (20)

PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Classroom Observation Tools for Teachers
PPTX
Cell Types and Its function , kingdom of life
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Pre independence Education in Inndia.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
STATICS OF THE RIGID BODIES Hibbelers.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
Microbial diseases, their pathogenesis and prophylaxis
Classroom Observation Tools for Teachers
Cell Types and Its function , kingdom of life
Week 4 Term 3 Study Techniques revisited.pptx
human mycosis Human fungal infections are called human mycosis..pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Supply Chain Operations Speaking Notes -ICLT Program
O5-L3 Freight Transport Ops (International) V1.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Pre independence Education in Inndia.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPH.pptx obstetrics and gynecology in nursing

C Programming: Control Structure

  • 1. Chapter 3: Control Flow/ Structure PREPARED BY: MS. SA SOKNGIM
  • 2. Content 1. Decision Making 2. Loops 3. Break and Continue Statement 4. Switch… case Statement 5. goto and label Statement
  • 3. 1. Decision Making  Decision making is used to specify the order in which statements are executed. • Decision making in a C program using: • if statement • if…else statement • if…else if…else statement • nested if...else statement • Switch case Statement
  • 4. 1.1 if statement if (testExpression) { // statements }
  • 5. Example: if statement // Program to display a number if user enters negative number // If user enters positive number, that number won't be displayed #include <stdio.h> int main() { int number; printf("Enter an integer: "); scanf("%d", &number); // Test expression is true if number is less than 0 if (number < 0) { printf("You entered %d.n", number); } printf("The if statement is easy."); return 0; }
  • 6. 1.2 if...else statement  The if...else statement executes some code if the test expression is true (nonzero) and some other code if the test expression is false (0). Syntax of if...else if (testExpression) { // codes inside the body of if }else { // codes inside the body of else }
  • 7. Example: if...else statement // Program to check whether an integer entered by the user is odd or even #include <stdio.h> int main() { int number; printf("Enter an integer: "); scanf("%d",&number); // True if remainder is 0 if( number%2 == 0 ) printf("%d is an even integer.",number); else printf("%d is an odd integer.",number); return 0; }
  • 8. 1.3 if...else if....else Statement  The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities.  The if...else if…else statement allows you to check for multiple test expressions and execute different codes for more than two conditions.
  • 9. Syntax of if...else if....else statement. if (testExpression1) { // statements to be executed if testExpression1 is true } else if(testExpression2) { // statements to be executed if testExpression1 is false and testExpression2 is true } else if (testExpression 3) { // statements to be executed if testExpression1 and testExpression2 is false and testExpression3 is true } else { // statements to be executed if all test expressions are false }
  • 10. Example: if...else if....else statement // Program to relate two integers using =, > or < #include <stdio.h> int main(){ int number1, number2; printf("Enter two integers: "); scanf("%d %d", &number1, &number2); //checks if two integers are equal. if(number1 == number2){ printf("Result: %d = %d“, number1,number2); } //checks if number1 is greater than number2. else if (number1 > number2) { printf("Result: %d > %d", number1, number2); } // if both test expression is false else { printf("Result: %d < %d", number1, number2); } return 0; }
  • 11. 1.4 Nested if else statement  Nested if else statement is same like if else statement, where new block of if else statement is defined in existing if or else block statement.  Used when user want to check more than one conditions at a time.
  • 12. Syntax of Nested If else Statement if(condition is true){ if(condition is true){ statement; }else{ statement; } }else{ statement; }
  • 13. Example of Nested if else Statement #include <stdio.h> void main(){ char username; int password; printf("Username:"); scanf("%c",&username); printf("Password:"); scanf("%d",&password); if(username=='a'){ if(password==12345){ printf("Login successful"); }else{ printf("Password is incorrect, Try again."); } }else{ printf("Username is incorrect, Try again."); } }
  • 14. 2. Loops  Loops are used in programming to repeat a specific block until some end condition is met.  There are three loops in C programming: o for loop o while loop o do...while loop o Nested loops
  • 15. 2.1 for Loop  The syntax of a for loop is: for (initializationStatement; testExpression; updateStatement) { // codes }
  • 17. Example: for loop // Program to calculate the sum of first n natural numbers // Positive integers 1,2,3...n are known as natural numbers #include <stdio.h> int main(){ int n, count, sum = 0; printf("Enter a positive integer: "); scanf("%d", &n); // for loop terminates when n is less than count for(count = 1; count <= n; ++count) { sum += count; } printf("Sum = %d", sum); return 0; }
  • 18. 2.2 while loop  The syntax of a while loop is: while (testExpression) { //codes }
  • 19. Example: while loop / Program to find factorial of a number // For a positive integer n, factorial = 1*2*3...n #include <stdio.h> int main(){ int number; long factorial; printf("Enter an integer: "); scanf("%d",&number); factorial = 1; // loop terminates when number is less than or equal to 0 while (number > 0) { // factorial = factorial*number; factorial *= number; --number; } printf("Factorial= %lld", factorial); return 0; }
  • 20. 2.3 do...while loop  The do..while loop is similar to the while loop with one important difference.  The body of do...while loop is executed once, before checking the test expression.  The do...while loop is executed at least once.
  • 21. do...while loop Syntax The syntax of a do while loop is: do { // codes } while (testExpression);
  • 22. Example: do...while loop // Program to add numbers until user enters zero #include <stdio.h> int main() { double number, sum = 0; // loop body is executed at least once do{ printf("Enter a number: "); scanf("%lf", &number); sum += number; }while(number != 0.0); printf("Sum = %.2lf",sum); return 0; }
  • 23. 2.4 Nested loops  C programming allows to use one loop inside another loop.  Syntax for loop for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); }
  • 24. 2.4 Nested loops (Con)  Syntax while loop while(condition) { while(condition) { statement(s); } statement(s); }
  • 25. 2.4 Nested loops (Con)  Syntax do while loop do { statement(s); do { statement(s); }while( condition ); }while( condition );
  • 26. Example of Nested Loops #include <stdio.h> int main() { int n, c, k; printf("Enter number of rowsn"); scanf("%d",&n); for ( c = 1 ; c <= n ; c++ ){ for( k = 1 ; k <= c ; k++ ) printf("*"); printf("n"); } return 0; }
  • 27. 3. Break And Continue Statement  What is BREAK meant?  What is CONTINUE meant?
  • 28. 3.1 Break Statement  The break statement terminates the loop immediately when it is encountered.  The break statement is used with decision making statement such as if...else.  Syntax of break statement break;
  • 29. Flowchart Of Break Statement
  • 31. Example: break statement // Program to calculate the sum of maximum of 10 numbers // Calculates sum until user enters positive number # include <stdio.h> int main() { int i; double number, sum = 0.0; for(i=1; i <= 10; ++i) { printf("Enter a n%d: ",i); scanf("%lf",&number); // If user enters negative number, loop is terminated if(number < 0.0) { break; } // sum = sum + number; sum += number; } printf("Sum = %.2lf",sum); return 0; }
  • 32. 3.2 Continue Statement  The continue statement skips some statements inside the loop.  The continue statement is used with decision making statement such as if...else.  Syntax of continue Statement continue;
  • 35. Example: continue statement // Program to calculate sum of maximum of 10 numbers // Negative numbers are skipped from calculation # include <stdio.h> int main(){ int i; double number, sum = 0.0; for(i=1; i <= 10; ++i) { printf("Enter a n%d: ",i); scanf("%lf",&number); // If user enters negative number, loop is terminated if(number < 0.0) { continue; } // sum = sum + number; sum += number; } printf("Sum = %.2lf",sum); return 0; }
  • 36. 4. Switch...Case Statement  The if...else if…else statement allows you to execute a block code among many alternatives. If you are checking on the value of a single variable in if...else if…else statement, it is better to use switch statement.  The switch statement is often faster than nested if...else (not always). Also, the syntax of switch statement is cleaner and easy to understand.
  • 37. Syntax of switch...case switch (n){ case constant1: // code to be executed if n is equal to constant1; break; case constant2: // code to be executed if n is equal to constant2; break; . . . default: // code to be executed if n doesn't match any constant }
  • 39. Example: switch Statement // Program to create a simple calculator // Performs addition, subtraction, multiplication or division depending the input from user # include <stdio.h> int main() { char operator; double firstNumber,secondNumber; printf("Enter an operator (+, -, *,): "); scanf("%c", &operator); printf("Enter two operands: "); scanf("%lf %lf",&firstNumber, &secondNumber);
  • 40. switch(operator) { case '+': printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber+secondNumber); break; case '-': printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-secondNumber); break; case '*': printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber*secondNumber); break; case '/': printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber); break; // operator is doesn't match any case constant (+, -, *, /) default: printf("Error! operator is not correct"); } return 0; }
  • 41. 5. goto Statement  The goto statement is used to alter the normal sequence of a C program.
  • 42. Syntax of goto Statement goto label; ... .. ... ... .. ... ... .. ... label: statement;
  • 43. What is Label?  The label is an identifier. When goto statement is encountered, control of the program jumps to label: and starts executing the code.
  • 44. Example: goto Statement // Program to calculate the sum and average of maximum of 5 numbers // If user enters negative number, the sum and average of previously entered positive number is displayed # include <stdio.h> int main(){ const int maxInput = 5; int i; double number, average, sum=0.0; for(i=1; i<=maxInput; ++i){ printf("%d. Enter a number: ", i); scanf("%lf",&number);
  • 45. // If user enters negative number, flow of program moves to label jump if(number < 0.0) goto jump; sum += number; // sum = sum+number; } jump: average=sum/(i-1); printf("Sum = %.2fn", sum); printf("Average = %.2f", average); return 0; } Example: goto Statement