Top Banner
CONDITIONAL STATEMENT Group 2 By
32

Conditional Statement

Apr 15, 2017

Download

Documents

Jordan Semoy
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Conditional Statement

CONDITIONAL STATEMENT

Group 2

By

Page 2: Conditional Statement

WHAT IS CONDITIONAL STATEMENT ? In Computer Science, conditional statements are

features of a programming language which perform different computations or actions depending on whether a programmer-specified Boolean condition evaluates to true or false.

It is used to control the amount of time a statement or sequence of statement is carried out based on some condition.

Page 3: Conditional Statement

IMPORTANCE OF CONDITIONAL STATEMENT Conditional statements are important

because the goals of the user of the program maybe varied going from one user to the next. Programs which have this structure for programming offer options/paths which the user may take in the program to acquire a specific outcome. Conditional statements are important in achieving specific objectives of the user.

Page 4: Conditional Statement

TOPICS BEING FOCUSED ON Conditional statement If statement Switch statement

Page 5: Conditional Statement

CONDITIONAL STATEMENTConditional statement

sequenceselection iteration

bounded unboundedIf statement

If-then-else

Else if

Switch Statement

Page 6: Conditional Statement

SEQUENTIAL CONTROL STRUCTURE This refers to programming instructions

which are performed in a strictly sequential manner and each instruction is executed step by step.

Page 7: Conditional Statement

EXAMPLES OF SEQUENTIAL Pascal Start

READ CostREAD QuanREAD Unit_Cost

Cost = Quan*Unit_Cost PRINT “The cost is :”,CostStop

Page 8: Conditional Statement

EXAMPLES OF SEQUENTIAL C #include<stdio.h> #include<conio.h>

Int main (){Printf (“welcome to C language”);getch();Return 0:}

Page 9: Conditional Statement

SELECTION CONTROL STRUCTURE This is used where one of several

alternative programming instructions are selected and executed depending on whether or not a particular condition is true or false.

There are three forms of IF statements:  IF-THEN IF-THEN-ELSE IF-THEN-ELSIF

Page 10: Conditional Statement

IF-THEN CONDITION The order of operation for if-then condition

is:

IF condition THEN sequence_of_statements END IF;

The sequence of statements is executed only if the condition is true. If the condition is false or null, the IF statement does nothing. In either case, control passes to the next statement.

Page 11: Conditional Statement

EXAMPLES OF IF-THEN PascalStart Read Num1Read Num2

If Num1>Num2 ThenPrint “larger”,Num1

Stop

Page 12: Conditional Statement

EXAMPLES OF IF-THEN C#include<stdio.h> int main(){ int x,y;

printf("Enter value for x :"); scanf("%d",&x); printf("Enter value for y :"); scanf("%d",&y); if ( x > y ){ printf("X is large number - %d\n",x);

}return 0;

}

Page 13: Conditional Statement

IF-THEN-ELSE The operations for if-then-else is the

same as if then, only difference is that if the first condition is false it is able to executed the next condition

IF condition THEN sequence_of_statements

ElseSequence_of_statementsEND IF;

Page 14: Conditional Statement

EXAMPLES OF IF-THEN-ELSE PascalStart Read Num1Read Num2

If Num1>Num2 ThenPrint “larger”,Num1

elsePrint “larger”,Num2

EndifStop

Page 15: Conditional Statement

EXAMPLES OF IF-THEN-ELSE C#include<stdio.h> int main(){ int x,y;

printf("Enter value for x :"); scanf("%d",&x); printf("Enter value for y :"); scanf("%d",&y); if ( x > y ){ printf("X is large number - %d\n",x);

}else{ printf("Y is large number - %d\n",y);

}return 0;

}

Page 16: Conditional Statement

IF-THEN-ELSEIF If the first condition is false or null,

the ELSIF clause tests another condition. An IF statement can have any number of ELSIF clauses; the final ELSE clause is optional. Conditions are evaluated one by one from top to bottom. If any condition is true, its associated sequence of statements is executed and control passes to the next statement. If all conditions are false or null, the sequence in the ELSE clause is executed. 

Page 17: Conditional Statement

EXAMPLES OF IF-THEN-ELSEIF Pascal IF sales > 50000 THEN

bonus := 1500; ELSIF sales > 35000 THEN

bonus := 500; ELSE

bonus := 100; END IF;

Page 18: Conditional Statement

EXAMPLES OF IF-THEN-ELSEIF C #include <stdio.h> main() {Int cow if (cows == 5 ) { printf("We have 5 cows\n"); } else if( (cows == 6 ) { printf("We have 6 cows\n"); } }

Page 19: Conditional Statement

SWITCH STATEMENT A switch statement is a selection statement

that lets you transfer control to different statements within the switch body depending on the value of the switch expression. The switch expression must evaluate to an integral or enumeration value. The body of the switch statement contains case clauses that consist of

A case label An optional default label A case expression A list of statements.

Page 20: Conditional Statement

SWITCH STATEMENT CONT’D If the value of the switch expression equals

the value of one of the case expressions, the statements following that case expression are processed. If not, the default label statements, if any, are processed.

A case label contains the word case followed by an integral constant expression and a colon. The value of each integral constant expression must represent a different value; you cannot have duplicate case labels. Anywhere you can put one case label, you can put multiple case labels.

Page 21: Conditional Statement

SWITCH STATEMENT CONT’D A default clause contains a default label

followed by one or more statements. You can put a case label on either side of the default label. A switch statement can have only one default label.

Page 22: Conditional Statement

SWITCH STATEMENT CONT’D The value of the switch expression is

compared with the value of the expression in each case label. If a matching value is found, control is passed to the statement following the case label that contains the matching value. If there is no matching value but there is a default label in the switch body, control passes to the default labelled statement. If no matching value is found, and there is no default label anywhere in the switch body, no part of the switch body is processed.

Page 23: Conditional Statement

EXAMPLE OF SWITCH STATEMENT char key;

printf("Enter an arithmetic operator\n"); scanf("%c",&key); switch (key)

{ case '+':

add(); break; case '-':

subtract(); break;

case '*': multiply();

break; case '/':

divide(); break; default: printf("invalid key\n");

break; }

Page 24: Conditional Statement

ITERATION Occurs when one or more programming

instructions are performed repeatedly depending on whether or not a particular condition is met.

These may also be referred to as loops. There are two main types:

For-loop While-loop

Page 25: Conditional Statement

ITERATION(WHILE LOOP) The most basic loop in C is the while

loop.A while statement is like a repeating if statement. Like an If statement, if the test condition is true: the statments get executed. The difference is that after the statements have been executed, the test condition is checked again. If it is still true the statements get executed again.This cycle repeats until the test condition evaluates to false.

Page 26: Conditional Statement

ITERATION(WHILE LOOP) CONT’D Basic syntax of while loop is: while ( expression ) {

Single statement or Block of statements;

}

Page 27: Conditional Statement

EXAMPLE OF WHILE LOOP Pascal

Read NumberWhile number<=5 Do

Read numberEndwhile

Page 28: Conditional Statement

EXAMPLE OF WHILE LOOP C#include <stdio.h> main() { int i = 10;

while ( i > 0 ) { printf("Hello %d\n", i ); i = i -1; }

}

Page 29: Conditional Statement

ITERATION (FOR LOOP) Whereas the number of iterations through

a WHILE loop is unknown until the loop completes, the number of iterations through a FOR loop is known before the loop is entered. FOR loops iterate over a specified range of integers.

Basic syntax of for loop is as follows:

for( expression1; expression2; expression3){ Single statement or Block of statements; }

Page 30: Conditional Statement

EXAMPLE OF FOR LOOP PascalRead Sum=0Read Counter=0For Counter =1 to 5 Do

Read NumberSum =Sum +number

Endfor

Page 31: Conditional Statement

EXAMPLE OF FOR LOOP C#include <stdio.h> main() { int i; int j = 10;

for( i = 0; i <= j; i ++ ) { printf("Hello %d\n", i ); }

}

Page 32: Conditional Statement

THANK YOU FOR WATCHING

GOODBYE