Conditions in C Language - ProgrammerTech
Language:

Search

Conditions in C Language

  • Share this:
Conditions in C Language

Conditions in C Language

Conditions are one of the most important elements that must be provided in any programming language, whether it is in the C or C++ programming language or any other language. It plays a major role in every programming language, like building materials if I wanted to design a house.

In the previous lesson, we talked that the relational operators if there is a condition that depends on the comparison between a value and another value, where the result is either true (true) or false (false), now we will use it in this lesson.

Syntax

The conditional sentence consists of two parts, the first is the condition and the second is the commands when the condition is executed, and the conditional statements are if and switch.

If(condition){
Statment ; }

 

The conditional if statement in C.

The if statement is used to execute one or more sentences according to a specific condition in general. In the following we will attach several examples in the if statement.

Example 1

Write a program that tests the number if it is less than 5 that prints a Saja sentence.

#include<stdio.h>
main()
{
    int cs;
    printf("enter number  ");
    scanf("%d", &cs);
    if(cs <5)
  printf("Saja \n");
}

We note that I defined the variable named cs of type int, and then I made a print command using the printf function, which I entered a number, and then I used the input function, which is scanf. That is, we tell the user to enter the number you want, and you use a conditional sentence. If the number you entered is less than 5, print the word Saja, and if the number entered is greater than 5, nothing will be printed.

Example 2

Write a program that tests the number if it is less than 5 prints the Database sentence and if it is greater than 5 prints the word Saja.

#include<stdio.h>
main()
{
    int cs;
    printf("enter number  ");
    scanf("%d", &cs);
if(cs < 5)
    printf("Database \n");
if(cs >5)
    printf("Saja \n");
}

The first result: If the user enters the number 1, the word Database will be printed for us.

enter number 1
Database

The second result: If the user enters the number 10, the word Saja will be printed for us.

enter number 10
Saja

 

Using the if and else conditional sentences in C

Alternatively, we can use something called else, which means if if the first option is not met, then execute the second option.

Structure of the if and else conditional statement in C

if(condition)
    statement;
else
    statement;

An else statement is used if we want to execute a statement or group of statements and the if condition is false. The else statement must precede the if statement since the test cannot begin with the else statement alone.

Example 3

Write a program that tests the number if it is less than 5, prints the Saja sentence for us, and if the number is not 5, print the Database.

#include<stdio.h>
main()
{
    int cs;
    printf("enter number ");
    scanf("%d", &cs);
    if(cs <5)
    printf("Saja \n");
    else
    printf("Database \n");
}

When this code is executed and the user is asked to enter a number, if the user enters the number 4, it will print the word Saja for us, and if the number entered is 8, it will print the Database.

 

The else if conditional statement in C

The else if statement can be written if we want another test inside the else statement because there cannot be an else statement twice for a single if statement.

Example 4

Write a program that will test the number if it is less than 5, print the word Kaissar, and if it is equal to 5, print cs, and if it is not, it will print the database.

#include<stdio.h>
main()
{
    int doha;
    printf("enter number  ");
    scanf("%d" , &doha);
    if(doha <5)
    printf("Kaissar \n");
    else if (doha == 5)
    printf("cs \n");
    else
    printf("Database \n");
}

When the program is run, the user will be asked to enter a number. If the user enters numbers from 1 to 4, he will print the word Kaissar for us. If he enters the number 5, he will print cs for us. If he enters numbers from 6 and above, he will print the Database.

Example 5

I am writing a program that will test the number entered if my husband prints Saja and if otherwise prints the Database.

#include<stdio.h>
main()
{
    int doha;
    printf ("enter number  ");
    scanf("%d", &doha);
    if(doha%2==0)
    printf("Saja \n");
    else
    printf("Database \n");
}

When running this code, it will be as mentioned in the question text. If the entered number is even, meaning 2, 4, 6, or other even numbers, it will print Saja, and if the number is odd, such as 1, 3, 5 and other odd numbers, it will print. Database.

 

The switch conditional sentence in C language

switch is used to test a variable value with an integer constant value, where each constant value is considered as a condition or comparison, and the switch decision is similar to the if, if else decisions with some differences, and it contains a set of cases (case) and each case ends with the break statement.

The if clause can sometimes be replaced with it and it is more orderly than the if clause which contains many else, if clauses. The switch statement takes one numerical variable and then compares it to a number of casses that the programmer has set. If it is true, a certain statement is executed. The switch clause is used to compare values ​​of a variable with one number of values ​​it is likely to take.

Syntax

switch (var)
{
    case 1:
        statement1;
        statement2;
        break;
    case 5:
        statement1;
        break;
    default:
        statement1;
}

We write a switch, then put a variable or a numeric value in parentheses, then open the curly braces. And for each case we want, we write the statement case, which is the case, followed by the conditional value that represents the case, then two dots, and we put case1 if the value of var is equal to 1 then two points. And apply the following case for example statement1 and statement2 Any codes will be executed in case1 if var is 1 then we put break to end this line. We note that when writing case1 we do not put curly braces, and the switch clause can contain an infinite number of cases if, for example, case5 is the same thing in case1.

And finally, we terminate the switch statement in a general case default, which is executed if none of the other cases above apply, that is, it is similar to the else principle in the conditional if statement. Here we assume that var equals 2 will pass first and say does 2 equal 1 in case1 and it is no, and it will pass the same command on case5 and it will be no and then in the end it executes default.

Example 6

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(int argc, char** argv)
{
    int cost = 2;
    if (cost == 2)
    {
        printf("The price is cheap\n");
    }
    else if(cost == 3)
    {
        printf("The price is normal");
    }
    else if(cost == 4)
    {
        printf("the price is too high");
    }
    else
    {
        printf("Please check accounting");
    }
    switch (cost) {
        case 2:
            printf("The price is cheap\n");
            break;
        case 3:
            printf("The price is normal");
            break;
        case 4:
            printf("the price is too high");
            break;
        default:
            printf("Please check accounting");
        }
    return 0;
}

At the beginning of the previous example, we used the conditional if and else clauses, then we came to the switch condition, which is the topic of our current lesson, and we told it case 2, that is, if the value is 2 then print the price cheap. Then break the loop via break. Then we put another possibility, which is case 3, to print the normal price for us if the condition is met, and also the possibility of case 4 to print that the price is expensive. And in the event that none of the conditions above are met, it implements the default state that the accounting is reviewed.

Running the program we will get the result that the price is twice as cheap. We notice that the price is cheap twice, and they are the first when the conditional if statements have met the condition and the second also fulfills the condition of the first switch statement. If the break clause is not written in one of the states of the switch clause, it will jump to the next state even if its condition does not apply. For example, for our previous example, if the value of var is 1, the condition will be executed in case 1 and then it will jump to the next case instead of terminating the switch statement as a whole, so we must put break in all cases of the switch statement except for the default case that does not come after it Other cases unless the programmer intended for that to happen.

Example 7

#include<stdio.h>
main(){
int n1,n2 ;
char b;
printf("enter equation  ");
scanf("%d %c %d", &n1,&b,&n2);
switch(b)
{
case '+':printf("sum= %d \n",n1+n2); break;
case '-':printf("sub= %d \n",n1-n2); break;
case '*':printf("mult= %d \n",n1*n2); break;
case '/':printf("div= %d \n",n1/n2); break;
case '%':printf("div= %d \n",n1%n2); break;
default : printf("error entering \n "); break;
}
}

Tags:
Saja Alsadig

Saja Alsadig

من السودان مبرمجة مواقع ويب و تطبيقات أندرويد اتقن لغات "html, css, php, js" بالإضافة للتعامل مع اندرويد ستوديو و بناء التطبيقات, مهتمة بمجال التقنية منذ الصغر و كان لدي شقف كبير للدخول بالجامعه بقسم البرمجة و دخلته في عام 2013 و تعلمت كل شيئ ضمن الجامعة و خارجها من التعليم الذاتي و اتمنى أن أكون عند حسن ظن الجميع و إيصال الأمانة لكم, اعمل حاليا في إدارة موقع جامعة السودان.