Conditional Statements (Programming Fundamental part-4)
Today we are going to learn about conditional statement in programming. Conditional statements are a way for computers to make decisions.
Like in real life we say that “If I complete this project today, I will eat burger”. Similarly in programming we write it in syntax form.
if is a reserved keyword used to create conditions in many programming languages. I will use C to explain.
bool task_completed = true;
if(task_completed == true){
printf("You can eat burger");
}Here you can see in the program we told the computer to check if task_completedis equal to true then print “You can eat burger” on screen.
Get Pixel and Code’s stories in your inbox
Join Medium for free to get updates from this writer.
For those who dont know we use == in C to check if something is equal to other. The condition task_completed == truebecomes true because value of our variable is true. If it’s not true, it becomes false. The code inside the if block runs only when the condition is true.
okay, you have learned if statement but now what if you wanted to do something else if task has not been completed by you. Let's say you wanted to eat pizza if the task is not completed.
To say that to computer we use else after if.
bool task_completed = false;
if(task_completed == true){
printf("You can eat burger");
}
else{
printf("Eat Pizza");
}Here we said that print “You can eat burger” if task_completed is true but print “Eat Pizza” if task_completed is anything else than true. In our case it is false, So, the computer will run the code in else block and print “Eat Pizza”.







