• We just launched and are currently in beta. Join us as we build and grow the community.

Conditional Statements

nothingmatters

Exploit Framework Developer
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
114
Likes
161
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Conditional Statements

In this part you will learn:
1. C syntax
2. Conditional Statements
3. Operators

In this tutorial I will teach you about conditional statements in C. The first program will be a basic one with a most basic conditional statement. The second one is a grade calculator.
First Program. Basic Conditional Statements
Basic Step:
Open Dev C++ then File > new > source file and start writing the code below.
  1. #include<stdio.h>
  2. #include<conio.h>

These two are the most common and basic header files. The first one stdio.h is used to grant us most of the basic functions of C like Input and Output functions. The second one conio.h provides us functions like getch() which is used to pause our screen until we press a button.
  1. int

    main(

    )
  2. {
  3. int

    a;
  4. char

    c;
  5. printf

    (

    "Enter an integer:"

    )

    ;
  6. scanf

    (

    "%d"

    ,&

    a)

    ;

  7. printf

    (

    "\n

    Enter a Character:"

    )

    ;
  8. scanf

    (

    " %c"

    ,&

    c)

    ;

Now at first we declared two variables. A integer variable and a character variable. We took the input from the user in these variables using scanf() function.
  1. if

    (

    a==

    2

    )
  2. {
  3. printf

    (

    "\n

    You Enter the right number."

    )

    ;
  4. }
  5. else
  6. {
  7. printf

    (

    "\n

    You Enter the wrong number."

    )

    ;
  8. }

  9. if

    (

    c==

    'M'

    )
  10. {
  11. printf

    (

    "\n

    You Enter the right character."

    )

    ;
  12. }
  13. else
  14. {
  15. printf

    (

    "\n

    You Enter the wrong character."

    )

    ;
  16. }


  17. getch

    (

    )

    ;
  18. }


This is the conditional statement i.e ‘if()’ and ‘else’ . We write our condition in brackets and in square brackets we write the code that we want to execute when our condition in if() is met. Like in this program I have placed a condition that if a is equal to 2 then the program should output a statement “ you entered the right number.” And if a is not equal to 2 then the ‘else’ part will execute and the program will output a statement “ you entered the wrong number.”. only one condition executes at a time either ‘if’ or else part. In the condition we enter ‘==’ this is the operator in C which means ‘equal to’ similar to ‘=’ in mathematics.
Same is the condition with the character variable too.
Execute > compile
then Execute > run
Output
1.png


Grade Calculator
Basic Step:
Open Dev C++ then File > new > source file and start writing the code below.
  1. #include<stdio.h>
  2. #include<conio.h>

These two are the most common and basic header files. The first one stdio.h is used to grant us most of the basic functions of C like Input and Output functions. The second one conio.h provides us functions like getch() which is used to pause our screen until we press a button.
  1. int

    main(

    )
  2. {
  3. int

    m;

  4. printf

    (

    "Enter you marks of any subject (out of 100) :"

    )

    ;
  5. scanf

    (

    "%d"

    ,&

    m)

    ;

At first we just declared an integer variable. Then afterwards we took input from the user in our integer variable. The user should enter his marks of any subject out of 100.
  1. if

    (

    m>=

    90

    &&

    m<=

    100

    )
  2. {
  3. printf

    (

    "\n

    Your grade is = A*"

    )

    ;
  4. }
  5. else

    if

    (

    m>=

    80

    &&

    m<=

    90

    )
  6. {
  7. printf

    (

    "\n

    Your grade is = A"

    )

    ;
  8. }

  9. else

    if

    (

    m>=

    70

    &&

    m<=

    80

    )
  10. {
  11. printf

    (

    "\n

    Your grade is = B"

    )

    ;
  12. }
  13. else

    if

    (

    m>=

    60

    &&

    m<=

    70

    )
  14. {
  15. printf

    (

    "\n

    Your grade is = C"

    )

    ;
  16. }
  17. else

    if

    (

    m>=

    50

    &&

    m<=

    60

    )
  18. {
  19. printf

    (

    "\n

    Your grade is = D"

    )

    ;
  20. }
  21. else

    if

    (

    m>=

    45

    &&

    m<=

    50

    )
  22. {
  23. printf

    (

    "\n

    Your grade is = E"

    )

    ;
  24. }
  25. else

    if

    (

    m<

    45

    &&

    m>=

    0

    )
  26. {
  27. printf

    (

    "\n

    You are Fail"

    )

    ;
  28. }
  29. else
  30. {
  31. printf

    (

    "\n

    Invalid number"

    )

    ;
  32. }

  33. getch

    (

    )

    ;
  34. }

In this part of code me place many statements. These statement compare the marks of the student with the grading criteria that we have set. You can set any condition you like. The program first checks the marks of the students with first condition. If it is a match with condition the program executes the code in the brackets and ends the program. Otherwise it checks the ‘else if() ‘ conditions. Note that only one statement executes in the whole program. It can be either if() or else if or else.
In the conditions we have used few operators that might be new to you.
The first is ‘>=’ operator this means “greater or equal to”.
The second is ‘=’ operator this means ”less or equal to”.
The third one is ‘&&’ operator this means ”AND”.
These are all logical operators.

Execute > compile
then Execute > run
Output
2.png

21.png


Book traversal links for Conditional Statements

  • ‹ Writing Your First C Program
  • Up
  • Switch Statement and Loops ›


Download
You must upgrade your account or reply in the thread to view hidden text.
 

439,009

316,008

316,017

Top