/
Control Structures Control Structures

Control Structures - PowerPoint Presentation

giovanna-bartolotta
giovanna-bartolotta . @giovanna-bartolotta
Follow
402 views
Uploaded On 2016-04-03

Control Structures - PPT Presentation

Contents Decision making statements if statement ifelse statement Nested ifelse statement if elseif else Statement Selection statements SwitchCase Statement Iteration statements ID: 273588

statements statement number expression statement statements expression number test num printf int loop executed break program sum syntax true user code enter

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Control Structures" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Control StructuresSlide2

Contents

Decision making statements

if statement

if...else statement

Nested if...else statement (if...

elseif

....else Statement)

Selection statements

Switch-Case Statement

Iteration statements

for Loop

While loop

Do-while Loop

Jump statements

Break Statement

Continue Statement

Slide3

Control

Structures

Control

flow

  refers to the order in which the individual statements ,  instructions or functioncalls of an imperative or declarative program are executed or evaluated.There are four types of control statements in C:Decision making statementsSelection statementsIteration statementsJump statements Decision making statements : The if-else statement , nested if and if-else statements is used to carry out a logical test and then take one of two possible actions depending on the outcome of the test .if statement syntax if (test expression) { statement/s to be executed if test expression is true; }

BackSlide4

If the test expression is true then, statements for the body if,

i.e

, statements inside parenthesis are executed. But, if the test expression is false, the execution of the statements for the body of if statements are skipped. Write a C program to print the number entered by user only if the number entered is negative.

#include <stdio.h> int main(){ int num; printf("Enter a number to check.\n"); scanf("%d",&num); if(num<0) /* checking whether number is less than 0 or not. */printf("Number=%d\n",num); printf("The if statement in C programming is easy."); return 0; }Enter a number to check. -2 Number=-2 The if statement in C programming is easy. BackSlide5

if...else statement :

The if...else statement is used, if the programmer wants to execute some code, if the test expression is true and execute some other code if the test expression is false.

Syntax of if...else

if (test expression) statements to be executed if test expression is true; else statements to be executed if test expression is false;Slide6

Write a C program to check whether a number entered by user is even or odd

#include <

stdio.h

>int main()

{ int num; printf("Enter a number you want to check.\n"); scanf("%d",&num); if((num%2)==0) printf("%d is even.",num); else printf("%d is odd.",num); return 0; }BackSlide7

Enter a number you want to check.

25

25 is odd.

Nested if...else statement (if...elseif....else Statement)

The if...else statement can be used in nested form when a serious decision are involved.Syntax of nested if...else statementif (test expression) statements to be executed if test expression is true; else if(test expression 1) statements to be executed if test expressions 1 is true; else if (test expression 2) . . . else statements to be executed if all test expressions are false; BackSlide8

Selection Statement: Switch-Case Statement

:

A switch statement is used for 

multiple way selections that will branch into different code segments based on the value of a variable or expression. This expression or variable must be of integer data type.

Syntax:switch (expression) { case value1: code segment1; break; case value2: code segment2; break; . . . case valueN: code segmentN; BackSlide9

break;

default: default code segment;

}

The value of this expression is either generated during program execution

or read in as user input. The case whose value is the same as that of theexpression will be executed.BackSlide10

Iteration Statements

:

Iteration statements are used to execute a particular set of instructions repeatedly until a particular condition is met or for a fixed number of iterations.

There are 3 types of loops in C programming:for loop

while loopdo...while loopfor Loop Syntax: The for statement or the for loop repeatedly executes the set of instructions that comprise the body of the for loop until a particular condition is satisfied.BackSlide11

Syntax:

for(initialization; termination; increment/decrement)

{ //statements to be executed }

The for loop consists of three expressions:

The initialization expression, which initializes the looping index. The initialization expression is executed only once, when the loop begins. The termination expression, which represents a condition that must be true for the loop to continue execution.The increment/decrement expression is executed after every iteration to update the value of the looping index.Slide12

Write a program to find the sum of first n natural numbers where n is entered by user. Note: 1,2,3... are called natural numbers

#include <

stdio.h

> int main()

{ int n, count, sum=0; printf("Enter the value of n.\n"); scanf("%d",&n); for(count=1;count<=n;++count) { sum+=count;} printf("Sum=%d",sum); return 0; }BackSlide13

While Statement

:

The while statement executes a block of statements repeatedly while a particular condition is true.

Syntax of while loop:

while (test expression) { statements to be executed. } Write a C program to find the factorial of a number, where the number is entered by user. (Hints: factorial of n = 1*2*3*...*n#include <stdio.h>  int main(){ int number, factorial; printf("Enter a number.\n"); Slide14

scanf

("%

d",&number

); factorial=1; while (number>0){ factorial=factorial*number; --number;

} printf("Factorial=%d",factorial); return 0; }BackSlide15

Do-while Loop

:

The do-while statement evaluates the condition at the end of the loop after executing the block of statements at least once. If the condition is true the loop continues, else it terminates after the first iteration.

Syntax:do

{ some code/s; } while (test expression);Write a C program to add all the numbers entered by a user until user enters 0. #include <stdio.h> int main(){ int sum=0,num; do Slide16

{

printf("Enter a number\n");

scanf

("%d",&num); sum+=num; } while(num!=0);

printf("sum=%d",sum); return 0; }BackSlide17

Jump statements

:

There are two statement built in C, break; and continue; to interrupt the normal flow of control of a program.

Break Statement :

Syntax of break statement Break;Example#include <stdio.h> int main () {int a = 10; while( a < 20 ) { printf("value of a: %d\n", a); a++; if( a > 15) {break; } } return 0; }BackSlide18

Continue Statement

:

It is sometimes desirable to skip some statements inside the loop. In such cases, continue statements are used.

Write a C program to find the product of 4 integers entered by a user. If user enters 0 skip it. # include <

stdio.h> int main(){ int i,num,product; for(i=1,product=1;i<=4;++i){ printf("Enter num%d:",i); scanf("%d",&num); Slide19

if(num==0) continue;

product*=num;

}

printf("product=%d",product);

return 0; }BackSlide20

Thank You