C Programming

4-Structured Programming in C Programming Language

C Programming Lessons, Learn C Programming, Introduction to C Language, How to Program with C

In our previous articles, we introduced the C language and processed basic expressions, operators and operands.

With this article, we will start to create simple programs in C and continue with applications and various examples in the next lessons.

Structured Programming in C

C has 3 kinds of selection structures. These are;

  • if selection structure
  • if/else selection structure
  • switch selection structure

if : if it is true, it does the operation. if not, it goes to the next line

else/if : executes one command if the condition is true, executes another command if not

switch : executes one of the different operations according to the value of a statement

if (grade>=60)						if (grade>=60)
	printf(“Passed\n”);					printf(“Passed\n”);
							else
								printf(“Failed\n”);

C allows to use the conditional operator (?:) , which is very similar to the if/else structure.

This operator is the only ternary operator of C. This operator takes 3 operands.

The first operand is the condition itself.

The second operand is the value of the entire conditional statement if the condition is true, and the third operand is the value of the entire conditional statement if the condition is false.

            printf(“%s\n”, grade>=60 ? “Passed” : “Failed”);

or        grade>=60 ? printf(“Passed\n”) : printf(“Failed\n”);          

The conversion specifier %s is used here to print characters.

Simple Example for School Grades

if (grade>=90)
	printf(“A\n”);
else
	if (grade>=80)
	printf(“B\n”);
else if (grade>=70)
	printf(“C\n”);
else if (grade>=60)
	printf(“D\n”);
else
	printf(“F\n”);

*Hints:

1)         else                             statement equals to               else if (grade..

               if (grade..

2) If the body of an if…else statement has only one statement, you do not need to use brackets {}. But for good programming example and ease of reading the code it is recommended to use brackets.

if (x > y) {
    printf("3");
}
printf("5");
if (x > y)
    printf("3");
printf("5");

Now let’s look at the comparison example of 2 numbers entered from the keyboard

#include<stdio.h>
int main()
{
	int num1, num2;
	printf("Enter 2 integers \n");
	printf("these two numbers will be compared: ");
	scanf("%d %d", &num1, &num2);

	if(num1 == num2)
           printf("%d equals to %d\n", num1,num2);
        if(num1 != num2)
           printf("%d is not equal to %d\n", num1,num2);
        if(num1 < num2)
           printf("%d is less than %d\n", num1,num2);
        if(num1 > num2)
           printf("%d is greater than %d\n", num1,num2);
        if(num1 <= num2)
           printf("%d is less than or equal to %d\n", num1,num2);
        if(num1 >= num2)
           printf("%d is greater than or equal %d\n", num1,num2);

    return 0;
}

C Programming Lessons. Structured Programming in C. if - else/if - switch Selection Structures. Starting C Programming. Operands Operators.
Entered 3 and 6
Entered 7 and 7
C Programming Lessons. Structured Programming in C. if - else/if - switch Selection Structures. Starting C Programming. Operands Operators.
9 and 4 entered

* The switch selection structure will be examined and exemplified in the loop control topic.

Leave a Reply

Your email address will not be published. Required fields are marked *