You are on page 1of 5

B046 Md Rafiquer Rahman

C Assignment 3
1. Write the syntax of nested if else.And write a program to demonstate
the same.
Solution:-
 Syntax of nested if else :-
if(condition) {
//Nested if else inside the body of "if"
if(condition2) {
//Statements inside the body of nested "if"
}
else {
//Statements inside the body of nested "else"
}
}
else {
//Statements inside the body of "else"
}
 Program to demonstrate nested if else:-
#include <stdio.h>
int main()
{
    int var1, var2;
   printf("Input the value of var1:");
   scanf("%d", &var1);
   printf("Input the value of var2:");
   scanf("%d",&var2);
   if (var1 != var2)
   {
    printf("var1 is not equal to var2\n");
    //Nested if else
    if (var1 > var2)
    {
        printf("var1 is greater than var2\n");
    }
    else
    {
        printf("var2 is greater than var1\n");
    }
   }
   else
   {
    printf("var1 is equal to var2\n");
   }
   return 0;
}

2. Write the syntax of switch - case statement.And write a program to


demonstate the same.
Solution:-
 Syntax of switch-case statement :-
switch (variable or an integer expression)
{
case constant:
//C Statements
;
case constant:
//C Statements
;
default:
//C Statements
;
}
 Program to demonstrate switch-case statement :-
#include<stdio.h>
int main()
{
     int num=2;
     switch(num+2)
     {
         case 1:
           printf("Case1: Value is: %d", num);
         case 2:
           printf("Case1: Value is: %d", num);
         case 3:
           printf("Case1: Value is: %d", num);
         default:
           printf("Default: Value is: %d", num);
    }
    return 0;
}

3.Differentiate for,while and do-while loop.


Solution:-

For Loop While Loop Do while Loop

Syntax: For(initialization; Syntax: While(condition), { . Syntax: Do { .


condition;updating), { . Statements; . } Statements; }
Statements; } While(condition);

It is known as entry It is known as entry It is known as exit


controlled loop. controlled loop controlled loop

For Loop While Loop Do while Loop


If the condition is not true If the condition is not true Even if the condition is not
first time than control will first time than control will true for the first time the
never enter in a loop. never enter in a loop. control will enter in a loop.
There is no semicolon; after There is no semicolon; after There is semicolon; after
the condition in the syntax of the condition in the syntax of the condition in the syntax
the for loop. the while loop. of the do while loop.
Initialization and updating is Initialization and updating is Initialization and updating
the part of the syntax. not the part of the syntax. is not the part of the syntax

4. Explain the use of break and continue.

Solution:-

 USE OF BREAK :-
If we want to jump out of a loop instantly, without waiting to get back to
the conditional test. 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.

 USE OF CONTINUE :-
If 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.

2. Write the syntax of goto statement.And write a program to


demonstate the same.

Solution:-

 Syntax of goto statement :-


//any scope
{
//statement(s)
[condition]
goto label_name;
//statement(s)
label_name;
//statement(s)
}
 Program to demonstrate goto statement :-
#include <stdio.h>

int main()
{
int num;
printf("Enter a positive integer: ");
scanf("%d",&num);
//if number is less than 0 then transferring the
//program control to QUIT
if(num<0)
goto QUIT;
printf("Entered number is: %d\n",num);
QUIT: //label
printf("BYE BYE!!!\n");
return 0;
}

You might also like