CONDITIONAL STATEMENT Group 2 By
CONDITIONAL STATEMENT
Group 2
By
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.
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.
TOPICS BEING FOCUSED ON Conditional statement If statement Switch statement
CONDITIONAL STATEMENTConditional statement
sequenceselection iteration
bounded unboundedIf statement
If-then-else
Else if
Switch 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.
EXAMPLES OF SEQUENTIAL Pascal Start
READ CostREAD QuanREAD Unit_Cost
Cost = Quan*Unit_Cost PRINT “The cost is :”,CostStop
EXAMPLES OF SEQUENTIAL C #include<stdio.h> #include<conio.h>
Int main (){Printf (“welcome to C language”);getch();Return 0:}
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
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.
EXAMPLES OF IF-THEN PascalStart Read Num1Read Num2
If Num1>Num2 ThenPrint “larger”,Num1
Stop
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;
}
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;
EXAMPLES OF IF-THEN-ELSE PascalStart Read Num1Read Num2
If Num1>Num2 ThenPrint “larger”,Num1
elsePrint “larger”,Num2
EndifStop
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;
}
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.
EXAMPLES OF IF-THEN-ELSEIF Pascal IF sales > 50000 THEN
bonus := 1500; ELSIF sales > 35000 THEN
bonus := 500; ELSE
bonus := 100; END IF;
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"); } }
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.
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.
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.
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.
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; }
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
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.
ITERATION(WHILE LOOP) CONT’D Basic syntax of while loop is: while ( expression ) {
Single statement or Block of statements;
}
EXAMPLE OF WHILE LOOP Pascal
Read NumberWhile number<=5 Do
Read numberEndwhile
EXAMPLE OF WHILE LOOP C#include <stdio.h> main() { int i = 10;
while ( i > 0 ) { printf("Hello %d\n", i ); i = i -1; }
}
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; }
EXAMPLE OF FOR LOOP PascalRead Sum=0Read Counter=0For Counter =1 to 5 Do
Read NumberSum =Sum +number
Endfor
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 ); }
}
THANK YOU FOR WATCHING
GOODBYE