decision making statements decision making statements if statement if...else statement nested...

20
Control Structures

Upload: egbert-long

Post on 24-Dec-2015

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

Control Structures

Page 2: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

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

Page 3: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

Control StructuresControl 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 statements Selection statements Iteration statements Jump 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; }

Back

Page 4: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

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.

Back

Page 5: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

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;

Page 6: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

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; }

Back

Page 7: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

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;

Back

Page 8: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

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;

Back

Page 9: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

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 the

expression will be executed.

Back

Page 10: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

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 loop do...while loop

for 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. Back

Page 11: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

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.

Page 12: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

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; } Back

Page 13: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

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");

Page 14: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

scanf("%d",&number); factorial=1; while (number>0){ factorial=factorial*number; --number; } printf("Factorial=%d",factorial); return 0; }

Back

Page 15: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

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

Page 16: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

{ printf("Enter a number\n"); scanf("%d",&num); sum+=num; } while(num!=0); printf("sum=%d",sum); return 0; }

Back

Page 17: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

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; }

Back

Page 18: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

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);

Page 19: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

if(num==0) continue; product*=num; } printf("product=%d",product); return 0; }

Back

Page 20: Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement)

Thank You