Anda di halaman 1dari 9

If else C program executes program sequentially. Sometimes, a program requires checking of certain conditions in program execution.

C provides various key condition statements to check condition and execute statements according conditional criteria. These statements are called as 'Decision Making Statements' or 'Conditional Statements.' If This is a conditional statement used in C to check condition or to control the flow of execution of statements. This is also called as 'decision making statement or control statement.' The execution of a whole program is done in one direction only. Syntax: Flowchart Example #include <stdio.h> void main() { if(condition) int n; scanf("%d",&n); { if(n==1) printf("\ni am vivek"); statements; } } /*vivek*/ Explanation:- if u give value of n as 1 then it print I am vivek otherwise it not give any output /*Vivek*/ /*Vivek*/

Nested If An If statement is inside another if statement is termed as nested if statement. This type of control structures is called conditional branching. Syntax: If(condition) { if (condition) { Statements } Example Write program to check given no is divisible by 5 or 2 or both #include <stdio.h> void main() { int n; scanf("%d",&n); if(n%5==0) { if(n%2==0) { printf("\n divisible 2");

else { Statements } } else { Statements } /*Vivek*/

} else { printf("\n not divisible 2"); } printf("\n divisible 5"); } else { printf("\n not divisible 5"); } } /*vivek*/ Explanation:-

If-else This is also one of the most useful conditional statement used in C to check conditions. In this the condition is checked first. If it is true, then the program control flow goes inside the braces and executes the block of statements associated with it. If it returns false, then it executes the else part of a program. Syntax: Flowchart Example #include <stdio.h> void main() { int no; scanf("%d",&no); if(no%2==0) printf("\n even "); else printf("\n odd "); } /*Vivek*/ Explanation:- if you give value of n as no which divisible by 2 then it print even otherwise it print odd

if(condition) { true statements; } else { false statements; }

/*Vivek*/

/*vivek*/

Nested If-Else It is a conditional statement which is used when we want to check more than 1 conditions at a time in a same program. The conditions are executed from top to bottom checking each condition whether it

meets the conditional criteria or not. If it found the condition is true then it executes the block of associated statements of true part else it goes to next condition to execute Syntax: Flowchart Example #include <stdio.h> void main() { int no; scanf("%d",&no); if(no>0) { printf(" No greater than0"); } else { if(no==0) { printf("\n It is 0 "); } else { printf("No less than0"); } } } /*vivek*/ Explanation:-if you give no then it first check no is greater then zero if yes it print otherwise it got to else in this else it chech if condition no is equal to zero if true then print otherwise it go to else and print no is less then zero.

if(condition) { if(condition) { statements; } else { statements; } } else { statements; } /*Vivek*/

/*Vivek*/

Switch case This is a multiple or multiway brancing decision making statement. When we use nested if-else statement to check more than 1 conditions then the complexity of a program increases in case of a lot of conditions. Thus, the program is difficult to read and maintain. So to overcome this problem, C provides 'switch case'.Switch case checks the value of a expression against a case values, if condition matches the case values then the control is transferred to that point. The execution of switch statement begins with the evaluation of expression. If the value of expression matches with the constant then the statements following this statement execute sequentially till it

executes break. The break statement transfers control to the end of the switch statement. If the value of expression does not match with any constant, the statement with default is executed. Some important points about switch statement The expression of switch statement must be of type integer or character type. If case is int then: case5: Case in char then : case a: The default case need not to be used at last case. It can be placed at any place. The case values need not to be in specific order.

Syntax: switch(expression) { case expr1: statements; break; case expr2: statements; break;

Flowchart

case exprn: statements; break; default: statements; } /*vivek*/ /*vivek*/

Example #include <stdio.h> void main() { int n; scanf("%d",&n); switch(n) { case 1: printf("\nIt is 1 "); break; case 2: printf("\n It is 2 "); break; case 3: printf("\nIt is 3 "); break; default: printf("\nInvalid number "); } } /*vivek*/ Explanation:-if you give value of n as 1 it print case1and break if n is 2 it goto case 2and print case 2 if n is 3 it goto case 2and print case 3 otherwise print default.

Looping statement It is also called a Repetitive control structure. Sometimes we require a set of statements to be executed a number of times by changing the value of one or more variables each time to obtain a different result. This type of program execution is called looping. C++ provides the following construct while loop do-while loop for loop

for loop In this loop structure, more than one variable can be initilized. One of the most important feature of this loop is that the three actions can be taken at a time like variable initilisation, condition checking and increment/decrement. The for loop can be more concise and flexible than that of while and do-while loops The flow diagram indicates that in for loop three operations take place:

Initialization of loop control variable Testing of loop control variable Update the loop control variable either by incrementing or decrementing. Operation (i) is used to initialize the value. On the other hand, operation (ii) is used to test whether the condition is true or false. If the condition is true, the program executes the body of the loop and then the value of loop control variable is updated. Again it checks the condition and so on. If the condition is false, it gets out of the loop.

Syntax: for(initialize;decision;increment/decrement) { statement(s); }

Flowchart

/*vivek*/

/*vivek*/

Example #include <stdio.h> void main() { int i; for(i=1; i<3; i++) { printf("vivek\n"); } } /*vivek*/ Explanation: -i is intialize to 1 therfore first time 1<3 condition is true it increment in i by 1 and print vivek now i become2 0<2 condition is true it increment i by 1 and print vivek now i become 3 and 3<3 is false therefore it stop.

While loop:Here the condition is checked first. If it is true, then the program control flow goes inside the loop and executes the block of statements associated with it. At the end of loop increment or decrement is done to change in variable value. This process continues until test condition satisfies.

Syntax:

Flowchart

while(condition) { statement(s); }

/*vivek*/

/*vivek*/

Example #include <stdio.h> void main() { int i=1; while(i<3) { printf("\n vivek"); i++; } /*vivek*/ } Explanation: -i is intialize to 1 therfore first time 1<3 condition is true print vivek and it increment in i by 1 now i become2 0<2 condition is true it print vivek and increment i by 1 now i become 3 and 3<3 is false therefore it stop.

Do-While loop:Sometimes, there is need to execute a block of statements first then to check condition. At that time such type of a loop is used. In this, block of statements are executed first and then condition is checked. Here first the block of statements are executed. At the end of loop, while statement is executed. If the resultant condition is true then program control goes to evaluate the body of a loop once again. This process continues till condition becomes true. When it becomes false, then the loop terminates. Syntax: do { statements; } while (condition); Flowchart Example #include <stdio.h> void main() { int i=1; do { printf("\n vivek"); i++; } while(i<3); } /*vivek*/ Explanation: -i is intialize to 1 and print vivek and increment in i by 1 now i become 2 and check for condition 2<3 if true it print

/*vivek*/

/*vivek*/

vivek and increment in i by 1 now i become 3 and 3<3 is false therefore it stop.

Jump Statements The jump statements unconditionally transfer program control within a function. goto statement break statement continue statement exit() Goto Statement : In C programming language goto statement is used to transfer the control unconditionally from the one statement to any other statement in the program. i.e goto allows to make jump to another point in the program. In its general form, the goto statement is written as goto label; where the label is an identifier that is used to label the target statement to which the control is transferred. Control may be transferred to anywhere within the current function. The target statement must be labeled, and a colon must follow the label. Thus the target statement will appear as label: (note:- when we use goto lable it end with ; and lable end with : )

Syntax: goto vivek; ------------vivek:

Flowchart

Example #include <stdio.h> void main() { printf("vivek\n"); goto end; printf("hello\n"); end: printf("world"); } /*vivek*/ Explanation: -progrm execute first printf statement and print vivek after ths it execute goto statement which take control to end which skip the printf

/*vivek*/

/*vivek*/

statement hello and go to end and execute the next printf statement world Therefore output vivek world Break Statement : Sometimes, it is necessary to exit immediately from a loop as soon as the condition is satisfied. The break statement, when executed in a switch structure, provides an immediate exit from the switch structure. Similarly, you can use the break statement in any of the loop. When the break statement executes in a loop, it immediately exits from the loop. Syntax: while(condition) { statement; break; statement; } Flowchart Example #include <stdio.h> void main() { int i=0; while(i<3) { printf("vivek\n"); break; printf("hello"); } printf("world"); } /*vivek*/ Explanation: -it stop loop in between .here 0<3 is true it print vivek and due to break condition it skip the hello statement and stop the loop and go outside of loop and print world

/*vivek*/

/*vivek*/

Continue Statement : Sometimes, it is required to skip a part of a body of loop under specific conditions. So, C supports 'continue' statement to overcome this anomaly. The working structure of 'continue' is similar as that of that break statement but difference is that it cannot terminate the loop. It causes the loop to be continued with next iteration after skipping statements in between. Continue statement simply skipps statements and continues next iteration.

Syntax:

Flowchart

while (condition) { Statement 1; If (condition) continue; statement; } /*vivek*/

Example #include <stdio.h> void main() { int i; for(i=1; i<=4; i++) { if(i==2) continue; printf("%d\n",i); } } /*vivek*/ Explanation: -in the for loop first time 1==2 is false it not satisfy and print 1 if condition and go to for loop againand increment by 1 now 2==2 is true it got to if condtion and due to continue it skip statement printf therefore 2 is not print in output and i is again increment and check 3==2 is false it not go toif condition and print 3 and loop is contine uptile condion is break.

/*vivek*/

Exit statement: Syntax: Example

Return statement: Syntax: Example

Anda mungkin juga menyukai