0% found this document useful (0 votes)
6 views13 pages

Programming Dzeipapo Volume 3

The document provides an overview of conditional statements in programming, explaining their importance in controlling program flow based on specific conditions. It details various types of conditional statements such as if, if-else, else if, and switch statements, along with their syntax and examples. Additionally, it introduces logical operations and includes programming exercises for practical application of the concepts discussed.

Uploaded by

mastermindjabha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Programming Dzeipapo Volume 3

The document provides an overview of conditional statements in programming, explaining their importance in controlling program flow based on specific conditions. It details various types of conditional statements such as if, if-else, else if, and switch statements, along with their syntax and examples. Additionally, it introduces logical operations and includes programming exercises for practical application of the concepts discussed.

Uploaded by

mastermindjabha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

By the #code scape

PROGRAMMING DZEIPAPO VOLUME3 team


Join our lessons for less
Or get your assignment
CONDITIONAL STATEMENTS help at
071 859 6126
WHAT ARE CONDITIONAL STATEMENTS

These are fundamental building blocks of programming that controls the flow basing on
conditions or decisions.
For example we want our program to print “ Zimbabwe open university” to the screen if the
student pin is “a1112223b”. The program will print Zimbabwe open university only if the pin
matches the one in the condition. This is the fundamental aspect in account searching and pin
checking in ATMs and other automated machines.
Conditional statements include if statements, if else statements, else if statements and the
switch statements, each of which have its own syntax.
If statements- used to execute a block of code if the condition is true
If else statements- used to execute one block of code if the condition is true and another if
the
condition is false.
Else if statements- used to check multiple conditions and execute different blocks of code
accordingly.
Switch statements – used to execute different blocks of code basing on the value of variable.
IF STATEMENTS
Syntax
If( condition){
//code
}
Eg.
Int number=10;
If(number==10){
printf(“ the number is %d”,number);
}
This will output “ the number is 10”.
Note the use of( == )to compare instead of( = )which is used to
assign values in variables
IF ELSE STATEMENTS
Syntax
If( condition){
//code
}else{
//another code here if the condition is false
}
Eg.
Int number=10;
If(number==10){
printf(“ the number is %d”,number);
}else{
printf(“the number is not 10”);
}
This will output “ the number is 10”.
If u change the value inside the variable to another number other than 10, it will output “the
number is not 10”
ELSE IF STATEMENTS
Syntax
If( condition){
//code
}else if(condition2){
//another code here if the condition is false
}else if(condition3){
//another code here
}else{
//code to execute value doesn’t meet any of the above conditions
Eg.
If(number==1){
printf(“ the number is %d”,number);
}else if(number==2{
printf(“the number is 2”);
}else {
printf(“number is none of the above);}
ELSE IF STATEMENTS
Syntax
Switch(variable){
case value1:
//code here
break;
case value2:
//code here
break;
case value3:
//code here
break;
Default:
//code here if the value doesn’t match any of the above conditions
break;
}
Example
int day;
printf(“ enter number of the day you wish to print”);
Scanf(“%d”,&day);
Switch(day){
case 1:
Printf(“Sunday”);
break;
case 2:
Printf(“Monday”);
break;
case 3:
Printf(“Tuesday”);
break;
case 4:
Printf(“Wednesday”);
break;
case 5:
Printf(“Thursday”);
break;
case 6:
Printf(“Friday”);
break;
case 7:
Printf(“Sartaday”);
break;
default:
Printf(“invalid input”);
break;
}
So this program uses if statements to print days of the week.
There are many satiations where u can use switch statements e.g.
creating calculator with
four operations which are addition, division, subtraction and
multiplication.
LOGICAL OPERATIONS
these are symbols or key words that are use to connect or modify conditions in programming.
Common logical operators
1) AND (&&). Returns true if both conditions are true
2) OR(||). Returns true if at least one condition is true
3) NOT (!) .Returns the opposite of the condition which means true becomes false and false
becomes true.

Using the && operator


int age;
char gender;
If(age>18&&gender==‘M’){
Printf(“ you are allowed to enter this night club”);
}
This program is checking for both gender and age to allow the person to enter a club.
If the person is under 18 and is a male, he is not allowed
if the person is under 18 and is a female , she is not allowed
if the person is above 18 and is a male, he is allowed
If a person is above 18 and is a female, she is not allowed
So the code will be executed only if both conditions are true.
Using the OR(||) operator
int age;
char gender;
If(age>18||gender==‘M’){
printf(“ you are allowed to enter this night club”);
}
This program is checking for either gender OR age to allow the person to enter
a club.
If the person is under 18 and is a male, he is allowed.
if the person is under 18 and is a female , she is not allowed
If the person is above 18 and is a male, he is allowed.
if the person is above 18 and is a female , she is allowed.
The programming is allowing all men( and only women above 18)
QUESTIONS
1) write a c program of a factory door that allows only men above 18 to enter and restrict men below 18 and all
women to enter.
2) write a C program that ticket number 1 to 20) OR VIP tickets holders to enter an earoplane , and restricts the
rest.
Tip: you can prompt the user like
print“(“ chooser your ticket category \n 1) VIP \n 2) ordinary”);
scanf(“ %d”,&category);
Print“(“ Enter your ticket number:”);
Scanf(“ %d”,&ticket-number);
3) Write a program for an ATM authentication process that allows user to withdraw money only if his/her card

number is “ 1111111” and his pin is “ 1111”;


4) Write a c program for a calculator that uses switch statements to performs basic arithmetic calculations such as
addition,
subtraction, multiplication and division .
5) Write a c program that print a respective unirvesity based on the user input for example 1 out put ZOU, 2
output UZ, 3
output NUST……
Compile your answers and send to 071 859 6126 and stand a chance to earn 20% discount on VIP lessons. If you
can’t answer the
questions don’t worry we are here to help you,
Message “VIP lessons” to 071 859 6126 an we will respond you
THE END
Next on #code scape CONTACT US;
PROGRAMMING DZEIPAPO JOIN OUR
VOLUME 4(LOOPS)
1) FREE CLASS
Books to search :
2) VIP CLASS
PROGRAMMING DZEIPAPO
V(1-10) 3) VVIP CLASS
For less
Message “ lessons” to 071
859 6126
And we will respond you

You might also like