You are on page 1of 6

/*Logical operators ! not !

!= - not equal == - equal during comparison && - and || - or */ #include<iostream> using namespace std; void main() { int x; cout<<"Enter integer >10"<<endl; cin>>x; if(x>10) cout<<"That's right"<<endl; else//you don't put condition //in else cout<<"it has to be >10"<<endl; } #include<iostream> using namespace std; void main() { int x; cout<<"Enter integer >10"<<endl; cin>>x;

if(x>10) cout<<"That's right"<<endl; if(x<10) cout<<"it has to be >10"<<endl; } #include<iostream> using namespace std; void main() { cout<<"Enter test score"<<endl; float score; cin>>score; if(score>=50 && score<=100) cout<<"Pass"<<endl; if(score<50 && score>=0) cout<<"Fail"<<endl; if(score>100 || score<0) cout<<"Wrong score"<<endl; } #include<iostream> using namespace std; void main() { cout<<"Enter test score"<<endl; float score; cin>>score;

if(score>=50 && score<=100) //by default only one line is //executed with if statement //if you want more lines - put {} { cout<<"Pass"<<endl; cout<<"Great"; } if(score<50 && score>=0) cout<<"Fail"<<endl; if(score>100 || score<0) cout<<"Wrong score"<<endl; } #include<iostream> using namespace std; void main() { cout<<"Enter test score"<<endl; float score; cin>>score; //nested if statement //one if inside the other if(score<=100) { //any if statement is a scope if(score>=50) cout<<"Pass"<<endl; else

{ if(score>0) cout<<"Fail"<<endl; else cout<<"Wrong score"<<endl; } } else cout<<"Wrong score"<<endl; }

#include<iostream> using namespace std; void main() { ////you can use only int and char with //switch statement int choice; cout<<"Press 1 to continue"<<endl; cout<<"Press 2 to exit"<<endl; cin>>choice; switch(choice) { case 1://same that if(choice==1) cout<<"continue"<<endl; break;//separates cases //or gets you out of the scope case 2: case 3: case 4: //same as choice ==2 || //choice ==3 || choice ==4

cout<<"exit"<<endl; break; default://for numbers that are not //listed in previous cases cout<<"Enter right number"<<endl; break; } }

#include<iostream> using namespace std; void main() { //break and continue for(int i=0; i<10; i++) { if(i==3) break; //when i==3 program will stop cout<<i<<endl; } }

#include<iostream> using namespace std; void main() { //break and continue

for(int i=0; i<10; i++) { if(i==3) continue; //will skip i=3 cout<<i<<endl; } }

You might also like