Top Banner
ared by: Mrs. Nurul Zakiah Binti Zamri CONTROL STATEMENT
12
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: Control statement-Selective

Prepared by: Mrs. Nurul Zakiah Binti Zamri Tan

CONTROL STATEMENT

Page 2: Control statement-Selective

TYPE OF CONTROL STATEMENT

C support 3 kinds of control structure:

sequential

selective

iterative

Page 3: Control statement-Selective

statement that is used to alter the continuous sequential execution of statements

sequential

no branching, looping or have any decision making in the statement

A series of coding.

Source code is execute one after and after.

CONTROL STATEMENT ??

Use to control the flow of the source code/programming

Key to write a structured program where CS can :

Reduce complexityImprove clarity.

Facilitate debugging and modifying

Technique to organize a program

Page 4: Control statement-Selective

Operation is simple to trace

ADVATAGES OF CONTROL STATEMENT

Structures are easier to define in flowcharts

Structure programming increases programmer's productivity

Page 5: Control statement-Selective

CONTROL STATEMENT

Simple if statement

If else statement

If-else if ladder statement Switch case statement

go to statement for statement

While statement

do-while

Unconditional Conditional Looping

Page 6: Control statement-Selective

If statement

If (test condition)

{

Statement block;

}

Next statement;

GENERAL FORM

#include <stdio.h>#include <conio.h>

int main (void){

int num1;int num2;

printf (“Please Enter 2 numbers”);scanf(“%d%d”, &num1,&num2);

If (num1== num2)

{

printf(“There are equal”);

}getch();}

EXAMPLE

To execute one statement or group of statements for particular condition

Page 7: Control statement-Selective

If –else-statement

If (test condition){Statement block-1;}else{Statement block-2}Next statement;

GENERAL FORM

#include <stdio.h>

void main(){

int mark;printf (“Please Enter mark”);scanf( “%d”, &mark);

If (mark=>39){printf(“Pass”);}else {printf(“Fail”);}getch();}

EXAMPLE

To execute one group of statements if the test condition is true or other group if the test condition is false

Page 8: Control statement-Selective

If else.. ladder statement

If (test condition-1){Statement block-1;}Else if (test condition-2){Statement block-2;}……Else if (test condition-n){Statement block-n;}Else

Default statement;Next statement;

GENERAL FORMTo take multiple decision. This statement is form by joining if..else statement.

If one condition is false, it checks for the next condition and so on. When all the conditions are false, then else block is executed

Page 9: Control statement-Selective

#include<stdio.h> #include<string.h> void main()

{ int n; printf(" Enter 1 to 3 to select your pet"); scanf("%d",&n);

if(n==1) { printf("You pet is a tiger"); } else if(n==2) {printf("You pet is a sugar glider"); } else if(n==3) { printf("You pet is a cow"); } else {printf("No pet selected"); } getch(); }

EXAMPLE

If else.. ladder statement

Page 10: Control statement-Selective

switch statement switch (expression)

{ case value1: Statement block-1:break;

case value2: Statement block-2:break; ….. .case value-n: statement block-n:break;

default:default statement;break;}Next statement

GENERAL FORM

Alternative to if else if ..ladder statement

to execute a block of code based on selection from multiple choices.

C switch statement is a multiway decisions that tests whether a variable or expression matches one of a number of constant integer values, and branches accordingly.

'break' is a statement. It is most commonly used with the switch statement. 'break' statement stops the execution within the switch statement and exits from the switch statement.

Page 11: Control statement-Selective

#include <stdio.h>

main(){int Grade;choose:

printf(“Please insert your grade A, B, C, D, E, or F”);scanf("%d",&Grade);

switch( Grade ) { case 'A' :

printf( "Excellent\n" ); break; case 'B' :

printf( "Good\n" ); break; case 'C' :

printf( "OK\n" ); break;

EXAMPLE

case 'D' :printf( "Mmmmm....\n" );

break; case 'F' :

printf( "You must do better than this\n" ); break; default :

printf( "What is your grade anyway?\n" ); go to choose; break; }getch();}

Page 12: Control statement-Selective

goto statement

goto label:

GENERAL FORM

Used to transfer the program control unconditionally from one point to another

its functionality is limited and it is only recommended as a last resort if structured solutions are much more complicated