Showing posts with label Decision making statements in C. Show all posts
Showing posts with label Decision making statements in C. Show all posts

Sunday, September 28, 2014

BRANCHING AND LOOPING in C


6. Write short notes on the following: (JAN 2009)
for loop
while loop
dowhile loop
Switch case  (MAY 2009/FEB 2009/FEB 2010)

1. ITERATION, FOR LOOPS
The basic format of the for statement is,
for( start condition; continue condition; re-evaulation )
program statement;
/* sample program using a for statement */
#include <stdio.h>
main() /* Program introduces the for statement, counts to ten */
{
int count;
for( count = 1; count <= 10; count = count + 1 )
printf(“%d “, count );

printf(“\n”);
}
The program declares an integer variable count. The first part of the for statement for (count = 1; initialized the value of count to 1.
The for loop continues with the condition count <= 10; evaluates as TRUE. As
the variable count has just been initialized to 1, this condition is TRUE and so the
program statement printf(“%d “, count ); is executed, which prints the value of count to the screen, followed by a space character.
Next, the remaining statement of the for is executed count = count + 1); which adds one to the current value of count. Control now passes back to the conditional test, count <= 10; which evaluates as true, so the program statement printf(“%d “, count ); is executed.
Count is incremented again, the condition re-evaluated etc, until count reaches a value of
11.
When this occurs, the conditional test count <= 10; evaluates as FALSE, and the for loop terminates, and program control passes to the statement printf(“\n”); which prints a newline, and then the program terminates, as there are no more statements left to execute

2. THE WHILE STATEMENT
The while provides a mechanism for repeating C statements whilst a condition is
true. Its format is, while( condition ) program statement;
Somewhere within the body of the while loop a statement must alter the value of the condition to allow the loop to finish.
Example:
/* Sample program including while */
#include <stdio.h>
main()
{
int loop = 0;
while( loop <= 10 ) {
printf(“%d\n”, loop);
++loop;
}
}
The above program uses a while loop to repeat the statements
printf(“%d\n”,loop); ++loop; the value of the variable loop is less than or
equal to 10.
3. THE DO WHILE STATEMENT
The do { } while statement allows a loop to continue whilst a condition evaluates as TRUE (non-zero). The loop is executed as least once.
Example:
/* Demonstration of DO...WHILE */
#include <stdio.h>
main()
{
int value, r_digit;
printf(“Enter the number to be reversed.\n”);
scanf(“%d”, &value);
do {
r_digit = value % 10; printf(“%d”, r_digit); value = value / 10;
} while( value != 0 );
printf(“\n”);
}
The above program reverses a number that is entered by the user. It does this by using the modulus % operator to extract the right most digit into the variable r_digit. The original number is then divided by 10, and the operation repeated whilst the number is not equal to 0.

Saturday, September 27, 2014

Decision making statements in C

 Explain about the various decision making statements in "C" language. (JAN 2009/FEB2010)

1. IF STATEMENTS
DECISION MAKING
The if statements allows branching (decision making) depending upon the value or state of variables. This allows statements to be executed or skipped, depending upon decisions.
The basic format is,
if( expression )
program statement;
Example:
if( students < 65 )
++student_count;
In the above example, the variable student_count is incremented by one only if the value of the integer variable students is less than 65. The following program uses an if statement to validate the users input to be in the range 1-10.
#include <stdio.h>
main()
{
int number;
int valid = 0;

while( valid == 0 ) {
printf(“Enter a number between 1 and 10 à”);
scanf(“%d”, &number);
/* assume number is valid */
valid = 1;
if( number < 1 ) {
printf(“Number is below 1. Please re-enter\n”);
valid = 0;
}
if( number > 10 ) {
printf(“Number is above 10. Please re-enter\n”);
valid = 0;
}
}
printf(“The number is %d\n”, number );
}
2. IF ELSE
The general format for these are,
if( condition 1 )
statement1;
else if( condition 2 )
statement2;
else if( condition 3 )
statement3; else statement4;
The else clause allows action to be taken where the condition evaluates as false (zero). The following program uses an if else statement to validate the users input to be in the range 1-10.
Example:
#include <stdio.h>
main()
{
int number;
int valid = 0;
while( valid == 0 ) {
printf(“Enter a number between 1 and 10 à”);
scanf(“%d”, &number);
if( number < 1 ) {
printf(“Number is below 1. Please re-enter\n”);
valid = 0;
}
else if( number > 10 ) {
printf(“Number is above 10. Please re-enter\n”);
valid = 0;
}
else
valid = 1;
}
printf(“The number is %d\n”, number );
}
This program is slightly different from the previous example in that an else clause is used to set the variable valid to 1. In this program, the logic should be easier to follow.