CONDITIONAL STATEMENTS AND LOOPS PART-ll



The switch Statement
           Instead of using the if-else-if ladder, the switch statement is available in programing language C for handling multiple choices.The general from of the switch construct is:

switch (variable)   {
case  constant1:
statemant(s);
break;
case constant2 :
statement(s);
break;
……………..
default :
statement(s);   }







          The switch control statement begins with the switch keyword followed by one block which contains the different cases each case handles the statements corresponding to an option (a satisfied condition) and ends with the break statement which transfers the control out of the switch structure to the original program. Note that the variable between the parentheses following the swith keyword is used to test the condition and is called as the control variable. If result is evaluated as “constant1”, the “case constant1:” is executed. If the value evaluted as “constant2”, the “case constant2:”, is executed and so on. If the value of the variable dos not correspond to any case, the default case is executed.
For example:
#include<stdio.h>
main() {
int day;
printf("enter day number:");
scanf("%d",&day);
switch(day)
{ case 1:   printf("monday");
break;
case 2:     printf("tuesday");
break;
case 3:     printf("wednesday");
break;
case 4:    printf("thursday");
break;
case 5:    printf("friday");
break;
case 6:   printf("satarday");
break;
case 7:  printf("sunday");
break;
default:  printf("wrong day number");   } }
Output:








THE LOOP CONSTRUCTS
      The loop or Iteration construct, directs a program to perform a set of operations again and again until a specified condition is achived. This condition causes the termination of the loop. Programming language C contains three statements for looping:

  • The while loop 
  • The do   while loop 
  • The for loop

     The while and do-while loop constructs are more suitable where prior knowledge of the terminating condition is not known. While loop evaluates a test expression before allowing entry into the loop, whereas do-while is executed atleast one before it evaluates the test expression which is available at the end of the loop.
      The for loop construct is appropriates when in advance it is known as to how many times the loop will be executed.
       The control statement may be given in the starting of the looping construct or in the end. Depending upon wether  the control statement is given at the Entery or Exit of the looping construct, looping constructs are classified into following two types.
Entery controlled loop
Exit controlled loop
      In Entry controlled loop, the control statement (test condition) is given at the entry of the loop, if it evaluates to true then only the compiler reads and executes the body of the loop, otherwise it exits the loop and executes next statement in seqence.
     In Exit controlled loop, first the loop body is executed and then the test condition is tested. If it evaluates to true, then only the next iteration takes place. As condition is tested after execution of loop body. So, even if condition is false, the loop body gets executed once.








1.  While loop:
      While loop is an Entry controlled loop. While loop construct contains the condition first. If the condition is satisfied, the control executes the statements following the while loop else, it ignores these statements. The general form of while loop is:
while (condition)    {
statement1 ;
statement2 ;
……………….       }
For example:
#include<stdio.h>
main()  {
int c = 1;
while (c <= 3) {
printf("Hello World \n");
c++;   }
printf("Is Done");   }
Output:

CONDITIONAL STATEMENTS AND LOOPS PART-ll CONDITIONAL STATEMENTS AND LOOPS PART-ll Reviewed by vijay pratap singh on September 12, 2017 Rating: 5

No comments:

Powered by Blogger.