Docsity

Understanding Conditional Statements in C Programming, Schemes and Mind Maps of Computer science

Toggle sidebar
bg1
Conditional Statement
1. if Statement
The syntax of the if statement in is:
if (test expression)
{
// code
}
The if statement evaluates the test expression inside the parenthesis ().
If the test expression is evaluated to true, statements inside the body of if are executed.
If the test expression is evaluated to false, statements inside the body of if are not executed.
Exmaple:
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number < 0) {
printf("You entered %d.\n", number);
}
printf("The if statement is easy.");
return 0;
}
pf3
pf4
pf5

Partial preview of the text

Download Understanding Conditional Statements in C Programming and more Schemes and Mind Maps Computer science in PDF only on Docsity!

Conditional Statement

1. if Statement

The syntax of the if statement in is: if (test expression) { // code }  The if statement evaluates the test expression inside the parenthesis ().  If the test expression is evaluated to true, statements inside the body of if are executed.  If the test expression is evaluated to false, statements inside the body of if are not executed. Exmaple: #include <stdio.h> int main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number < 0) { printf("You entered %d.\n", number); } printf("The if statement is easy."); return 0;

2. if...else Statement

The syntax of the if..else statement is: if (test expression) { // run code if test expression is true } else { // run code if test expression is false }

  1. If the test expression is evaluated to true,  statements inside the body of if are executed.  statements inside the body of else are skipped from execution.
  2. If the test expression is evaluated to false,  statements inside the body of else are executed  statements inside the body of if are skipped from execution.

3. if...else Ladder

Syntax of if...else Ladder if (test expression1) { // statement(s) } else if(test expression2) { // statement(s) } else if (test expression3) { // statement(s) } else { // statement(s)

Exmple: #include <stdio.h> int main() { int number1, number2; printf("Enter two integers: "); scanf("%d %d", &number1, &number2); if(number1 == number2) { printf("Result: %d = %d",number1,number2); } else if (number1 > number2) { printf("Result: %d > %d", number1, number2); } else { printf("Result: %d < %d",number1, number2); } return 0;