Tuesday, November 30, 2010

CONTROL STATEMENTS IN C

DOWNLOAD PROGRAMS1
DOWNLOAD PROGRAMS2
C provides two styles of flow control:
  • Branching
  • Looping
Branching is deciding what actions to take and looping is deciding how many times to take a certain action.

Branching:

Branching is so called because the program chooses to follow one branch or another.

if statement

This is the most simple form of the branching statements.
It takes an expression in parenthesis and an statement or block of statements. if the expression is true then the statement or block of statements gets executed otherwise these statements are skipped.
NOTE: Expression will be assumed to be true if its evaulated values is non-zero.
if statements take the following form:
Show Example
if (expression)
  statement;

or

if (expression)
  {
    Block of statements;
  }

or

if (expression)
  {
    Block of statements;
  }
else
  {
    Block of statements;
  }

or

if (expression)
  {
    Block of statements;
  }
else if(expression)
  {
    Block of statements;
  }
else
  {
    Block of statements;
  }

? : Operator

The ? : operator is just like an if ... else statement except that because it is an operator you can use it within expressions.
? : is a ternary operator in that it takes three values, this is the only ternary operator C has.
? : takes the following form:
Show Example
if condition is true ? then X return value : otherwise Y value;

switch statement:

The switch statement is much like a nested if .. else statement. Its mostly a matter of preference which you use, switch statement can be slightly more efficient and easier to read.
Show Example
switch( expression )
     {
        case constant-expression1:      statements1;
        [case constant-expression2:     statements2;]    
        [case constant-expression3:     statements3;]
        [default : statements4;]
     }

Using break keyword:

If a condition is met in switch case then execution continues on into the next case clause also if it is not explicitly specified that the execution should exit the switch statement. This is achieved by using break keyword.
Try out given example Show Example

What is default condition:

If none of the listed conditions is met then default condition executed.
Try out given example Show Example

Looping

Loops provide a way to repeat commands and control how many times they are repeated. C provides a number of looping way.

while loop

The most basic loop in C is the while loop.A while statement is like a repeating if statement. Like an If statement, if the test condition is true: the statments get executed. The difference is that after the statements have been executed, the test condition is checked again. If it is still true the statements get executed again.This cycle repeats until the test condition evaluates to false.
Basic syntax of while loop is as follows:
Show Example
while ( expression )
{
   Single statement 
   or
   Block of statements;
}

for loop

for loop is similar to while, it's just written differently. for statements are often used to proccess lists such a range of numbers:
Basic syntax of for loop is as follows:
Show Example
for( expression1; expression2; expression3)
{
   Single statement
   or
   Block of statements;
}
In the above syntax:
  • expression1 - Initialisese variables.
  • expression2 - Condtional expression, as long as this condition is true, loop will keep executing.
  • expression3 - expression3 is the modifier which may be simple increment of a variable.

do...while loop

do ... while is just like a while loop except that the test condition is checked at the end of the loop rather than the start. This has the effect that the content of the loop are always executed at least once.
Basic syntax of do...while loop is as follows:
Show Example
do
{
   Single statement
   or
   Block of statements;
}while(expression);

break and continue statements

C provides two commands to control how we loop:
  • break -- exit form loop or switch.
  • continue -- skip 1 iteration of loop.
You already have seen example of using break statement. Here is an example showing usage of continue statement.
#include 

main()
{
    int i;
    int j = 10;

    for( i = 0; i <= j; i ++ )
    {
       if( i == 5 )
       {
          continue;
       }
       printf("Hello %d\n", i );
    }
}
This will produce following output:
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10

Sunday, November 28, 2010

QUESTIONS ON CONTROL STRUCTURES


1. 
How many times "IndiaBIX" is get printed?
#include<stdio.h>
int main()
{
    int x;
    for(x=-1; x<=10; x++)
    {
        if(x < 5)
            continue;
        else
            break;
        printf("IndiaBIX");
    }
    return 0;
}
A.Infinite timesB.11 times
C.
0 times
D.10 times
Answer: Option C
Explanation:
No answer description available for this question. Let us discuss.


2. 
How many times the while loop will get executed if a short int is 2 byte wide?
#include<stdio.h>
int main()
{
    int j=1;
    while(j <= 255)
    {
        printf("%c %d\n", j, j);
        j++;
    }
    return 0;
}
A.Infinite timesB.
255 times
C.256 timesD.254 times
Answer: Option B
Explanation:
The while(j <= 255) loop will get executed 255 times. The size short int(2 byte wide) does not affect the while() loop.


3. 
Which of the following is not logical operator?
A.
&
B.&&
C.||D.!
Answer: Option A
Explanation:
Bitwise operators:
& is a Bitwise AND operator.
Logical operators:
&& is a Logical AND operator.
|| is a Logical OR operator.
! is a NOT operator.
So, '&' is not a Logical operator.


4. 
Which is the correct order of mathematical operators ?
A.Division, Multiplication, Modulus
B.
Division, Multiplication, Subtraction
C.Modulus, Addition, Division
D.Addition, Division, Modulus, Subtraction
Answer: Option B
Explanation:
Simply called as BODMAS (Bracket of Division, Multiplication, Addition and Subtraction).


5. 
Which of the following cannot be checked in a switch-case statement?
A.CharacterB.Integer
C.
Float
D.enum
Answer: Option C
Explanation:
The switch/case statement in the c language is defined by the language specification to use an int value, so you can not use a float value.

switch( expression )
{
    case constant-expression1:    statements 1;
    case constant-expression2:    statements 2;    
    case constant-expression3:    statements3 ;
    ...
    ...
    default : statements 4;
}
The value of the 'expression' in a switch-case statement must be an integer, char, short, long. Float and double are not allowed.