Code Scappers

Use of Break and Default with Switch Statement

This structure helps to make a decision from a number of choices. The switch statement is a multi−way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly.


Syntax :

switch( integer expression)

  {

      case 1 :

        statement;

      case 2 :

        statement;

      case 3 :

        statement;

      default :

        statement;

  }

The integer expression which follows the keyword switch is any C expression will yield an integer value. It could possibly be an integer constant like 1, 2 or 3, or an expression that evaluates to an integer.
If a case value matches the expression value, execution begins at that case. All case expressions must be different.
The case "default" is implemented if none of the other cases are satisfied. A default case is optional.
The cases and the default case can occur in any order.

C Programs using Switch statements

1.Write a C program to enter a grade & check its corresponding remarks using switch statement.

#include<stdio.h>

void main()

{

char g;

printf("Enter your grade: \n");

scanf("%c", &g);

switch(g)

{

  case 'A' :printf("Outstanding Performance!\n" );

  break;

  case 'B' :printf("Excellent!Keep it up\n" );

  break;

  case 'C' :printf("Well done!Continue with it\n" );

  break;

  case 'D' : printf("You passed!Work Harder\n" );

  break;

  case 'F' : printf("Failed!Better try again\n" );

  break;

  default : printf("Invalid grade\n" );

}

printf("Grade Obtained is %c\n", g );

}

Output: Enter your grade
B
Excellent!Keep it up
Grade Obtained is B

TOPIC 8: Program Loops and Iterations

while loop:

If the program needs to perform repetitive tasks, then while loops are used.
The program keeps repeatedly executing the statement inside the while until the condition becomes false(0).

NOTE 1:If the condition is initially false, the statement will not be executed


Syntax :

while(condition)

{

 statements....;

}

C Programs using While Loops

Write a C program to check if a number is a palindrome or not

#include<stdio.h>

void main()

{

int num, temp;

int rev = 0;

printf("Enter a number\n");

scanf("%d", &num);

temp = num;

while( temp != 0 )

{

  rev = rev * 10;

  rev =  rev+temp%10;

  temp = temp/10;

}

if ( num == rev )

  printf("The number %d is a palindrome.\n", num);

else

  printf("The number %d is not a palindrome.\n", num);

}

Output: Enter a number
12321
The number 12321 is a palindrome.

Enter a number
12000
12000 is not a palindrome number.

do while loop:

The body of the do-while executes at least once.
The do-while structure is similar to the while loop except the relational test occurs at the bottom (rather than top) of the loop.
The do-while tests for a positive relational test; that is, as long as the test is True, the body of the loop continues to execute.


Syntax :

do

{

set of C statements;

}

while (expression) 

NOTE 1: The test expression must be enclosed within parentheses, just as it does with a while statement.

C Programs using do while loop

1. Write a C program to add all the numbers entered by a user until user enters 0.


#include<stdio.h>

void main()

{
  int sum=0;
  int num;

  do

 {

       printf("Please enter the number\n");

       scanf("%d",&num);

       sum=sum+num;


 } while(num!=0);

printf("The sum is %d\n", sum);

}

Output: Please enter the number
3
Enter a number
-2
Enter a number
0
The sum is 1

2.Write a C program to print hello and the number 5 times.

#include<stdio.h>

void main()

{

int m = 5;

do

{

   printf("Hello %d\n", m );

    m = m -1;

} while ( m > 0 );

}

Output:
Hello 5
Hello 4
Hello 3
Hello 2
Hello 1

2.Write a C program to count the no of digits in a number

#include<stdio.h>
void main()

{

  int number;

  int c=0;

  printf("Enter an integer: ");

  scanf("%d", &number);

  do

   {

      number=number/10;

      c++;

   } while(number!=0);

  printf("The number of digits are : %d",c);

}

Output:Enter an integer:
33425
The total number of digits are:
5

<≪ Back Next >>