Monday, April 28, 2025

C Tutorial - 7 - If..Else

#include <stdio.h>

int main()
{
    if (10 < 999)
    {
        printf("left is smaller than right \n");
    }

    return 0;
}

Result:

left is smaller than right

Process returned 0 (0x0)   execution time : 0.048 s
Press any key to continue.

This C program demonstrates the use of an if statement to check whether the left-hand side of the comparison 10 < 999 is smaller than the right-hand side.

When the program is run, it will print "left is smaller than right" because the condition is true. If the condition were false, then the code inside the if block would not be executed.

Here's how the program works:

  • The program includes the standard input/output library stdio.h.
  • The main() function is defined, which returns an integer.
  • Inside the main() function, an if statement is used to check whether the condition 10 < 999 is true.
  • If the condition is true, then the code inside the curly braces {...} is executed. In this case, the code simply prints the message "left is smaller than right" to the console using printf() function.
  • The return statement is used to exit the program and return a value of 0 to the operating system.
#include <stdio.h>

int main()
{
    int first = 5;
    int second = 5;

    if (first < second)
    {
        printf("first is smaller than second \n");
    }
    else if (first > second)
    {
        printf("first is bigger than second \n");
    }
    else
        printf("numbers are equal \n");

    return 0;
}

Result:

numbers are equal

Process returned 0 (0x0)   execution time : 0.060 s
Press any key to continue.

This C program compares two integer variables first and second and prints a message depending on their values.

In the if statement, it checks whether first is less than second. If it is true, it prints the message "first is smaller than second".

If the if statement condition is not true, it moves to the else if block and checks whether first is greater than second. If it is true, it prints the message "first is bigger than second".

If neither the if statement nor the else if statement conditions are true, it moves to the else block and prints the message "numbers are equal".

At the end, the program returns 0, indicating successful execution.

 

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...