if,loop,switch
TRANSCRIPT
![Page 1: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/1.jpg)
![Page 2: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/2.jpg)
Disclaimer:This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
![Page 4: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/4.jpg)
Control Statement• Used in the C language to make decisions and
control the flow of the program.• It decide the flow of a program.
![Page 5: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/5.jpg)
They are decision making statements
• If statement• If-else statement• Switch case
![Page 6: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/6.jpg)
If Statements
Syntax:
if (condition) { statement(s);}
![Page 7: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/7.jpg)
![Page 8: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/8.jpg)
#include <stdio.h>
int main()
{
int num;
printf("Enter a number to check.\n");
scanf("%d",&num);
if(num>5)
printf("numbur greater than 5");
return 0;
}
![Page 9: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/9.jpg)
![Page 10: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/10.jpg)
If statement cont..
• Adding a semicolon at the end of an if clause is a common mistake.• This mistake is hard to find, because it is not a compilation error or a runtime error, it is a logic error.
![Page 11: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/11.jpg)
if…else StatementSyntax: if(condition) { true statements; } else { false statements; }
![Page 12: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/12.jpg)
Flow chart:
![Page 13: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/13.jpg)
if...else Example#include <stdio.h>main(){
int no;printf("\n Enter Number :");scanf("%d",&no);if(no%2==0)
printf("Number is even !");else
printf("\n\n Number is odd !");return 0;
}
![Page 14: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/14.jpg)
![Page 15: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/15.jpg)
switch Statement• This is a multiple or multiway brancing decision
making statement.• Similar to if statements• Can list any number of branches• Used in place of nested if statements• Avoids confusion of deeply nested ifs• Easy to find out errors.
![Page 16: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/16.jpg)
![Page 17: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/17.jpg)
Switch flowchart
Expression?
Statement(s) Statement(s) Statement(s)Statement(s)Statement(s)
value1 value2 value3 value4 Value n
entry
![Page 18: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/18.jpg)
#include <stdio.h>main(){
int no;printf("Enter any number from 1 to 3 :");scanf("%d",&no);switch(no){
case 1:printf("It is 1 !");break;
case 2:printf("It is 2 !");break;
case 3:printf("It is 3 !");break;
default:printf("Invalid number !");
}getch();}
![Page 19: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/19.jpg)
![Page 20: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/20.jpg)
Looping statement
• It uses an expression that evaluates to either TRUE or FALSE
• Used to determine when an action should be repeatedly performed in a program.
1. do loop 2. while loop 3. for loop statements.
![Page 21: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/21.jpg)
while Loop
It repeatedly executes a target statement as long as a given condition is true.
Syntax: while(condition) { statements; }
![Page 22: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/22.jpg)
Flow chart
![Page 23: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/23.jpg)
#include <stdio.h> int main (){ /* local variable definition */ int a = 10;
/* while loop execution */ while( a < 20 ) { printf("value of a: %d\n", a); a++; } return 0;}
![Page 24: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/24.jpg)
![Page 25: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/25.jpg)
do-while LoopSyntax;
do {
statements
}
while (condition);
![Page 26: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/26.jpg)
![Page 27: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/27.jpg)
#include <stdio.h> int main () {
/* local variable definition */ int a = 10;
/* do loop execution */ do { printf("value of a: %d\n", a); a = a + 1; }while( a < 20 );return 0; }
![Page 28: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/28.jpg)
![Page 29: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/29.jpg)
For Loop • It is a repetition control structure that allows
you to efficiently write a loop that needs to execute a specific number of times.
• Easy to use• Highly flexible• More than one variable can be initilized.• More than one increments can be applied.
![Page 30: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/30.jpg)
Syntax:for ( init; condition; increment ) { statement(s); }
![Page 31: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/31.jpg)
Flow chart:
![Page 32: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/32.jpg)
for Loop Example#include <stdio.h>int main(){int a;for (a=0; a<5; a++){printf("Baabtra\n");}return 0;}
![Page 33: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/33.jpg)
![Page 34: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/34.jpg)
• If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance.
• www.baabtra.com | www.massbaab.com |
www.baabte.com
![Page 35: if,loop,switch](https://reader035.vdocuments.us/reader035/viewer/2022070316/555a6063d8b42a47748b530a/html5/thumbnails/35.jpg)
Thank you