i am quite new to C, so my understanding of a few statements are unclear. first,
what is the meaning of selection, i know it has to do with programs being executed on basis of true of false, but a concrete defintion or example is still a bit unclear for me.
then, what could be a few good examples of if statement, if else, else statements
i did a few, dont know if i am correct, perhaps you could make some just to make it more clear.
if statement
# include<iostream.h>
void main()
{
int num1;
cout<<"Enter a number:";
cin>>num1;
if(num1>0)
cout<<"Number is greater then zero";
if(num1<0)
cout<<"Number is lesser than zero";
}
if else
#include <iostream.h> //Include the header file
void main() //Most important function
{
int age; //Variable declaration
cout<<"Enter your age please:"; //Prompt user for input
cin>>age; //Accept input
if(age>=18)
cout<<"Congrats! You are eligible to vote!";
else
cout<<"Sorry!You are not eligible!";
}
switch case
# include<iostream.h>
void main( )
{
int num1,num2,ch;
cout<<"\t\t\t MATH CALCULATOR\n\n\n";
cout<<"\t1. Add\n";
cout<<"\t2. Sub\n";
cout<<"\t3. Mul\n";
cout<<"\t4. Div\n";
cout<<"Enter 2 numbers:\n";
cin>>num1>>num2;
cout<<"Enter ur choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<num1+num2;
break;
case 2:
cout<<num1-num2;
break;
case 3:
cout<<num1*num2;
break;
case 4:
cout<<num1/num2;
break;
default:
cout<<"Wrong menu option!";
break;
}
}