15
15
#include<iostream>
using namespace std;
int main(){
for(int i = 0;i<5;i++){
cout<<"Bhavya";
break; // As this is encountered, the flow of program will take
exit from the loop
}
return 0;
}
//continue
#include<iostream>
using namespace std;
int main(){
for(int i = 0;i<5;i++){
if(i==2) // Here 0,1,3 and 4 will only be printed i.e. i = 2 iteration
will be skipped
continue; // Skip a particular iteration
cout<<i<<endl;
}
return 0;
}
//switch
#include<iostream>
using namespace std;
int main(){
int val;
cin>>val;
switch(val){ // expression should be integer or character
expression here
case 1:cout<<"Bhavya"; break; // always use break after each
case
case 2: cout<<"Bhava1";break;
case 3: cout<<"Bhavya2";break;
default: cout<<"Class is over";break;
}
return 0;
}
// scope of variable
#include<iostream>
using namespace std;
int global = 6;
int main(){
int a; // declared variable
int b = 5; // intialization of variable b
b = 10;
// int b = 6 -> This won't work and will give us error (redefined)
if(true){
int b = 7;
cout<<b<<endl; // scope inside if statement
}
cout<<b<<endl; // scope inside main()
cout<<global; // printing global variable
return 0;
}
// post and pre operators
#include<iostream>
using namespace std;
int main(){
int a = 10;
// Pre increment means -> first increment and then use
// Post increment means -> first use and then increment
cout<<(++a)<<endl; // op is 11
cout<<(a++)<<endl; // op is 11
cout<<a<<endl; // op is 12
int c = 6;
int d = ++c + 1; // -> 8 as the output
int e = 5;
cout<<(++e) * (++e)<<endl; // Homework question (find out why)
return 0;
}
// bitwise
#include<iostream>
using namespace std;
int main(){
bool a = false;
bool b = false;
cout<<(a&b)<<endl; // BITWISE AND
cout<<(a|b)<<endl; // BITWISE OR
cout<<(~a)<<endl; // BITWISE NOT
cout<<(a&b)<<endl; // BITWISE AND
cout<<(2&3)<<endl; // BITWISE AND on 2 numbers
cout<<(3|4)<<endl; // BITWISE OR on 2 numbers
int c = 12;
cout<<c<<endl;
return 0;
}
1) a = false -> ~a = -1 but how to get 1 here like negation of 0 is 1 but
we are getting -1, do hit and trial and try to know whether we can
get ~0 as 1 or not.
2) Why can't we confidently say that in right shift operator , we can
not say that it is always divide by 2? and why can we say that in left
shift it is always multiply by 2
3) int a = 5; why is the output of (++a) * (++a) = 49. Use the concept
of precedence in this
4) Search what can be given inside the expression of switch i.e.
float,string,statement,character,number? Explore them
5) Can we use continue inside switch instead of continue?
6) Using global variable is a bad practice. Why?