Have you ever thought what can if or else or else-if or while can do. Let’s play with Statements
The DECISION CONTROL STRUCTURE:
The first thing after you read the heading strikes is decisions. You have to take decisions when you are in the field and batting and your team needs 5 run from 2 balls. Either you decide to go for a sixer or a double and then a sixer. Right? This is what decision is.
The DECISION CONTROL STRUCTURE:
The first thing after you read the heading strikes is decisions. You have to take decisions when you are in the field and batting and your team needs 5 run from 2 balls. Either you decide to go for a sixer or a double and then a sixer. Right? This is what decision is.
IF:
The General form of If is:
The General form of If is:
if (the condition is right)
{
Here it can be more than one statement.
Execute the statement/statements;
}
{
Here it can be more than one statement.
Execute the statement/statements;
}
So how you check i.e. how you give the condition? It would be done by relational operators.
Like
Operators Expressions Type
x==y x and y are equal
x>=y x is greater than or equal to y
x>y x is greater than y
Operators Expressions Type
x==y x and y are equal
x>=y x is greater than or equal to y
x>y x is greater than y
x<y x is less than y
! Logical Not
* / % Arithmetic and Modulus
&& Logical AND
|| Logical OR
! Logical Not
* / % Arithmetic and Modulus
&& Logical AND
|| Logical OR
Now you must be thinking of what if the condition fails and I have to do even something more after the ‘if’. That’s why there is if-else & nested if-else.
What is if-else, nested if-else & else-if?
If-else is nothing but the same like if, if the condition doesn’t satisfy then the else statement will be executed. I know you know the meaning of nests. Yeah if-else within an if-else that’s it.
if (condition)
{
Statement/statements;
}
else
{
Statements
[if (condition)
statements;
else
statements;]
}
Yes my dear it is that simple. Of course you might be tired of giving braces so if you have single statements don’t bother to give those braces.
The else if clause is nothing different. It is just a way of rearranging the else with the if that follows it. This would be evident if you look at the following code:
What is if-else, nested if-else & else-if?
If-else is nothing but the same like if, if the condition doesn’t satisfy then the else statement will be executed. I know you know the meaning of nests. Yeah if-else within an if-else that’s it.
if (condition)
{
Statement/statements;
}
else
{
Statements
[if (condition)
statements;
else
statements;]
}
Yes my dear it is that simple. Of course you might be tired of giving braces so if you have single statements don’t bother to give those braces.
The else if clause is nothing different. It is just a way of rearranging the else with the if that follows it. This would be evident if you look at the following code:
if (i == 2)
printf (“With you…”);
else {if (j == 2)
printf (“…All the time”) ;}
if (i == 2 )
printf (“With you…” ) ;
elseif (j==2) ;
printf (“…All the time” ) ;
The LOOP CONTROL STRUCTURE:
Now what if will you do if you have to do a same job for repeated times? Bother writing same code again and again? No, we are smart so there is a way and that is loop control structures. They are:
1. WHILE:
General Form:
printf (“With you…”);
else {if (j == 2)
printf (“…All the time”) ;}
if (i == 2 )
printf (“With you…” ) ;
elseif (j==2) ;
printf (“…All the time” ) ;
The LOOP CONTROL STRUCTURE:
Now what if will you do if you have to do a same job for repeated times? Bother writing same code again and again? No, we are smart so there is a way and that is loop control structures. They are:
1. WHILE:
General Form:
Initialise loop counter;
while (condition)
{
do this;
and this;
Increment the counter;
}
while (condition)
{
do this;
and this;
Increment the counter;
}
Now there comes FOR, It allows you to specify three things about that loop within a single line. Isn’t that good? That’s why the most popular loop control structure is FOR.
2. FOR:
General Form:
for ( initialise counter; test counter; increment counter)
{
do this;
and this;
and this;
}
2. FOR:
General Form:
for ( initialise counter; test counter; increment counter)
{
do this;
and this;
and this;
}
The thing is you can do nested statements with while/for same like as you do in if. Another thing is you can do multiple initialisation within a for loop. For example, for(i=1,j=2; jThe loops still we played is for fixed number of times, what if we need for unknown number of times, there is a ODD loop for that and this is DO-WHILE, It can also be implemented using FOR.
3. DO-WHILE:
General Form:
do{
statement/statements;
}while(condition);
I know you’re thinking it still looks the same to me but the trick here is that if you gives in the condition some instructions to match if user want it again or not that’s it.
/* Execution of a loop an unknown number of times */
3. DO-WHILE:
General Form:
do{
statement/statements;
}while(condition);
I know you’re thinking it still looks the same to me but the trick here is that if you gives in the condition some instructions to match if user want it again or not that’s it.
/* Execution of a loop an unknown number of times */
main( )
{ char another ; int num ; do
{
printf ( “Enter a number ” ) ;
scanf ( “%d”, &num ) ;
printf ( “square of %d is %d”, num, num * num ) ;
printf ( “\nWant to enter another number y/n ” ) ;
scanf ( ” %c”, &another ) ;
} while ( another == ‘y’ ) ;
}
{ char another ; int num ; do
{
printf ( “Enter a number ” ) ;
scanf ( “%d”, &num ) ;
printf ( “square of %d is %d”, num, num * num ) ;
printf ( “\nWant to enter another number y/n ” ) ;
scanf ( ” %c”, &another ) ;
} while ( another == ‘y’ ) ;
}
And here is the sample output…
Enter a number 3
square of 3 is 9
Want to enter another number y/n y
Enter a number 6
square of 6 is 36
Want to enter another number y/n
square of 3 is 9
Want to enter another number y/n y
Enter a number 6
square of 6 is 36
Want to enter another number y/n
THE BREAK & CONTINUE STATEMENTS:
Sometimes we need to jump out of the loop instantly without waiting for other conditional tests. The keyword Break allows us to do this. When break is encountered inside any loop, control automatically passes to the first statement after the loop. A break is usually associated with an if.
In some programming situations we want to take the control to the beginning of the loop, bypassing the statements inside the loop, which have not yet been executed. The keyword continue allows us to do this. When continue is encountered inside any loop, control automatically passes to the beginning of the loop. A continue is usually associated with an if.
The CASE CONTROL STRUCTURE:
Now in programming/real time we have some situations where we have to choice between a number of alternatives like MCQs in your JEE exam. So we can do the same using an C program also. How? Just by using SWITCH.
SWITCH:
The General form of Switch:
Sometimes we need to jump out of the loop instantly without waiting for other conditional tests. The keyword Break allows us to do this. When break is encountered inside any loop, control automatically passes to the first statement after the loop. A break is usually associated with an if.
In some programming situations we want to take the control to the beginning of the loop, bypassing the statements inside the loop, which have not yet been executed. The keyword continue allows us to do this. When continue is encountered inside any loop, control automatically passes to the beginning of the loop. A continue is usually associated with an if.
The CASE CONTROL STRUCTURE:
Now in programming/real time we have some situations where we have to choice between a number of alternatives like MCQs in your JEE exam. So we can do the same using an C program also. How? Just by using SWITCH.
SWITCH:
The General form of Switch:
switch ( integer expression )
{
case constant 1 :
do this ;
break;
case constant 2 :
do this ;
break;
case constant 3 :
do this ;
break;
default :
do this ;
}
The GOTO Keyword:
Everybody says that goto keyword makes C programmer’s life miserable. The case is if you’re a good programmer and want to be a, it can always be done using if, for, while or switch. The only programming situation in favour of using goto is when we want to take the control out of the loop that is contained in several other loops. The following program illustrates this.
{
case constant 1 :
do this ;
break;
case constant 2 :
do this ;
break;
case constant 3 :
do this ;
break;
default :
do this ;
}
The GOTO Keyword:
Everybody says that goto keyword makes C programmer’s life miserable. The case is if you’re a good programmer and want to be a, it can always be done using if, for, while or switch. The only programming situation in favour of using goto is when we want to take the control out of the loop that is contained in several other loops. The following program illustrates this.
main( ) { int i, j, k ; for ( i = 1 ; i {
for ( j = 1 ; j {
for ( k = 1 ; k {
if ( i == 3 && j == 3 && k == 3 )
goto out ;
else
printf ( “%d %d %d\n”, i, j, k ) ;
}
}
}
out :
printf ( “Out of the loop at last!” ) ;
}
for ( j = 1 ; j {
for ( k = 1 ; k {
if ( i == 3 && j == 3 && k == 3 )
goto out ;
else
printf ( “%d %d %d\n”, i, j, k ) ;
}
}
}
out :
printf ( “Out of the loop at last!” ) ;
}
Go through the program and find out how it works. Also write the same program without using goto.