Certain task requires execution of some statement ignoring the rest
these statement can be called decision making statement. Decision
making structures require that the programmer specify one or more conditions to
be evaluated or tested by the program, along with a statement or statements to
be executed if the condition is determined to be true, and optionally, other
statements to be executed if the condition is determined to be false.
Statement Description If statement An ‘if’ statement consists of a
condition followed by one or more statements. If-else statement An ‘if’ statement can be followed by
an optional ‘else’ statement, which executes when the condition is false. If-else-if statement An if statement can be followed by an optional else if...else statement, which is very
usefull to test various conditions using single if...else if statement. Nested if statement You can use one ‘if’ or ‘else if’
statement inside another ‘if’ or ‘else if’ statement(s). Switch case statement A ‘switch’ statement allows a
variable to be tested for equality against a list of values. Nested switch statement You can use one ‘switch’ statement
inside another ‘switch’ statement(s). break statement Terminates the loop or switch statement and transfers execution to the statement
immediately following the loop or switch. continue statement Causes the loop to skip the
remainder of its body and immediately retest its condition prior to
reiterating. goto statement Transfers control to the labelled
statement. Though it is not advised to use goto statement in your program.
If Statement
The if statement is used to control the sequence of
statement it changes the sequential execution.
Syntax
-
if(condition){
Statement (s)
Statement (s)
} Statement (x)
Example of If statement
#include<stdio.h>
#include<conio.h>
void main(){
int i=0;
clrscr();
if(printf("%d",i)<printf("\ni=3")){
printf("%d",++i);
}
printf("%d%d",++i,~++i);
getch();
}
Output
0
i=3 1 3 -3
If-Else Statement
The if else expression execute a group of statement if
test expression is true it also executes another group of statement if the text
expression is False hence in either case a group of statement is executed.
Syntax -
If
(condition){
Statement (s)
true block
}
else{
Statement (s)
false block
}
Example of If-Else statement
#include<stdio.h>
#include<conio.h>
void main(){
int i=0,j=1;
clrscr();
if(printf("%d\b",i++)<printf("%d\n",j)){
printf("%d%d",i++*++j,++j^i++^++j);
}
else{
printf("%d%d",++i&++j&++i,++j^++j^i);
}
getch();
}
Output
1
00
If-Else-If Statement
An
if statement can be followed by
an optional else if...else statement,
which is very usefull to test various conditions using single if...else if
statement.
Syntax -
If (condition 1){
Statement (s)
}
else if(condition 2){
statement2 (s)
}
else if(condition 3){
statement3 (s)
}
else{
statement
}statement-X
Example of If-Else-If statement
#include<stdio.h>
#include<conio.h>
void main(){
int i,j,k;
clrscr();
printf("enter value of i=");
scanf("%d",&i);
printf("enter value of j=");
scanf("%d",&j);
printf("enter value of k=");
scanf("%d",&k);
if(i>j&&i>k){
printf("max=%d",i);
}
else if(j>k){
printf("max=%d",j);
}
else{
printf("max=%d",k);
}
getch();
}
Output
Enter value of i=55
Enter value of j=19
Enter value of k=88
Max=88
Nested If-Else Statement
If is perfectly all right if we write an enter if else
with in the body of the if statement this is called nesting this helps in multi
decision making.
Syntax -
If (condition){
If(condition){
Statement (s)
}
else{
Statement (s)
}
}
else{
if(condition){
statement2 (s)
}
else{
statement3 (s)
}
}statement-X
Example of Nested If-Else statement
#include<stdio.h>
#include<conio.h>
void main(){
int i,j,k,l;
clrscr();
printf("Enter value of i=");
scanf("%d",&i);
printf("Enter value of j=");
scanf("%d",&j);
printf("Enter value of k=");
scanf("%d",&k);
printf("Enter value of l=");
scanf("%d",&l);
if(i>j){
if(i>k){
if(i>l){
printf("Max=%d",i);
}
else{
printf("Max=%d",l);
}
}
else{
if(k>l){
printf("Max=%d",k);
}
else{
printf("Max=%d",l);
}
}
}
else{
if(j>k){
if(j>l){
printf("Max=%d",j);
}
else{
printf("Max=%d",l);
}
}
else{
if(k>l){
printf("Max=%d",k);
}
else{
printf("Max=%d",l);
}
}
}
getch();
}
Output
Enter value of i=8
Enter value of i=88
Enter value of i=4
Enter value of i=11
Max=88
Switch Case
Statement
The control statement that allows us to make a
decision from the number of choice is called switch case default this statement
test the value of a given expression against a list of case value and when a
match is found a block of statement associates with that case is executed the
break statement at the end of each block signed at the end of a particular case
and causes an exit from the switch statement.
Syntax -
switch(expression){
case 1:
constant-expression :
statement(s);
break; //optional
case 2: constant-expression :
statement(s);
break; //optional
default : //Optional
statement(s);
}
Example of Switch Case statement
#include<stdio.h>
#include<conio.h>
void main(){
int i;
clrscr();
printf("Enter value of i=");
scanf("%d",&i);
switch(i){
case 1: printf("One");
break;
case 2: printf("Two");
break;
case 3: printf("Three");
break;
default: printf("Enter value of 1 to 3:");
}
getch();
}
Output
Enter value of i=6
Enter value of 1 to 3:
Enter value of i=2
Two
Nested Switch Case Statement
It is
possible to have a switch as part of the statement sequence of an outer switch.
Even if the case constants of the inner and outer switch contain common values,
no conflicts will arise.
Syntax -
switch(ch1) {
case 1:
cout <<
"This A is part of outer switch";
switch(ch2) {
case 1:
cout <<
"This A is part of inner switch";
break;
default:
statement
}
break;
default:
switch(ch2) {
case 1:
cout <<
"This A is part of inner switch";
break;
default:
statement
}
break;
}
Break Statement
The statement causes unconditional exit from for while
do while if else or switch statement immediately outside the block in which
break appear.
Syntax -
Break;
Continue Statement
The continue statement causes conditional transfer to
the next iteration in a for while do for statement the control is transfer to
the statement begin of the block in which continue appear.
Syntax -
Continue;
Goto Statement
The statement causes unconditional transfer to
statement marked with the label within the function the label is marked with a
unique many or number followed by a colon and a statement.
Syntax -
goto label;
..
.
label: statement;
0 Comments