21

Control structures in c

Embed Size (px)

Citation preview

Page 1: Control structures in c
Page 2: Control structures in c

Reshma Raju

[email protected]

Reshma chippykutty

twitter.com/username

in.linkedin.com/in/profilename

8547829221

CONTROL STRUCTURE IN C

Page 3: Control structures in c

Disclaimer: This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocument of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd

Page 4: Control structures in c

CONTROL STRUCTURE IN C

The control structure is any mechanism that departs from thedefault straight-line execution.

The instructions, statements or group of statements in aprogramming language which determines the sequence ofexecution of other instructions or statements.

Page 5: Control structures in c

if statement

The if statement is a decision making statement.

It is used to control the flow of execution and also used to test logically whether the condition is true or false.

Syntax:-

If(condition)

{

Statement;

}

Page 6: Control structures in c

Example for if statement

#include<stdio.h> OUTPUT

void main() Enter the number :9

{ The entered number 9 is less than 10

int i;

printf("Enter the number :");

scanf("%d",&i);

if(i<10)

{

printf("The entered number %d is less than 10",i);

}

}

Page 7: Control structures in c

If…..else statement

The if....else statement is an extension of the simple if statement.

The syntax is:

if (condition){True-block statement;}else{False-block statement;}

If the condition is true, then the true-block statements are executed; otherwise the false-block statement are executed.

Page 8: Control structures in c

Example for if….else statement

#include<stdio.h> OUTPUT

void main() Enter the number :50

{

int i; The entered number 50 is greater than 10

printf("Enter the number :");

scanf("%d",&i);

if(i<10)

{

printf(“\nThe entered number %d is less than 10\n",i);

}

else

{

printf(“\nThe entered number %d is greater than 10\n",i);

}

}

Page 9: Control structures in c

Nested If-else statement

The nested if...else statement is used when program requires more than one test expression.

The syntax is:if (test expression1)

{ statement to be executed if test expression1 is true;

}

else if(test expression2)

{statement to be executed if test expression1 is false and 2 is true;

}

else if (test expression 3)

{ statement to be executed if text expression1 and 2 are false and 3 is true;

}

.

.

else

{ statements to be executed if all test expressions are false;

}

Page 10: Control structures in c

Example for Nested if-else statement

#include <stdio.h> OUTPUT

int main() Enter two integers :40 50

{ Result: 50>40

int numb1, numb2;

printf("Enter two integers :");

scanf("%d %d",&numb1,&numb2);

if(numb1==numb2) //checking whether two integers are equal.

printf("Result: %d = %d",numb1,numb2);

else

if(numb1>numb2) //checking whether numb1 is greater than numb2.

printf("Result: %d > %d",numb1,numb2);

else

printf("Result: %d > %d",numb2,numb1);

}

Page 11: Control structures in c

Switch statement

The switch statement evaluates an expression, then attempts to match the result to one of several possible cases.

Each case contains a value and a list of statements

Often a break statement is used as the last statement in each case's statement list

A break statement causes control to transfer to the end of the switch statement

If a break statement is not used, the flow of control will continue into the next case

Page 12: Control structures in c

Example for switch statement#include<stdio.h>

void main()

{

int int_i; // declare the variable int_i

printf("\n 1.Play game \n");

printf("\n 2.Load game \n");

printf("\n 3.Play multiplayer \n");

printf("\n 4.Exit \n");

printf("\n ENTER YOUR CHOICE:");

scanf("\n %d",&int_i); //store the integer variable

switch(int_i) // select the switch case statement

{

case 1:

printf("\n WELCOME PLAYER");

break; // break the case 1

Page 13: Control structures in c

case 2:

printf("\n WAIT WHILE THE GAME IS LOADING");

break;

case 3:

printf("\n CONNECT YOUR PARTNERS") ; OUTPUT

break; 1. Play Game

case 4: 2. Load Game

exit(0); //exit the function 3. Play Multiplayer4. Exit

default: ENTER YOUR CHOICE:2

printf("\n INVALID") ;

break; WAIT WHILE THE GAME IS LOADING

}

}

Page 14: Control structures in c

Ternary condition

Syntax :

expression 1 ? expression 2 : expression 3

expression1 is Condition

expression2 is Statement Followed if Condition is True

expression2 is Statement Followed if Condition is False

Page 15: Control structures in c

Example for ternary condition

#include<stdio.h>

int main()

{

int num;

printf("Enter the Number : ");

scanf("%d",&num);

(num%2==0)?printf("Even"):printf("Odd");

}

OUTPUT

Enter the number : 5

Odd

Page 16: Control structures in c

Break statement break statement is used to exit from a loop or a switch, control

passing to the first statement beyond the loop or a switch.

Example for break statement#include<stdio.h>

void main()

{

int i; OUTPUT

for(i=0;i<=10;i++)

{ 0 1 2 3 4

if(i==5)

{

break;

}

printf(" %d",i);

}

}

Page 17: Control structures in c

Continue statement continue is similar to the break statement but it only works

within loops where its effect is to force an immediate jump to the loop control statement.

Example for continue statement#include<stdio.h>

void main()

{

int i; OUTPUT

for(i=0;i<10;i++)

{ 0 1 2 3 4 5 6 7 8 9

if(i==5)

{

continue;

}

printf(" %d",i);

}

}

Page 18: Control structures in c

THANK YOU

Page 19: Control structures in c

Want to learn more about programming or Looking to become a good programmer?

Are you wasting time on searching so many contents online?

Do you want to learn things quickly?

Tired of spending huge amount of money to become a Software professional?

Do an online course @ baabtra.com

We put industry standards to practice. Our structured, activity based courses are so designedto make a quick, good software professional out of anybody who holds a passion for coding.

Page 20: Control structures in c

Follow us @ twitter.com/baabtra

Like us @ facebook.com/baabtra

Subscribe to us @ youtube.com/baabtra

Become a follower @ slideshare.net/BaabtraMentoringPartner

Connect to us @ in.linkedin.com/in/baabtra

Give a feedback @ massbaab.com/baabtra

Thanks in advance

www.baabtra.com | www.massbaab.com |www.baabte.com

Page 21: Control structures in c

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Cafit Square,Hilite Business Park,Near Pantheerankavu,Kozhikode

Start up VillageEranakulam,Kerala, India.

Email: [email protected]

Contact Us