There are several statements in C Programming language are discussed below:
C program executes program sequantially. Sometimes, a program requires checking of certain conditions in a 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.' Following are the different conditional statements used in C.
This is a conditional statement used in C to check condition or to control the flow of executin of statements. This is also called as 'desicion making statement.' or 'control statement'. The execution of a whole program is done in one direction only.
if(condition){ statements; }
In above syntax, 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 there are more than 1 (one) statements in if statement then use { } braces else it is not necessary to use
void main(){ int a; a = 5; if(a>4) printf("\n Value of A is greater than 4!"); if(a == 4) printf("\n Value of A is 4!"); }
This is also one of the most useful conditional statement used in C to check conditions.
if(condition){ true statements; } else{ false statements; }
An example program for if else statement:
void main(){ int no; printf("\nEnter nunmber:"); scanf("%d", &no); if(no%2 == 0){ printf("\n\nNumber is Even!"); else printf("\n\nNumber is Odd!"); } }
Even Number: 11
Number is odd!
if(condition){ if(condition){ statements; } else{ statements; } else{ statements; }
An example program for Nested if-else statement:
void main(){ int no; printf("\nEnter nunmber:"); scanf("%d", &no); if(no%2 == 0){ printf("\n\nNumber is Even!"); } else{ if(no==0) { printf("\n\n It is 0!"); } else{ printf("Number is less than 0!"); } } }
Even Number: 0
It is 0!
This is a multiple or multiway branching decision making statement. When we use nested if-else statement to check more than 1 conditions then the complexity of a program increases in a case of a lot of conditions. Thus, the program is diffult to read and maintain. So to ouvercome 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 transfered to that point
switch(expression){ case expr1: statements; break; case expr2: statements; break; ..... ..... ..... case exprn: statements; break; default: statements; }
An example program for switch case:
void main() { int no; printf("\n Enter any number from 1 to 3:"); scanf("%d",&no); switch(no) { case 1: printf("\n\n It is 1"); case 2: printf("\n\n It is 2"); case 3: printf("\n\n It is 3"); case 4: printf("\n\n It is 4"); default: printf("\n\nInvalid Number!"); } } Even any number from 1 to 3: 3 It is 3! Even any number from 1 to 3: 5 Invalid Number!
'A loop' is a part of code of a program which is executed repeatedly.
A loop is used using condition. The repetition is done intil condition becomes true.
A loop declaration and execution can be done in following ways
There are two types of looping statements
In such type of loop, the test condition is checked first before the loop is executed. Some common examples of this is looping statemetns are:
In such type of loop, the loop is executed first. Then the condition is checked after block of statements are executed. The loop executed atleast one time compulsorily:
This is an entry controlled looping statement. It is used to repeat a block of statments as long as the condition is true.
while(condition) { statements; increment / decrement; }
Example Program:
void main() { int a; a = 1; while(a <= 5) { printf("\nHello"); a += 1; // i.e. a = a+1 } } output: Hello Hello Hello Hello Hello
This is an entry controlled looping statement. In this loop structure, more than one variable can be initialized. One of the most important feature of this loop is that the three actions can be taken at a time like variable initialization, condition checking and increment/decrement. The for loop can be more concise and flexible than that of while and do-while loops.
for(initialization; test-condition; increase/decrease) { statements; }
Feature
Example Program:
void main() { int a; for(i=0; i<5; i++) { printf("\n\tHello"); //5 times } output: Hello Hello Hello Hello Hello
This is an entry controlled looping statement. Sometimes, there is need to execute a block of statements first then to check condition. At the time such type of a loop is used. In this, block of statements are executed first and then condition is checked.
do { Statements; (increment/Decrement); }while(condition); Note: The while statement should be terminated with ; (semicolon).
Example Program:
void main() { int a; a = 1; do() { printf("\n\tHello"); // 5 times a+=1; // i.e. a = a + 1 }while(a <= 5); a = 6; do { printf("\n\n\t Technowell"); // 1 time a += 1; // i.e. a = a+1 }while(a<=5); } output: Hello Hello Hello Hello Hello Technowell
Sometimes, it is necessary to exit immediately from a loop as soon as the condition is satisfied.
When break statement is used inside a loop, then it can cause to terminate from a loop. The statement after break statement are skipped.
while(condition) { __ __ __ __ __ __ __ break ;------↴ __ __ __ __ ↓ __ __ __ ↓ ↓ } ←---------------↵ void main() { int i; for(i=1; ; i++) { if(i > 5) break; printf("%d", i); // 5 times only } } output: 12345
Sometimes, it 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 to loop. It causes the loop to be continued with next iteration after skipping statements in between. Continue statement simply skips statements and continues next iteration.
while(condition)←--- { ↑ __ __ __ ↑ __ __ __ __ ↑ ↑ continue ;---- __ __ __ __ __ __ __ } void main() { int i; for(i=1; i<=8; i++) { if(i == 6) contiune; printf("\n\t%d", i); // 6 is omited } } output: 1 2 3 4 5 7 8
It is a well known as 'jumping statement.' It is primarily used to transfer the control of execution to any place in a program. It is useful to provide branching within a loop.
When the loops are deeply nested at that if an error occurs then it is diffuct to get exited from such loops. Simple break statement cannot woek here properly. In this situation goto statement is used.
Syntax: while(condition) { for( ; ; ; ) { __ __ __ __ __ __ __ goto err;-------↴ __ __ __ __ ↓ __ __ __ ↓ ↓ } ↓ err : ←-------------↵ } void main() { int i = 1, j; while(i <= 3) { for(j = 1; j <= 3; j++) { printf(" * "); if(j == 2) goto stop; } i = i + 1; } stop: printf("\n\nExited!"); } output: * * Exited!