C Decision making(conditional Statements)
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
Less than: a < b Not Equal to: a != b
Less than or equal to: a <= b
Conditional Statements
You can use these conditions to perform different actions for different decisions.
C has the following conditional statements:
i. Use if to specify a block of code to be executed, if a specified condition is true
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
iv. Use switch to specify many alternative blocks of code to be executed
The if Statement
Use the if statement to specify a block of code to be
executed if a condition is true.
if. Statements
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
The else Statement
Use the else statement to
specify a block of code to be
executed if the condition
is false.
If..else statement example
If else Example explained
In the example above, time (20) is greater than 18, so the condition
is false.
Because of this, we move on to the else condition and print to the
screen "Good evening". If the time was less than 18, the program
would print "Good day".
C Else If statements
The else if Statement
Use the else if statement to specify a new condition if the
first condition is false.
Else if statement syntax
Else if statement example
Else if Example explained
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 switch statement selects one of
many code blocks to be executed:
This is how the switch expression works:
• The switch expression is evaluated once
• The value of the expression is compared with the values of each case
• If there is a match, the associated block of code is executed
• 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:
int main() { printf("Wednesday");
int day = 4; break;
switch (day) { case 4:
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
The break Keyword The default Keyword
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.
When a match is found, and the job is done,
it's time for a break. There is no need for
more testing.
A break can save a lot of execution time
because it "ignores" the execution of all the
rest of the code in the switch block.
C Loops
Loops
Loops can execute a block of code as long as
a specified condition is reached.
Loops are handy because they save time,
reduce errors, and they make code more
readable.
While Loop
The while loop loops through a block of code
as long as a specified condition is true:
While loops
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
The Do/While Loop
The do/while loop is a variant
of the while loop.
This loop will execute the
code block once, before
checking if the condition is
true, then it will repeat the
loop as long as the condition is
true.
do/while loop
The example below uses a do/while loop.
The loop will always be executed at least once, even if the condition is false,
because the code block is executed before the condition is tested:
C For Loop
For Loop
When you know exactly how many
times you want to loop through a
block of code, use the for loop instead
of a while loop:
For loops
Expression 1 is executed (one time) before the execution of the code block.
Expression 2 defines the condition for executing the code block.
Expression 3 is executed (every time) after the code block has been executed.
The example below will print the numbers 0 to 4:
For loop Example explained
Expression 1 sets a variable before the loop starts (int i = 0).
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