Conditional Statements To Students
Conditional Statements To Students
Conditional Statements
C If ... Else
Greater than: a > b
Conditions and If Statements
Greater than or equal to: a >=
You have already learned that C b
supports the usual
logical conditions from mathematics: Equal to a == b
You can use these conditions to perform different actions for different decisions.
ii. Use else to specify a block of code to be executed, if the same condition is false
iii. Use else if to specify a new condition to test, if the first condition is false
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an
error.
In the example below, we test two values to find out if 20 is greater than 18. If the
condition is true, print some text:
if…statement example2
Example explained
In the example above we use
two variables, x and y, to test
whether x is greater than y
(using the > operator).
As x is 20, and y is 18, and we
know that 20 is greater than 18,
we print to the screen that "x is
greater than y".
C if… else
In the example above, time (20) is greater than 18, so the condition
is false.
In the example above, time (22) is greater than 10, so the first condition is false.
The next condition, in the else if statement, is also false, so we move on to the else condition
since condition1 and condition2 is both false - and print to the screen "Good evening".
However, if the time was 14, our program would print "Good day."
C Switch Statements
Switch Statement
Instead of
writing many If..else statements, you
can use the switch statement.
• The value of the expression is compared with the values of each case
• The break statement breaks out of the switch block and stops the execution
• The default statement is optional, and specifies some code to run if there is no
case match
Switch statement example1
Switch statement example2
#include <stdio.h> case 3:
case 1: printf("Thursday");
printf("Monday"); break;
break; case 5:
case 2: printf("Friday");
printf("Tuesday"); break;
break;
Switch statement example continued
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
}
return 0;
}
Switch statements
When C reaches a break keyword, it breaks The default keyword specifies some
out of the switch block. code to run if there is no case match:
This will stop the execution of more code
and case testing inside the block.
Loops
While Loop
In the example below, the code in the loop will run, over and over again, as long
as a variable (i) is less than 5:
Note: Do not forget to increase the variable used in the condition (i++), otherwise
the loop will never end!
C Do/While Loop
For Loop
Expression 1 is executed (one time) before the execution of the code block.
Expression 3 is executed (every time) after the code block has been executed.
Expression 2 defines the condition for the loop to run (i must be less than 5). If
the condition is true, the loop will start over again, if it is false, the loop will end.
Expression 3 increases a value (i++) each time the code block in the loop has
been executed.
C Nested Loops
Nested Loops
It is also possible to place a loop inside another loop. This
is called a nested loop.
The "inner loop" will be executed one time for each
iteration of the "outer loop":
Nested loops example