Header Ads

if – else:C Programming Language - Part 4.1

if – else



If

If something is true, then if the statistic is used to run some code. It is written as below
If (expression) statement
Here expression means that any logical or relational operation is used. What we learned in the relational and logical section of the operator chapter, now it will be useful. Such as

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
Int main (void)
{
    Int temperature = 35;
    If (temperature> 30) {
            Printf ("Todays weather is too hot.");
}
    Return 0;
}
Today's climate is hot or cold, writing a program to find out. Where I took an integer variable called temperature and whose value was 35; Then we saw that with the temperature of 30, we met with the. It is seen today whether the temperature is greater than 30. We used the larger then logical operator to look at.
Since the temperature is greater than 30, so if the code inside it means printf has worked. If the temperature was less than 30, then the program would not have given any output. See the following program, where the value of the temperature variable is 25;

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
Int main (void)
{
    Int temperature = 25;
    If (temperature> 30) {
            Printf ("Todays weather is too hot.");
}
    Return 0;
}
If we run the program, we can not see anything on the console. Because if we see if the value of temperature is greater than 30 Since the temperature of the temperature is not greater than 30, so if the codes inside it will not run.
Now write a program, which will take the value of the user from the temperature. Then the console will show a message

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
Int main (void)
{
    Int temperature;
    Printf ("Enter temperature value:");
    Scanf ("% d", & temperature);
    If (temperature> 30)
    Printf ("Todays weather is too hot.");
    If (temperature <30) {
        Printf ("Todays weather is cool.");
    }
    Return 0;
}
Here's the temperature from the first user. If I checked using an if the temperature is greater than 30, then the output of the console is Todays weather is too hot. And if the temperature is less than 30 then the output will be Todays weather is cool.

If we want we can write more than one statement. Again if we can use one if we have another. Which is called nested if

If - else

If a logical test is true, then something will work. If it is a lie, then another task. If else from logic For example, if it is Friday, sleep. If not, then go to school / college by making bag. If "if you do not," if-else.

1
2
If (expression) statement 1
Else statement2;

This statement here implies, if expression is true, then statement 1 will work. If false, then statement2 will work. Write a program

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
Int main (void)
{
    Int day;
    Printf ("Enter value of day:");
    Scanf ("% c", & day);
    If ((day == 'F') || (day == 'f')) {
      Printf ("You can sleep today!: D");
    } Else {
        Printf ("oh no! Have to go to class: '(");
    }
    Return 0;
}
Here's what we did today, input the first letter of the day and put it in a variable called day. Then if we do a logical test here (day == 'F'). If the day's value matches F [means Friday, Friday] with curator, then you will print "You can sleep today!" : D "Because today is Friday. And if another curator inputs, that means, if any other day of the week, then it will be written "oh no! Have to go to class: '("

If - else is easy, is not it?

In the above program, maybe somebody can give either F or F as the first letter on Friday. Now if someone gives f input to the above program, then it will be written "oh no! Have to go to class: '("." But today, Friday, we have to fix the program so that any one or F or any of the input is written "You can sleep today!

Write the program below for him


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
Int main (void)
{
    Int day;
    Printf ("Enter value of day:");
    Scanf ("% c", & day);
    If ((day == 'F') || (day == 'f')) {
      Printf ("You can sleep today!: D");
    } Else {
        Printf ("oh no! Have to go to class: '(");
    }
    Return 0;
}
We've changed the condition a little bit. Writing: if ((day == 'F') || (day == 'f'))

Here || Means or means or We know it in the logical operator section. Now (day == 'F') or (day == 'f'), if any one is true, our program will print "You can sleep today!" : D "otherwise print" oh no! Have to go to class: '("


And if he is not else - if - else. It's an extra part. Below are some statements

1
If (x == 1) pfrintf ("% d", 1);
Here if x = 1 is then pfrintf ("% d", 1); The statement works and print 1. And if x = 1 is not there then pfrintf ("% d", 1); The statement will not work. A complete program

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
Int main (void)
{
    Int x;
    Printf ("Enter value of X:");
    Scanf ("% d", & x);
    If (x == 1) {
        Printf ("value of X is 1");
    }
    Return 0;
}

1
2
If (a> b) printf ("a is greater then b");
Else printf ("a is less then or equal to b");
If here a <b, that means if a is smaller than b, then printf ("a is greater then b"); The statement will work and print a is grater then b. And if a <b is not printf ("a is greater then b"); Will not work. Else go to the part and printf ("a is less then b"); The statement will work then print a less then b.

Full program

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
Int main (void)
{
    Int a, b;
    Printf ("input the valu of a:");
    Scanf ("% d", & a);
    Printf ("input the valu of b:");
    Scanf ("% d", & b);
    If (a> b) printf ("a is greater then b");
    Else printf ("a is less then or equal to b");
    Return 0;
}
Just use the if statement to write a program for the smallest of the two numbers in the numbers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
Int main (void)
{
    Int x, y;
     
    Printf ("input the valu of x:");
    Scanf ("% d", & x);
     
    Printf ("input of valu of y:");
    Scanf ("% d", & y);
     
    If (x> y) printf ("x is greater then y \ n");
    If (x <y) printf ("x is less then y \ n");
    If (x == y) printf ("x is equal to y \ n");
     
    Return 0;
}
In this program you will get two number input from you. After that, which of them is big, small or equal. If using the if-else statement, write a program for the smallest number in two numbers:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
Int main (void)
{
    Int x, y;
    Printf ("input the valu of x:");
    Scanf ("% d", & x);
      
    Printf ("input of valu of y:");
    Scanf ("% d", & y);
      
    If (x> y) printf ("x is greater then y \ n");
    Else printf ("x is less then y \ n");
}
The program here is just like the previous one. But do not show equality here. See the program below to see equality

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
Int main (void)
{
    Int x, y;
    Printf ("input the valu of x:");
    Scanf ("% d", & x);
    Printf ("input of valu of y:");
    Scanf ("% d", & y);
    If (x> y) printf ("x is greater then y \ n");
    If (x == y) printf ("x is equal to y \ n");
    Else printf ("x is less then y \ n");
}
A program for passing and failing

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
Int main (void)
{
    Int x;
    Printf ("input your number:");
    Scanf ("% d", & x);
      
    If (x> 40 && x <100)
    Printf ("Congratulation !!! You've passed");
    Else ("Ops !! You have failed.");
}
Here, if you can give the mark in the input of anxam, then it will show whether it is passing or failing.

Nested if-else
If I have another if / else block inside an if block, then nested if-else. See the following program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
Int main ()
{
  Int grade;
  Printf ("Type in your grade:");
  Scanf ("% d", & grade);
  If (grade <10) {
    Printf ("Oh no!");
  }
  Else {
    If (grade <40) {
      Printf ("You failed. \ N");
    }
    Else {
      Printf ("You passed! \ N");
      If (grade> = 90) {
        Printf ("And you got an A! \ N");
      }
    }
  }
  Return 0;
}


No comments

Theme images by sebastian-julian. Powered by Blogger.