Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // coded by newt
- // not compatible with programs running c++ yet; for educational purposes only
- // all codes below (except // codes) are original and thought of in the brightest way possible
- // they are all examples, so feel free to modify them in any manner if possible
- // syntax of ternary operators:
- (condition) ? (if_true) : (if_false)
- // example of a ternary operator:
- int x=15;
- int y=(x==15)? 10:20; //here it means if x is equal to 15 then y will be assigned 10 otherwise 20
- cout<<(x<=10)?"Good";"Not so good";
- // syntax of the IF, ELSE construct:
- if(condition)
- {
- statement 1;
- }
- else
- {
- statement 2;
- }
- // example of the IF, ELSE construct:
- if(a>b)
- {
- cout<<"A is greater than B";
- }
- else
- {
- cout<<"B is greater than A";
- }
- // first example of the nested IF, ELSE/IF, ELSE ladder:
- if(condition1)
- {
- statement 1;
- }
- else if(condition2)
- {
- statement 2;
- }
- else
- {
- statement 3;
- }
- // second example of the nested IF, ELSE/IF, ELSE ladder:
- if(condition1)
- if(condition2)
- .
- .
- .
- if(condition3)
- {
- statement 1;
- }
- else
- {
- statement 2;
- .
- .
- .
- }
- else
- {
- statement 3;
- }
- else
- {
- statement 4;
- }
- // syntax of the SWITCH, CASE construct:
- switch(switch variable)
- {
- case <constant1>: statement1;
- .
- .
- .
- statement n;
- break;
- .
- .
- .
- case <constantn>: statement1;
- .
- .
- .
- statement n;
- break;
- .
- .
- .
- default : statement1;
- .
- .
- .
- statement n;
- }
- // example of the default case option in the SWITCH, CASE construct:
- switch(choice)
- {
- case 1: cout<<"Sum: ";
- cout<<a+b<<end1;
- break;
- case 2: cout<<"Difference: ";
- cout<<a-b<<end1;
- break;
- case 3: cout<<"Product: ";
- cout<<a*b<<end1;
- break;
- case 4: cout<<"Quotient: ";
- cout<<a/b<<end1;
- break;
- case 5: cout<<"Remainder: ";
- cout<<a%b<<end1;
- break;
- default: cout<<"Invalid choice!";
- }
- // to accept a number from the user and check if it is an odd or even number:
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int x;
- cout<<"Enter a number";
- cin>>x;
- if(x%2==0) // checks if the remainder is 0
- cout<<"The number you entered is an even number";
- else
- cout<<"The number you entered is an odd number";
- }
- // to accept and alphanumeric value from the user and check if it is in upper case or lower case:
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- char alpha;
- cout<<"Enter an alphabet";
- cin>>alpha;
- if(alpha>='a' && alpha<='z')
- cout<<"The alphabet you entered is in lower case";
- else if(alpha>='A' && alpha<='Z')
- cout<<"The alphabet you entered is in upper case";
- }
- // to accept marks of a student in 5 subjects:
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- int m1,m2,m3,m4,m5,per;
- char grade;
- cout<<"Enter your marks in any five subjects: "<<end1;
- cin>>m1>>m2>>m3>>m4>>m5;
- per = (m1+m2+m3+m4+m5)/500 * 100;
- if(per>=90)
- grade='A';
- else if(per>=80)
- grade='B';
- else if(per>=70)
- grade='C';
- else if(per>=60)
- grade='D';
- else if(per>=50)
- grade='E';
- else
- grade='F';
- cout<<"Your grade is: "<<grade;
- }
- // to accept a number from the user and check if it is a big, medium or small number:
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr;
- int num;
- cout<<"Enter a number between 0 and 100";
- cin>>num;
- if num>>100;
- cout<<"The number you entered is a big number";
- else if num>>90;
- cout<<"The number you entered is a big number";
- else if num>>80;
- cout<<"The number you entered is a big number";
- else if num>>70;
- cout<<"The number you entered is a medium number";
- else if num>>60;
- cout<<"The number you entered is a medium number";
- else if num>>50;
- cout<<"The number you entered is a medium number";
- else if num>>40;
- cout<<"The number you entered is a medium number";
- else if num>>30;
- cout<<"The number you entered is a medium number";
- else if num>>20;
- cout<<"The number you entered is a small number";
- else if num>>10;
- cout<<"The number you entered is a small number";
- else if num>>0;
- cout<<"The number you entered is zero";
- else
- cout<<"Incorrect number! Enter a number divisible by 10";
- }
- // more c++ codes coming soon!
- // congratulations! you have finished newt's c++ example guide!
- // if you read through everything and used some of it - GREAT!
- // if you just scrolled to the end - LOSER!
- // copyright 2014 newt drost
- // distributed by the newt productions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement