75% developed

Intro To C++/Making statement

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Intro To C++
Playing with operator Making statement Reading & Writing Files

Controlling with if and else[edit | edit source]

The C++ if keyword performs the basic conditional test. If something is true, then perform action. And it's syntax looks like this:

if(test-expression){statements-to-perform-when-true}

We could want the different action if that relationship is false. By appending else statement, it is possible.if-else statement looks like this:

if(test-expression){statements-to-perform-when-true} else{statements-to-perform-when-true}

#include <iostream>;
using namespace std;

int main{
 int i=18;
 if(i>17){ 
   cout << "It's nice temperature." << endl;
 return 0;
}
#include <iostream>;
using namespace std;

int main{
 int i=4;
 if(i<15){ cout << "Too cold, turn on the heater."<< endl;}
 else if(15<=i<25){ cout << "It's nice temperature."<< endl;}
 else{ cout << "Too hot! Please turn on the air conditioner" << endl;}
 return 0;
}

Controlling with switch[edit | edit source]

When we have multiple case to check, using if-else statement repeatedly is not efficient.In this case, switch is more useful. The switch works in the different way. A given variable-value in parentheses try to find out a matching value among several case values in braces and execute the statements according to case value. Each statement finish with semicolon. Each value in braces finish with break;. break; let the switch block stop as the statement is executed and switch block satisfied the program. The switch syntax is as follows:

switch(variable-value){
case value1: statements-to-be-executed;break;
case value2: statements-to-be-executed;break;
...................
case valuen: statements-to-be-executed;break;
default:statements-to-be-executed; }

#include <iostream>
using namespace std;

int main(){
int season=2

switch( season )
 {
  case 1: cout << season <<":spring with sprout ";break;
  case 2: cout << season <<":cool summer with ice tea";break;
  case 3: cout << season <<"colorful autumn with fallen leaves:";break;
  case 4: cout << season <<":snowy winter and hot chocolate";break;
  }
  return 0;
}

Looping[edit | edit source]

A loop is a piece of code in the program that automatically repeat. The three type of loop in C++ programming are for loop, while loop and do while loop. The syntaxes are as following table:

for loop for(initializer; test-expression;incrementer){statements-to-be executed}
while loop while(test-expression){statements-to-be executed}
Do while loop do{statements-to-be executed}while(test-expression)
#include <iostream>
using namespace std;
int main(){
 cout<<"Fibonacci sequence by For loop"<< endl;
 int i,j=0;
 for(i=0;i<10;++i){
 cout << j+=i << endl;
 return 0;
}
#include <iostream>
using namespace std;
int main(){
 cout<<"Fibonacci sequence by while loop"<< endl;
 int i=0,j=0;
 while(i<10){cout << j+=i << endl;++i;}
 return 0;
}
#include <iostream>
using namespace std;
int main(){
 cout<<"Fibonacci sequence by do while loop"<< endl;
 int i=0,j=0;
 do{cout << j+=i << endl;++i;} while(i<10)
 return 0;
}
#include <iostream>
using namespace std;
int main(){

return 0;
}

Declaring & Defining Functions[edit | edit source]

In C++, a function is a group of code that provides some functionality to the program. When a function is called from the main program, the statements in a function are executed. Functions make program code easier and help working several programmer together. Tested functions can be reused. Each function needs to be declared at the start of the program. The syntax of a function prototype declaration is as follows:

return-data-type function-name(arguments-data-type list)

Function could be defined outside main function after declaration of function. The syntax of a function looks like this:

return-data-type function-name(arguments-data-type list){statements-to-be-executed}

#include <iostream>
using namespace std;
float trianglearea(float, float);
int main(){
 float x,y,z;
 cout << "Enter the width of triangle area:"<< endl;
 cin >> x;
 cout <<endl<<"Enter the height of triangle area:"<< endl;
 cin >> y;
 z=trianglearea(x,y);
 cout <<endl<<"The dimension of triangle area:"<< z <<"sq.ft"<< endl;
return 0;
}
float trianglearea(float width, float height){
 return ((width/2)*height);
}