6.Decision Making in C Programming l C Tutorials for beginers #6

6.Decision Making in C Programming l C Tutorials for beginers#6

https://harshaup.blogspot.com/2021/06/6decision-making-in-c-programming-l-c.html

In decision making user gives condition in program ,that statement is true or false.

1.if statement

2.if else statement

3.nested if statement

4.switch case

5.nested switch case

6.goto statement

If statement in c Program

ans: It contain boolean expression(True or false) followed by two or more statement.

Syntax:

if(expression)

{

statement will be executed if expression is true.

}

example:

include <stdio.h>
 
void main()
{
   int x = 1;
 
   if ( x == 1 ){
      printf("x is equal to one.\n");
   }
      printf("For comparison use == as = is the assignment operator.\n");
 
   }

Output:
x is equal to one.

If else statement in c Program

ans: if is followed by else statment which will be executed on wrong statement.

Syntax:

if(expression)

{

statement will be executed if expression is true.

}

else

{

statement will be executed if above expressiono is wrong

}

example:

include <stdio.h>
 
void main()
{
   int x = 1;
 
   if ( x == 2 ){
      printf("x is equal to one.\n");
   }
     else 
    printf("x is not equal to one.\n");
   }

Output:
x is not equal to one.

Nested If  statement in c

ans: Nested if means you can use more than one if under another if.

Syntax:

if(boolean expression1)

{

executes when expression 1 is true.

if(boolean expression2)

executed when expression 2 is true.

}

example:

#include <stdio.h>
 
void main()
{
   int n=11, c=45;
 
    if ( n == 11 )
      
   {
    if ( c == 45)
             
       {
       
          printf("value of n is %d \n",n);
      
          printf("value of c is %d \n",c);
   }
  }
}

Output:
Value of n  is 11
Value of c is 45

Nested If else statement in c

ans: Nested if else means you can use more than one if else under another if else.

example:

 
#include <stdio.h>
int main()
{
    int number1, number2;
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);

    //checks if two integers are equal.
    if(number1 == number2)
    {
        printf("Result: %d = %d",number1,number2);
    }

    //checks if number1 is greater than number2.
    else if (number1 > number2)
    {
        printf("Result: %d > %d", number1, number2);
    }

    // if both test expression is false
    else
    {
        printf("Result: %d < %d",number1, number2);
    }

    return 0;
}

Output

Enter two integers: 12
23
Result: 12 < 23

Goto statement in c

ans: It performs one way transfer of control to another line of code.

Goto statement Syntax

goto label;

label:

statement;

example :

#include 
void main() 
{ 
     goto patna; 
      begusarai: 
         printf("i am in begusarai \n"); 
    goto deoghar; 
          patna: 
          printf("i am in patna \n"); 
   goto begusarai; 
          deoghar: 
         printf("i am in deoghar \n"); 
}
   

Nested Switch case  statement in c

ans: You can use Switch Case inside another switch case.

Switch case  Syntax

switch(expression)

{

case constant-expression :

statement;

switch(expression)

case constant-expression :

statement;

break;

case constant-expression :

statement;

break;

default :

statement;

}

example :

#include 
int main () 
{ 
   int a = 100; int b = 200; 
    switch(a) 
{ 
     case 100: printf("This is part of outer switch\n", a ); 
         switch(b) 
{ 
     case 200: printf("This is part of inner switch\n", a ); 
    } 
   } 
   printf("Exact value of a is : %d\n", a ); 
printf("Exact value of b is : %d\n", b );
 return 0; 
}

Switch case  statement in c

ans: It is a control statement which contorls there statement using case, case is only executed if the case is true.

Switch case  Syntax

switch(expression)

{

case constant-expression :

statement;

break;

case constant-expression :

statement;

break;

default :

statement;

}

example :

#include<stdio.h>
 
void main()
{
   int n=3;
 
   
   switch(n)
   {
      case 1:
            printf("1\n");
              break;
      case 2:
           printf("2\n");
              break;
      case 3:
              printf("3\n");
              break;
 
      default:
              printf("you select other no from 1,2,3\n");
   }
 
   
}   

Ouput:

3

problems :-

1.// C Program to check whether a number is even or odd #include<stdio.h> int main(){ int a, b; printf("Enter a number\n"); scanf("%d", &a); if(a%2==0){ printf("%d is even\n", a); } else{ printf("%d is odd\n", a); } return 0; }

2.#include<stdio.h> int main(){ int age; printf("Enter your age\n"); scanf("%d", &age); // if(age!=90){ if(age>=90){ printf("You are above 90, you cannot drive\n"); } else{ printf("You can drive\n"); } if(age==50){ printf("Half Century\n"); } return 0; }

3.#include<stdio.h> int main(){ int num; printf("Enter your number\n"); scanf("%d", &num); if(num==1){ printf("Your number is 1\n"); } else if (num == 2) { printf("Your number is 2\n"); } else if (num == 3) { printf("Your number is 3\n"); } else{ printf("Its not 1, 2 or 3!\n"); } return 0; }

4.#include<stdio.h> int main(){ int rating; printf("Enter your rating (1-5)\n"); scanf("%d", &rating); switch(rating){ case 1: printf("Your rating is 1\n"); break; case 2: printf("Your rating is 2\n"); break; case 3: printf("Your rating is 3\n"); break; case 4: printf("Your rating is 4\n"); break; case 5: printf("Your rating is 5\n"); break; default : printf("Invalid rating!\n"); break; } return 0; }


No comments

Comments system

Powered by Blogger.